From 552ff08cc6f22874023591dda25407a2ad8ef001 Mon Sep 17 00:00:00 2001 From: "jens.weigele" Date: Wed, 27 Jul 2022 16:25:44 +0200 Subject: [PATCH] fix (service): add respect for fallback language for translateObject --- .../src/lib/tests/i18n-mocks/en.json | 6 +++- .../lib/tests/service/translateObject.spec.ts | 17 +++++++++++ libs/transloco/src/lib/transloco.service.ts | 29 +++++++++++++++++-- 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/libs/transloco/src/lib/tests/i18n-mocks/en.json b/libs/transloco/src/lib/tests/i18n-mocks/en.json index ceeb1813a..5eed7d30c 100644 --- a/libs/transloco/src/lib/tests/i18n-mocks/en.json +++ b/libs/transloco/src/lib/tests/i18n-mocks/en.json @@ -15,5 +15,9 @@ "desc": "Desc english" }, "key.is.like.path": "key is like path", - "empty": "" + "empty": "", + "fallback-object":{ + "title": "Title english", + "desc": "Desc english" + } } diff --git a/libs/transloco/src/lib/tests/service/translateObject.spec.ts b/libs/transloco/src/lib/tests/service/translateObject.spec.ts index 1ff9b72e2..a26ea79ef 100644 --- a/libs/transloco/src/lib/tests/service/translateObject.spec.ts +++ b/libs/transloco/src/lib/tests/service/translateObject.spec.ts @@ -24,6 +24,23 @@ describe('translateObject', () => { }); })); + it('should return a nested object from the fallback language', fakeAsync(() => { + service = createService({ + fallbackLang: 'en', + missingHandler: { + useFallbackTranslation: true + } + }) + loadLang(service, 'es'); + loadLang(service, 'en'); + service.setActiveLang('es'); + + expect(service.translateObject('fallback-object')).toEqual({ + "title": "Title english", + "desc": "Desc english" + }); + })); + it('should should support params', fakeAsync(() => { loadLang(service); expect( diff --git a/libs/transloco/src/lib/transloco.service.ts b/libs/transloco/src/lib/transloco.service.ts index b5576c1a9..db00b84a9 100644 --- a/libs/transloco/src/lib/transloco.service.ts +++ b/libs/transloco/src/lib/transloco.service.ts @@ -398,9 +398,10 @@ export class TranslocoService implements OnDestroy { key = scope ? `${scope}.${key}` : key; const value = unflatten(this.getObjectByKey(translation, key)); - /* If an empty object was returned we want to try and translate the key as a string and not an object */ + /* If an empty object was returned we want to try the same with the fallback language and afterwards translate + the key as a string and not an object */ return isEmpty(value) - ? this.translate(key, params!, lang) + ? this._handleMissingKeyForObject(key, value, params!, lang) : this.parser.transpile(value, params!, translation); } @@ -624,6 +625,30 @@ export class TranslocoService implements OnDestroy { ); } + /** + * @internal + */ + _handleMissingKeyForObject(key: string, value: any, params: HashMap | undefined, lang: string) { + if (this.config.missingHandler!.allowEmpty && value === '') { + return ''; + } + + if (!this.isResolvedMissingOnce && this.useFallbackTranslation()) { + // We need to set it to true to prevent a loop + this.isResolvedMissingOnce = true; + const fallbackValue = this.translateObject( + key, + params, + this.firstFallbackLang! + ); + this.isResolvedMissingOnce = false; + + return fallbackValue; + } + + return this.translate(key, params!, lang); + } + /** * @internal */