Skip to content

Commit a84ceb3

Browse files
authored
fix: 🐛 add key prefix when translating scope (#116)
1 parent 9405847 commit a84ceb3

File tree

4 files changed

+68
-11
lines changed

4 files changed

+68
-11
lines changed

projects/ngneat/transloco/src/lib/tests/transloco.mocks.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import enLazy from '../../../../../../src/assets/i18n/lazy-page/en.json';
1010
import esLazy from '../../../../../../src/assets/i18n/lazy-page/es.json';
1111
import enLazyScopeAlias from '../../../../../../src/assets/i18n/lazy-scope-alias/en.json';
1212
import esLazyScopeAlias from '../../../../../../src/assets/i18n/lazy-scope-alias/es.json';
13+
import enMF from '../../../../../../src/assets/i18n/transpilers/messageformat/en.json';
14+
import esMF from '../../../../../../src/assets/i18n/transpilers/messageformat/es.json';
1315
import { tick } from '@angular/core/testing';
1416
import { TranslocoService } from '../transloco.service';
1517
import { TRANSLOCO_LOADING_TEMPLATE } from '../transloco-loading-template';
@@ -23,7 +25,9 @@ export const mockLangs = {
2325
'lazy-page/en': enLazy,
2426
'lazy-page/es': esLazy,
2527
'lazy-scope-alias/en': enLazyScopeAlias,
26-
'lazy-scope-alias/es': esLazyScopeAlias
28+
'lazy-scope-alias/es': esLazyScopeAlias,
29+
'transpilers/messageformat/en': enMF,
30+
'transpilers/messageformat/es': esMF
2731
};
2832

2933
export const loader = {

projects/ngneat/transloco/src/lib/tests/transloco.service.spec.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,19 +122,47 @@ describe('TranslocoService', () => {
122122
expect(spy).toHaveBeenCalledTimes(1);
123123
}));
124124

125-
it('should support scoped lang', fakeAsync(() => {
125+
it('should support scope', fakeAsync(() => {
126126
const spy = createSpy();
127-
service.selectTranslate('lazyPage.title', null, 'lazy-page').subscribe(spy);
127+
service.selectTranslate('title', null, 'lazy-page').subscribe(spy);
128128
runLoader();
129129
expect(spy).toHaveBeenCalledWith('Admin Lazy english');
130130
}));
131131

132-
it('should support scoped lang with param', fakeAsync(() => {
132+
it('should support scope with lang', fakeAsync(() => {
133133
const spy = createSpy();
134-
service.selectTranslate('lazyPage.withParam', { param: 'Transloco' }, 'lazy-page').subscribe(spy);
134+
service.selectTranslate('title', null, 'lazy-page/es').subscribe(spy);
135+
runLoader();
136+
expect(spy).toHaveBeenCalledWith('Admin Lazy spanish');
137+
}));
138+
139+
it('should support scope with param', fakeAsync(() => {
140+
const spy = createSpy();
141+
service.selectTranslate('withParam', { param: 'Transloco' }, 'lazy-page').subscribe(spy);
135142
runLoader();
136143
expect(spy).toHaveBeenCalledWith('Admin Lazy english Transloco');
137144
}));
145+
146+
it('should support scope with lang and params', fakeAsync(() => {
147+
const spy = createSpy();
148+
service.selectTranslate('withParam', { param: 'Transloco' }, 'lazy-page/es').subscribe(spy);
149+
runLoader();
150+
expect(spy).toHaveBeenCalledWith('Admin Lazy spanish Transloco');
151+
}));
152+
153+
it('should support nested scope with params', fakeAsync(() => {
154+
const spy = createSpy();
155+
service.selectTranslate('params', { value: 'Transloco' }, 'transpilers/messageformat').subscribe(spy);
156+
runLoader();
157+
expect(spy).toHaveBeenCalledWith('Replaces standard Transloco - english');
158+
}));
159+
160+
it('should support nested scope with lang and params', fakeAsync(() => {
161+
const spy = createSpy();
162+
service.selectTranslate('params', { value: 'Transloco' }, 'transpilers/messageformat/es').subscribe(spy);
163+
runLoader();
164+
expect(spy).toHaveBeenCalledWith('Replaces standard Transloco - spanish');
165+
}));
138166
});
139167

140168
describe('translateObject', () => {

projects/ngneat/transloco/src/lib/transloco.service.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,21 @@ export class TranslocoService implements OnDestroy {
163163
* translate('scope.someKey', { }, 'en')
164164
*/
165165
translate<T = any>(key: TranslateParams, params: HashMap = {}, lang = this.getActiveLang()): T {
166-
const resolveLang = this._isLangScoped(lang) ? this.getActiveLang() : lang;
166+
let resolveLang = lang;
167+
let scope;
168+
if (this._isLangScoped(lang)) {
169+
const langFromScope = getLangFromScope(lang);
170+
const hasLang = this._isLang(langFromScope);
171+
resolveLang = hasLang ? langFromScope : this.getActiveLang();
172+
scope = this.getMappedScope(hasLang ? getScopeFromLang(lang) : lang);
173+
}
174+
167175
if (Array.isArray(key)) {
168-
return key.map(k => this.translate(k, params, resolveLang)) as any;
176+
return key.map(k => this.translate(scope ? `${scope}.${k}` : k, params, resolveLang)) as any;
169177
}
170178

179+
key = scope ? `${scope}.${key}` : key;
180+
171181
if (!key) {
172182
return this.missingHandler.handle(key, this.config);
173183
}
@@ -292,8 +302,7 @@ export class TranslocoService implements OnDestroy {
292302

293303
// Merged the scoped language into the active language
294304
if (scope) {
295-
const { scopeMapping = {} } = this.config;
296-
const key = scopeMapping[scope] || toCamelCase(scope);
305+
const key = this.getMappedScope(scope);
297306
flattenScopeOrTranslation = flatten({ [key]: translation });
298307
}
299308

@@ -351,6 +360,13 @@ export class TranslocoService implements OnDestroy {
351360
return this.getAvailableLangsIds().indexOf(lang) === -1;
352361
}
353362

363+
/**
364+
* @internal
365+
*/
366+
_isLang(lang: string) {
367+
return this.getAvailableLangsIds().indexOf(lang) !== -1;
368+
}
369+
354370
/**
355371
* @internal
356372
*
@@ -371,7 +387,10 @@ export class TranslocoService implements OnDestroy {
371387
* @internal
372388
*/
373389
_completeScopeWithLang(langOrScope: string) {
374-
return this._isLangScoped(langOrScope) ? `${langOrScope}/${this.getActiveLang()}` : langOrScope;
390+
if (this._isLangScoped(langOrScope) && !this._isLang(getLangFromScope(langOrScope))) {
391+
return `${langOrScope}/${this.getActiveLang()}`;
392+
}
393+
return langOrScope;
375394
}
376395

377396
/**
@@ -463,4 +482,9 @@ export class TranslocoService implements OnDestroy {
463482
ngOnDestroy() {
464483
this.subscription.unsubscribe();
465484
}
485+
486+
private getMappedScope(scope: string): string {
487+
const { scopeMapping = {} } = this.config;
488+
return scopeMapping[scope] || toCamelCase(scope);
489+
}
466490
}

src/assets/i18n/lazy-page/es.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
"title": "Admin Lazy spanish"
2+
"title": "Admin Lazy spanish",
3+
"withParam": "Admin Lazy spanish {{param}}"
34
}

0 commit comments

Comments
 (0)