Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix (service): add respect for fallback language for translateObject #583

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion libs/transloco/src/lib/tests/i18n-mocks/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
17 changes: 17 additions & 0 deletions libs/transloco/src/lib/tests/service/translateObject.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
29 changes: 27 additions & 2 deletions libs/transloco/src/lib/transloco.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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
*/
Expand Down