Skip to content

Commit a337c76

Browse files
committed
Add ability to dynamically add translation asset paths
1 parent ff8ea6e commit a337c76

File tree

3 files changed

+60
-4
lines changed

3 files changed

+60
-4
lines changed

packages/ng-lazy-translate/README.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,22 @@ Lazy loaded translation module for Angular using [messageformat](https://message
1212
[![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=bameyrick_ng-lazy-translate&metric=security_rating)](https://sonarcloud.io/summary/new_code?id=bameyrick_ng-lazy-translate)
1313
[![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=bameyrick_ng-lazy-translate&metric=sqale_rating)](https://sonarcloud.io/summary/new_code?id=bameyrick_ng-lazy-translate)
1414

15+
- [@qntm-code/ng-lazy-translate](#qntm-codeng-lazy-translate)
16+
- [Installation](#installation)
17+
- [Usage - Standalone Components](#usage---standalone-components)
18+
- [1. Create providers](#1-create-providers)
19+
- [Usage - Within a Module](#usage---within-a-module)
20+
- [1. Import Module](#1-import-module)
21+
- [2. Import module in component](#2-import-module-in-component)
22+
- [Using pipe in a template](#using-pipe-in-a-template)
23+
- [Using the service in a component/service](#using-the-service-in-a-componentservice)
24+
- [Dynamically adding translation asset paths](#dynamically-adding-translation-asset-paths)
25+
- [LazyTranslateModuleConfig](#lazytranslatemoduleconfig)
26+
- [Language](#language)
27+
- [TranslationAssetPaths](#translationassetpaths)
28+
- [Translation files](#translation-files)
29+
- [Default value](#default-value)
30+
1531
## Installation
1632

1733
```bash
@@ -134,6 +150,31 @@ export class MyService {
134150
}
135151
```
136152

153+
## Dynamically adding translation asset paths
154+
155+
You can dynamically add translation asset paths by calling the `addTranslationAssetPaths` method on the `LazyTranslateService`:
156+
157+
```typescript
158+
import { LazyTranslateService } from '@qntm-code/ng-lazy-translate';
159+
160+
export const appRoutes: Route[] = [
161+
{
162+
path: '',
163+
loadChildren: () =>
164+
import('./home/home.module').then(({ HomeModule }) => {
165+
const translateService = inject(LazyTranslateService);
166+
167+
translateService.addTranslationAssetPaths({
168+
'en.home': 'assets/i18n/en/home.json',
169+
'fr.home': 'assets/i18n/fr/home.json',
170+
});
171+
172+
return HomeModule;
173+
}),
174+
},
175+
];
176+
```
177+
137178
## LazyTranslateModuleConfig
138179

139180
Whether you use the standalone components or the module, the LazyTranslateModuleConfig options are the same.
@@ -142,7 +183,7 @@ Whether you use the standalone components or the module, the LazyTranslateModule
142183
| ------------------------- | ----------------------------------------------- | ----------------------------------------------------------------------------------------------------------------- | --------- | ---------------------------- |
143184
| defaultLanguage | string | The default language to use if no language is specified | Yes | N/A |
144185
| languages | [Language[]](#language) | The list of languages to support | Yes | N/A |
145-
| translationAssetPaths | [TranslationAssetPaths](#translationassetpaths) | The list of translation assets to load. The key is the language and the translation file name. | Yes | N/A |
186+
| translationAssetPaths | [TranslationAssetPaths](#translationassetpaths) | The list of translation assets to load. The key is the language and the translation file name. | No | N/A |
146187
| useDefaultLanguage | boolean | Whether to use the default language if the specified language is not found | No | `true` |
147188
| enableLogging | boolean | Whether to enable logging of missing translations | No | `true` |
148189
| missingTranslationHandler | (language: string, key: string) => void | A custom handler to use when a translation is not found. If not specified, the default handler will be used. | No | Will console.error a message |

packages/ng-lazy-translate/src/lib/models/translate-module-config.model.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { TranslationAssetPaths } from './translation-asset-paths.model';
66
export interface LazyTranslateModuleConfig {
77
languages: Language[];
88
defaultLanguage: string;
9-
translationAssetPaths: TranslationAssetPaths;
9+
translationAssetPaths?: TranslationAssetPaths;
1010
useDefaultLanguage?: boolean;
1111
enableLogging?: boolean;
1212
missingTranslationHandler?: MissingTranslationHandlerFn;

packages/ng-lazy-translate/src/lib/translate.service.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
shareReplay,
2222
withLatestFrom,
2323
} from 'rxjs';
24-
import { LazyTranslateModuleConfig } from './models';
24+
import { LazyTranslateModuleConfig, TranslationAssetPaths } from './models';
2525
import { NG_LAZY_TRANSLATE_CONFIG } from './tokens';
2626

2727
@Injectable()
@@ -51,9 +51,15 @@ export class LazyTranslateService {
5151
*/
5252
private readonly config: LazyTranslateModuleConfig;
5353

54+
private readonly translationAssetPaths = new Map<string, string>();
55+
5456
constructor(@Inject(NG_LAZY_TRANSLATE_CONFIG) config: LazyTranslateModuleConfig, private readonly http: HttpClient) {
5557
this.config = { useDefaultLanguage: true, enableLogging: true, ...config };
5658

59+
if (this.config.translationAssetPaths) {
60+
this.addTranslationPaths(this.config.translationAssetPaths);
61+
}
62+
5763
this.defaultLanguage$ = new BehaviorSubject<string>(this.config.defaultLanguage);
5864

5965
this.language$ = new BehaviorSubject<string>(
@@ -73,6 +79,13 @@ export class LazyTranslateService {
7379
}
7480
}
7581

82+
/**
83+
* Adds translation asset paths
84+
*/
85+
public addTranslationPaths(paths: TranslationAssetPaths): void {
86+
Object.entries(paths).forEach(([key, value]) => this.translationAssetPaths.set(key, value));
87+
}
88+
7689
public setLanguage(language: string): void {
7790
this.language$.next(this.getValidLanguageCode(language));
7891
}
@@ -167,14 +180,16 @@ export class LazyTranslateService {
167180
* Downloads a language file for a given namespace
168181
*/
169182
private downloadFile(language: string, namespace: string): Observable<Record<string, unknown> | undefined> {
170-
const path = this.config.translationAssetPaths[`${language}.${namespace}`];
183+
const path = this.translationAssetPaths.get(`${language}.${namespace}`);
171184

172185
if (!path) {
173186
if (this.config.missingFileHandler) {
174187
this.config.missingFileHandler(namespace, language);
175188
} else if (this.config.enableLogging) {
176189
console.error(`File with namespace ${namespace} not found for language ${language}`);
177190
}
191+
192+
return of(undefined);
178193
}
179194

180195
let observable = this.downloadedRequests[path];

0 commit comments

Comments
 (0)