Skip to content

Commit

Permalink
chore(transloco): second review remarks
Browse files Browse the repository at this point in the history
  • Loading branch information
maartentibau committed Sep 14, 2024
1 parent f41cd3b commit 73777bf
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 24 deletions.
7 changes: 4 additions & 3 deletions docs/docs/getting-started/config-options.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,13 @@ translocoConfig({
})
```

### `scopes.keepNamespaceCasing`
### `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({
// 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
scopes: {
keepNamespaceCasing: false
keepCasing: false
}
})
```
5 changes: 2 additions & 3 deletions docs/docs/lazy-load/scope-configuration.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +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 setting the `scope.keepNamespaceCasing` option to `true` in the `config`.

An alternative method to override the scope namespace is by specifying a 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
2 changes: 1 addition & 1 deletion libs/transloco/src/lib/scope-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class ScopeResolver {
if (isScopeObject(provider)) {
const {
scope,
alias = this.service.config.scope.keepNamespaceCasing
alias = this.service.config.scopes.keepCasing
? scope
: toCamelCase(scope),
} = provider as ProviderScope;
Expand Down
11 changes: 8 additions & 3 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 @@ -83,12 +88,12 @@ describe('ScopeResolver', () => {
);
});

it('should not alter namespace of the scope', () => {
it('should keep the original scope name', () => {
resolver = new ScopeResolver({
_setScopeAlias: spy,
config: {
scope: {
keepNamespaceCasing: true,
scopes: {
keepCasing: true,
},
},
} as any);
Expand Down
8 changes: 4 additions & 4 deletions libs/transloco/src/lib/tests/service/setTranslation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,13 @@ describe('setTranslation', () => {
expect(setTranslationsSpy).toHaveBeenCalledWith('en', merged);
});

it("should not change scope's name given scope.keepNamespaceCasing is set to true", () => {
service.config.scope.keepNamespaceCasing = true;
service._setScopeAlias('LAZY-page', 'LAZY-page');
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({ myScopeAlias: { ...translation } }),
...flatten({ 'LAZY-page': { ...translation } }),
};
expect(setTranslationsSpy).toHaveBeenCalledWith('en', merged);
});
Expand Down
14 changes: 7 additions & 7 deletions libs/transloco/src/lib/transloco.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ export interface TranslocoConfig {
allowEmpty: boolean;
};
interpolation: [string, string];
scope: {
keepNamespaceCasing?: boolean;
scopes: {
keepCasing?: boolean;
};
}

Expand Down Expand Up @@ -47,8 +47,8 @@ export const defaultConfig: TranslocoConfig = {
aot: false,
},
interpolation: ['{{', '}}'],
scope: {
keepNamespaceCasing: false,
scopes: {
keepCasing: false,
},
};

Expand All @@ -75,9 +75,9 @@ export function translocoConfig(
...defaultConfig.flatten,
...config.flatten,
},
scope: {
...defaultConfig.scope,
...config.scope,
scopes: {
...defaultConfig.scopes,
...config.scopes,
},
};
}
5 changes: 2 additions & 3 deletions libs/transloco/src/lib/transloco.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -789,10 +789,9 @@ export class TranslocoService implements OnDestroy {
}

private getMappedScope(scope: string): string {
const { scopeMapping = {} } = this.config;
const { scopeMapping = {}, scopes = { keepCasing: false } } = this.config;
return (
scopeMapping[scope] ||
(this.config.scope.keepNamespaceCasing ? scope : toCamelCase(scope))
scopeMapping[scope] || (scopes.keepCasing ? scope : toCamelCase(scope))
);
}

Expand Down

0 comments on commit 73777bf

Please sign in to comment.