Skip to content

Commit

Permalink
feat(transloco): add option to not change the casing of the provided …
Browse files Browse the repository at this point in the history
…scope
  • Loading branch information
maartentibau committed Sep 17, 2024
1 parent 85cb958 commit 2efec71
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 4 deletions.
11 changes: 11 additions & 0 deletions docs/docs/getting-started/config-options.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
})
```
3 changes: 2 additions & 1 deletion docs/docs/lazy-load/scope-configuration.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
Expand Down
7 changes: 6 additions & 1 deletion libs/transloco/src/lib/scope-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
28 changes: 28 additions & 0 deletions libs/transloco/src/lib/tests/scope-resolver.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ describe('ScopeResolver', () => {
spy = jasmine.createSpy('setScopeAlias');
resolver = new ScopeResolver({
_setScopeAlias: spy,
config: {
scopes: {
keepCasing: false,
},
},
} as any);
});

Expand Down Expand Up @@ -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');
});
});
11 changes: 11 additions & 0 deletions libs/transloco/src/lib/tests/service/setTranslation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
10 changes: 10 additions & 0 deletions libs/transloco/src/lib/transloco.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ export interface TranslocoConfig {
allowEmpty: boolean;
};
interpolation: [string, string];
scopes: {
keepCasing?: boolean;
};
}

export const TRANSLOCO_CONFIG = new InjectionToken<TranslocoConfig>(
Expand All @@ -44,6 +47,9 @@ export const defaultConfig: TranslocoConfig = {
aot: false,
},
interpolation: ['{{', '}}'],
scopes: {
keepCasing: false,
},
};

type DeepPartial<T> =
Expand All @@ -69,5 +75,9 @@ export function translocoConfig(
...defaultConfig.flatten,
...config.flatten,
},
scopes: {
...defaultConfig.scopes,
...config.scopes,
},
};
}
6 changes: 4 additions & 2 deletions libs/transloco/src/lib/transloco.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
);
}

/**
Expand Down

0 comments on commit 2efec71

Please sign in to comment.