diff --git a/docs/docs/getting-started/config-options.mdx b/docs/docs/getting-started/config-options.mdx index f4360800..558c7b7a 100644 --- a/docs/docs/getting-started/config-options.mdx +++ b/docs/docs/getting-started/config-options.mdx @@ -133,3 +133,14 @@ translocoConfig({ interpolation: ['<<<', '>>>'], }); ``` + +### `scopes.keepCasing` +This will make sure that the casing of the provided scope name is not altered, given that no alias has been set otherwise this setting will be ignored. + +```ts +translocoConfig({ + scopes: { + keepCasing: false + } +}) +``` diff --git a/docs/docs/lazy-load/scope-configuration.mdx b/docs/docs/lazy-load/scope-configuration.mdx index e45b3810..ed614812 100644 --- a/docs/docs/lazy-load/scope-configuration.mdx +++ b/docs/docs/lazy-load/scope-configuration.mdx @@ -139,8 +139,9 @@ Now we can access each one of the `todos` keys by using the `todos` namespace: ``` ## Scope's namespace +By default, the namespace will be the `scope`'s name camel-cased. you can keep the original casing by providing the [`scope.keepCasing`](../getting-started/config-options#scopeskeepcasing) config option. -By default, the namespace will be the `scope` name (camel cased), but we can override it by providing custom `alias` name in the module/component `scope` provider: +You can also provide a custom scope namespace by specifying an `alias` name in the module/component `scope` provider: ```ts provideTranslocoScope({ scope: 'todos', alias: 'customName' }); diff --git a/libs/transloco/src/lib/scope-resolver.ts b/libs/transloco/src/lib/scope-resolver.ts index bd3f6ee8..057eb3a5 100644 --- a/libs/transloco/src/lib/scope-resolver.ts +++ b/libs/transloco/src/lib/scope-resolver.ts @@ -19,7 +19,12 @@ export class ScopeResolver { if (provider) { if (isScopeObject(provider)) { - const { scope, alias = toCamelCase(scope) } = provider as ProviderScope; + const { + scope, + alias = this.service.config.scopes.keepCasing + ? scope + : toCamelCase(scope), + } = provider as ProviderScope; this.service._setScopeAlias(scope, alias); return scope; diff --git a/libs/transloco/src/lib/tests/scope-resolver.spec.ts b/libs/transloco/src/lib/tests/scope-resolver.spec.ts index b38dc994..c60f0712 100644 --- a/libs/transloco/src/lib/tests/scope-resolver.spec.ts +++ b/libs/transloco/src/lib/tests/scope-resolver.spec.ts @@ -8,6 +8,11 @@ describe('ScopeResolver', () => { spy = jasmine.createSpy('setScopeAlias'); resolver = new ScopeResolver({ _setScopeAlias: spy, + config: { + scopes: { + keepCasing: false, + }, + }, } as any); }); @@ -82,4 +87,27 @@ describe('ScopeResolver', () => { 'nestedScopesAdminPage', ); }); + + it('should keep the original scope name', () => { + resolver = new ScopeResolver({ + _setScopeAlias: spy, + config: { + scopes: { + keepCasing: true, + }, + }, + } as any); + + expect( + resolver.resolve({ + inline: undefined, + provider: { + scope: 'AdMiN-pAgE', + }, + }), + ).toEqual('AdMiN-pAgE'); + // one from before + expect(spy).toHaveBeenCalledTimes(1); + expect(spy).toHaveBeenCalledWith('AdMiN-pAgE', 'AdMiN-pAgE'); + }); }); diff --git a/libs/transloco/src/lib/tests/service/setTranslation.spec.ts b/libs/transloco/src/lib/tests/service/setTranslation.spec.ts index 797c37c8..46f523ad 100644 --- a/libs/transloco/src/lib/tests/service/setTranslation.spec.ts +++ b/libs/transloco/src/lib/tests/service/setTranslation.spec.ts @@ -102,5 +102,16 @@ describe('setTranslation', () => { }; expect(setTranslationsSpy).toHaveBeenCalledWith('en', merged); }); + + it("should not change scope's name given scope.keepCasing is set to true", () => { + service.config.scopes.keepCasing = true; + lang = 'LAZY-page/en'; + service.setTranslation(translation, lang); + const merged = { + ...flatten(mockLangs.en), + ...flatten({ 'LAZY-page': { ...translation } }), + }; + expect(setTranslationsSpy).toHaveBeenCalledWith('en', merged); + }); }); }); diff --git a/libs/transloco/src/lib/transloco.config.ts b/libs/transloco/src/lib/transloco.config.ts index 6adafffc..b162adda 100644 --- a/libs/transloco/src/lib/transloco.config.ts +++ b/libs/transloco/src/lib/transloco.config.ts @@ -18,6 +18,9 @@ export interface TranslocoConfig { allowEmpty: boolean; }; interpolation: [string, string]; + scopes: { + keepCasing?: boolean; + }; } export const TRANSLOCO_CONFIG = new InjectionToken( @@ -44,6 +47,9 @@ export const defaultConfig: TranslocoConfig = { aot: false, }, interpolation: ['{{', '}}'], + scopes: { + keepCasing: false, + }, }; type DeepPartial = @@ -69,5 +75,9 @@ export function translocoConfig( ...defaultConfig.flatten, ...config.flatten, }, + scopes: { + ...defaultConfig.scopes, + ...config.scopes, + }, }; } diff --git a/libs/transloco/src/lib/transloco.service.ts b/libs/transloco/src/lib/transloco.service.ts index 0ab9be01..40856cf9 100644 --- a/libs/transloco/src/lib/transloco.service.ts +++ b/libs/transloco/src/lib/transloco.service.ts @@ -789,8 +789,10 @@ export class TranslocoService implements OnDestroy { } private getMappedScope(scope: string): string { - const { scopeMapping = {} } = this.config; - return scopeMapping[scope] || toCamelCase(scope); + const { scopeMapping = {}, scopes = { keepCasing: false } } = this.config; + return ( + scopeMapping[scope] || (scopes.keepCasing ? scope : toCamelCase(scope)) + ); } /**