From 1bd2ee0bb0e78e87ba2bb86b098f3f1ab04252f3 Mon Sep 17 00:00:00 2001 From: Maarten Tibau Date: Thu, 12 Sep 2024 21:29:59 +0200 Subject: [PATCH] feat(transloco): add option to not change the casing of the provided scope --- docs/docs/getting-started/config-options.mdx | 11 ++++++++++ docs/docs/lazy-load/scope-configuration.mdx | 3 ++- libs/transloco/src/lib/scope-resolver.ts | 7 ++++++- .../src/lib/tests/scope-resolver.spec.ts | 21 +++++++++++++++++++ .../lib/tests/service/setTranslation.spec.ts | 11 ++++++++++ libs/transloco/src/lib/transloco.config.ts | 10 +++++++++ libs/transloco/src/lib/transloco.service.ts | 6 ++++-- 7 files changed, 65 insertions(+), 4 deletions(-) diff --git a/docs/docs/getting-started/config-options.mdx b/docs/docs/getting-started/config-options.mdx index f43608006..558c7b7a0 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 e45b3810a..ed6148126 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 bd3f6ee83..057eb3a50 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 b38dc994e..8caa8ed24 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,20 @@ 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'); + 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 797c37c89..46f523adf 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 6adafffcb..b162addaf 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 0ab9be01f..40856cf9c 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)) + ); } /**