@@ -392,64 +392,68 @@ export function createComposer(options: ComposerOptions = {}): Composer {
392392 _context . missing = _runtimeMissing
393393 }
394394
395- // t
396- const t = ( ...args : unknown [ ] ) : string => {
397- return computed < string > ( ( ) : string => {
398- const ret = translate ( _context , ...args )
399- if ( isNumber ( ret ) && ret === NOT_REOSLVED ) {
400- const [ key ] = parseTranslateArgs ( ...args )
401- if ( __DEV__ && _fallbackRoot && __root ) {
402- warn ( `Fall back to translate '${ key } ' with root locale.` )
395+ const defineComputed = < T > (
396+ fn : ( context : RuntimeContext ) => unknown ,
397+ argumentParser : ( ) => string ,
398+ warnType : string ,
399+ fallbackSuccess : ( root : Composer ) => T ,
400+ fallbackFail : ( key : string ) => T ,
401+ successCondition : ( val : unknown ) => boolean
402+ ) : ComputedRef < T > => {
403+ return computed < T > (
404+ ( ) : T => {
405+ const ret = fn ( _context )
406+ if ( isNumber ( ret ) && ret === NOT_REOSLVED ) {
407+ const key = argumentParser ( )
408+ if ( __DEV__ && _fallbackRoot && __root ) {
409+ warn ( `Fall back to ${ warnType } '${ key } ' with root locale.` )
410+ }
411+ return _fallbackRoot && __root
412+ ? fallbackSuccess ( __root )
413+ : fallbackFail ( key )
414+ } else if ( successCondition ( ret ) ) {
415+ return ret as T
416+ } else {
417+ throw new Error ( 'TODO:' ) // TODO
403418 }
404- return _fallbackRoot && __root ? __root . t ( ...args ) : key
405- } else if ( isString ( ret ) ) {
406- return ret
407- } else {
408- throw new Error ( 'TODO:' ) // TODO
409419 }
410- } ) . value
420+ )
421+ }
422+
423+ // t
424+ const t = ( ...args : unknown [ ] ) : string => {
425+ return defineComputed < string > (
426+ context => translate ( context , ...args ) ,
427+ ( ) => parseTranslateArgs ( ...args ) [ 0 ] ,
428+ 'translate' ,
429+ root => root . t ( ...args ) ,
430+ key => key ,
431+ val => isString ( val )
432+ ) . value
411433 }
412434
413435 // d
414436 const d = ( ...args : unknown [ ] ) : string => {
415- return computed < string > ( ( ) : string => {
416- const ret = datetime ( _context , ...args )
417- if ( isNumber ( ret ) && ret === NOT_REOSLVED ) {
418- const [ , options ] = parseDateTimeArgs ( ...args )
419- if ( __DEV__ && _fallbackRoot && __root ) {
420- const key = isString ( options . key ) ? options . key : ''
421- warn ( `Fall back to datetime format '${ key } ' with root locale.` )
422- }
423- return _fallbackRoot && __root
424- ? __root . d ( ...args )
425- : MISSING_RESOLVE_VALUE
426- } else if ( isString ( ret ) ) {
427- return ret
428- } else {
429- throw new Error ( 'TODO:' ) // TODO
430- }
431- } ) . value
437+ return defineComputed < string > (
438+ context => datetime ( context , ...args ) ,
439+ ( ) => parseDateTimeArgs ( ...args ) [ 0 ] ,
440+ 'datetime format' ,
441+ root => root . d ( ...args ) ,
442+ ( ) => MISSING_RESOLVE_VALUE ,
443+ val => isString ( val )
444+ ) . value
432445 }
433446
434447 // n
435448 const n = ( ...args : unknown [ ] ) : string => {
436- return computed < string > ( ( ) : string => {
437- const ret = number ( _context , ...args )
438- if ( isNumber ( ret ) && ret === NOT_REOSLVED ) {
439- const [ , options ] = parseNumberArgs ( ...args )
440- if ( __DEV__ && _fallbackRoot && __root ) {
441- const key = isString ( options . key ) ? options . key : ''
442- warn ( `Fall back to number format '${ key } ' with root locale.` )
443- }
444- return _fallbackRoot && __root
445- ? __root . d ( ...args )
446- : MISSING_RESOLVE_VALUE
447- } else if ( isString ( ret ) ) {
448- return ret
449- } else {
450- throw new Error ( 'TODO:' ) // TODO
451- }
452- } ) . value
449+ return defineComputed < string > (
450+ context => number ( context , ...args ) ,
451+ ( ) => parseNumberArgs ( ...args ) [ 0 ] ,
452+ 'number format' ,
453+ root => root . n ( ...args ) ,
454+ ( ) => MISSING_RESOLVE_VALUE ,
455+ val => isString ( val )
456+ ) . value
453457 }
454458
455459 // for custom processor
@@ -465,73 +469,51 @@ export function createComposer(options: ComposerOptions = {}): Composer {
465469
466470 // __transrateVNode, using for `i18n-t` component
467471 const __transrateVNode = ( ...args : unknown [ ] ) : unknown => {
468- return computed < unknown > ( ( ) : unknown => {
469- let ret : unknown
470- try {
471- // translate with custom processor
472- _context . processor = processor
473- ret = translate ( _context , ...args )
474- } finally {
475- _context . processor = null
476- }
477- if ( isNumber ( ret ) && ret === NOT_REOSLVED ) {
478- const [ key ] = parseTranslateArgs ( ...args )
479- if ( __DEV__ && _fallbackRoot && __root ) {
480- warn ( `Fall back to translate '${ key } ' with root locale.` )
472+ return defineComputed < unknown > (
473+ context => {
474+ let ret : unknown
475+ try {
476+ context . processor = processor
477+ ret = translate ( context , ...args )
478+ } finally {
479+ context . processor = null
481480 }
482- return _fallbackRoot && __root ? __root . __transrateVNode ( ...args ) : key
483- } else if ( isArray ( ret ) ) {
484481 return ret
485- } else {
486- throw new Error ( 'TODO:' ) // TODO
487- }
488- } ) . value
482+ } ,
483+ ( ) => parseTranslateArgs ( ...args ) [ 0 ] ,
484+ 'translate' ,
485+ root => root . __transrateVNode ( ...args ) ,
486+ key => key ,
487+ val => isArray ( val )
488+ ) . value
489489 }
490490
491491 // __numberParts, using for `i18n-n` component
492492 const __numberParts = (
493493 ...args : unknown [ ]
494494 ) : string | Intl . NumberFormatPart [ ] => {
495- return computed < string | Intl . NumberFormatPart [ ] > ( ( ) :
496- | string
497- | Intl . NumberFormatPart [ ] => {
498- const ret = number ( _context , ...args )
499- if ( isNumber ( ret ) && ret === NOT_REOSLVED ) {
500- const [ , options ] = parseNumberArgs ( ...args )
501- if ( __DEV__ && _fallbackRoot && __root ) {
502- const key = isString ( options . key ) ? options . key : ''
503- warn ( `Fall back to number format '${ key } ' with root locale.` )
504- }
505- return _fallbackRoot && __root ? __root . __numberParts ( ...args ) : [ ]
506- } else if ( isString ( ret ) || isArray ( ret ) ) {
507- return ret
508- } else {
509- throw new Error ( 'TODO:' ) // TODO
510- }
511- } ) . value
495+ return defineComputed < string | Intl . NumberFormatPart [ ] > (
496+ context => number ( context , ...args ) ,
497+ ( ) => parseNumberArgs ( ...args ) [ 0 ] ,
498+ 'number format' ,
499+ root => root . __numberParts ( ...args ) ,
500+ ( ) => [ ] ,
501+ val => isString ( val ) || isArray ( val )
502+ ) . value
512503 }
513504
514505 // __datetimeParts, using for `i18n-d` component
515506 const __datetimeParts = (
516507 ...args : unknown [ ]
517508 ) : string | Intl . DateTimeFormatPart [ ] => {
518- return computed < string | Intl . DateTimeFormatPart [ ] > ( ( ) :
519- | string
520- | Intl . DateTimeFormatPart [ ] => {
521- const ret = datetime ( _context , ...args )
522- if ( isNumber ( ret ) && ret === NOT_REOSLVED ) {
523- const [ , options ] = parseDateTimeArgs ( ...args )
524- if ( __DEV__ && _fallbackRoot && __root ) {
525- const key = isString ( options . key ) ? options . key : ''
526- warn ( `Fall back to datetime format '${ key } ' with root locale.` )
527- }
528- return _fallbackRoot && __root ? __root . __datetimeParts ( ...args ) : [ ]
529- } else if ( isString ( ret ) || isArray ( ret ) ) {
530- return ret
531- } else {
532- throw new Error ( 'TODO:' ) // TODO
533- }
534- } ) . value
509+ return defineComputed < string | Intl . DateTimeFormatPart [ ] > (
510+ context => datetime ( context , ...args ) ,
511+ ( ) => parseDateTimeArgs ( ...args ) [ 0 ] ,
512+ 'datetime format' ,
513+ root => root . __datetimeParts ( ...args ) ,
514+ ( ) => [ ] ,
515+ val => isString ( val ) || isArray ( val )
516+ ) . value
535517 }
536518
537519 // getLocaleMessage
0 commit comments