@@ -374,8 +374,9 @@ export class TranslateService implements ITranslateService {
374374 protected getParsedResultForKey (
375375 key : string ,
376376 interpolateParams ?: InterpolationParameters ,
377+ lang ?: Language ,
377378 ) : StrictTranslation | Observable < StrictTranslation > {
378- const textToInterpolate = this . getTextToInterpolate ( key ) ;
379+ const textToInterpolate = this . getTextToInterpolate ( key , lang ) ;
379380
380381 if ( isDefinedAndNotNull ( textToInterpolate ) ) {
381382 return this . runInterpolation ( textToInterpolate , interpolateParams ) ;
@@ -402,7 +403,15 @@ export class TranslateService implements ITranslateService {
402403 return this . isRoot ? this . _fallbackLang ( ) : ( this . parent ?. getFallbackLang ( ) ?? null ) ;
403404 }
404405
405- protected getTextToInterpolate ( key : string ) : InterpolatableTranslation | undefined {
406+ protected getTextToInterpolate ( key : string , lang ?: Language ) : InterpolatableTranslation | undefined {
407+ if ( lang ) {
408+ const res = this . store . getTranslationValue ( lang , key ) ;
409+ if ( res !== undefined ) {
410+ return res ;
411+ }
412+ return this . parent ?. getTextToInterpolate ( key , lang ) ;
413+ }
414+
406415 const currentLang = this . getCurrentLang ( ) ;
407416 const fallbackLang = this . getFallbackLang ( ) ;
408417
@@ -473,21 +482,23 @@ export class TranslateService implements ITranslateService {
473482 public getParsedResult (
474483 key : string | string [ ] ,
475484 interpolateParams ?: InterpolationParameters ,
485+ lang ?: Language ,
476486 ) : StrictTranslation | Observable < StrictTranslation > {
477487 return key instanceof Array
478- ? this . getParsedResultForArray ( key , interpolateParams )
479- : this . getParsedResultForKey ( key , interpolateParams ) ;
488+ ? this . getParsedResultForArray ( key , interpolateParams , lang )
489+ : this . getParsedResultForKey ( key , interpolateParams , lang ) ;
480490 }
481491
482492 protected getParsedResultForArray (
483493 key : string [ ] ,
484494 interpolateParams : InterpolationParameters | undefined ,
495+ lang ?: Language ,
485496 ) {
486497 const result : Record < string , StrictTranslation | Observable < StrictTranslation > > = { } ;
487498
488499 let observables = false ;
489500 for ( const k of key ) {
490- result [ k ] = this . getParsedResultForKey ( k , interpolateParams ) ;
501+ result [ k ] = this . getParsedResultForKey ( k , interpolateParams , lang ) ;
491502 observables = observables || isObservable ( result [ k ] ) ;
492503 }
493504
@@ -514,6 +525,7 @@ export class TranslateService implements ITranslateService {
514525 public get (
515526 key : string | string [ ] ,
516527 interpolateParams ?: InterpolationParameters ,
528+ lang ?: Language ,
517529 ) : Observable < Translation > {
518530 if ( ! isDefinedAndNotNull ( key ) || ! key . length ) {
519531 return of ( "" ) ;
@@ -523,12 +535,12 @@ export class TranslateService implements ITranslateService {
523535 if ( this . lastUseLanguage && this . loadingTranslations [ this . lastUseLanguage ] ) {
524536 return this . loadingTranslations [ this . lastUseLanguage ] . pipe (
525537 concatMap ( ( ) => {
526- return makeObservable ( this . getParsedResult ( key , interpolateParams ) ) ;
538+ return makeObservable ( this . getParsedResult ( key , interpolateParams , lang ) ) ;
527539 } ) ,
528540 ) ;
529541 }
530542
531- return makeObservable ( this . getParsedResult ( key , interpolateParams ) ) ;
543+ return makeObservable ( this . getParsedResult ( key , interpolateParams , lang ) ) ;
532544 }
533545
534546 /**
@@ -539,16 +551,17 @@ export class TranslateService implements ITranslateService {
539551 public getStreamOnTranslationChange (
540552 key : string | string [ ] ,
541553 interpolateParams ?: InterpolationParameters ,
554+ lang ?: Language ,
542555 ) : Observable < Translation > {
543556 if ( ! isDefinedAndNotNull ( key ) || ! key . length ) {
544557 throw new Error ( `Parameter "key" is required and cannot be empty` ) ;
545558 }
546559
547560 return concat (
548- defer ( ( ) => this . get ( key , interpolateParams ) ) ,
561+ defer ( ( ) => this . get ( key , interpolateParams , lang ) ) ,
549562 this . onTranslationChange . pipe (
550563 switchMap ( ( ) => {
551- const res = this . getParsedResult ( key , interpolateParams ) ;
564+ const res = this . getParsedResult ( key , interpolateParams , lang ) ;
552565 return makeObservable ( res ) ;
553566 } ) ,
554567 ) ,
@@ -563,16 +576,17 @@ export class TranslateService implements ITranslateService {
563576 public stream (
564577 key : string | string [ ] ,
565578 interpolateParams ?: InterpolationParameters ,
579+ lang ?: Language ,
566580 ) : Observable < Translation > {
567581 if ( ! isDefinedAndNotNull ( key ) || ! key . length ) {
568582 throw new Error ( `Parameter "key" required` ) ;
569583 }
570584
571585 return concat (
572- defer ( ( ) => this . get ( key , interpolateParams ) ) ,
586+ defer ( ( ) => this . get ( key , interpolateParams , lang ) ) ,
573587 this . onLangChange . pipe (
574588 switchMap ( ( ) => {
575- const res = this . getParsedResult ( key , interpolateParams ) ;
589+ const res = this . getParsedResult ( key , interpolateParams , lang ) ;
576590 return makeObservable ( res ) ;
577591 } ) ,
578592 ) ,
@@ -583,16 +597,20 @@ export class TranslateService implements ITranslateService {
583597 * Returns a translation instantly from the internal state of loaded translation.
584598 * All rules regarding the current language, the preferred language of even fallback languages
585599 * will be used except any promise handling.
600+ *
601+ * When `lang` is provided, the lookup goes directly to the specified language,
602+ * bypassing the current language and fallback chain.
586603 */
587604 public instant (
588605 key : string | string [ ] ,
589606 interpolateParams ?: InterpolationParameters ,
607+ lang ?: Language ,
590608 ) : Translation {
591609 if ( ! isDefinedAndNotNull ( key ) || key . length === 0 ) {
592610 return "" ;
593611 }
594612
595- const result = this . getParsedResult ( key , interpolateParams ) ;
613+ const result = this . getParsedResult ( key , interpolateParams , lang ) ;
596614
597615 return isObservable ( result ) ? this . keyToObject ( key ) : result ;
598616 }
@@ -623,17 +641,21 @@ export class TranslateService implements ITranslateService {
623641 public translate (
624642 key : string | Signal < string > ,
625643 params ?: InterpolationParameters | Signal < InterpolationParameters | undefined > ,
644+ lang ?: Language | Signal < Language | undefined > ,
626645 ) : Signal < Translation | TranslationObject > {
627646 return computed ( ( ) => {
628647 // Unwrap signals if needed
629648 const currentKey = isSignal ( key ) ? key ( ) : key ;
630649 const currentParams = params !== undefined && isSignal ( params )
631650 ? ( params as Signal < InterpolationParameters | undefined > ) ( )
632651 : params ;
652+ const currentLang = lang !== undefined && isSignal ( lang )
653+ ? lang ( )
654+ : lang ;
633655
634656 // instant() internally reads the store's translations() signal,
635657 // which provides reactivity for lang/translation/fallback changes.
636- return this . instant ( currentKey , currentParams ) ;
658+ return this . instant ( currentKey , currentParams , currentLang ) ;
637659 } ) ;
638660 }
639661
0 commit comments