Skip to content

Commit a7f7935

Browse files
authored
Added missing file handler option (#4)
1 parent e9c7d82 commit a7f7935

File tree

5 files changed

+21
-10
lines changed

5 files changed

+21
-10
lines changed

packages/ng-lazy-translate/README.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ export const appConfig: ApplicationConfig = {
5555
'fr.common': 'asset/i18n/fr/common.json',
5656
},
5757
missingTranslationHandler: (language: string, key: string) => console.error(`Custom handler: Could not find ${key} in ${language}`),
58+
missingFileHandler: (namespace: string, language: string) =>
59+
console.error(`Custom handler: CFile with namespace ${namespace} not found for language ${language}`),
5860
}),
5961
],
6062
};
@@ -136,14 +138,15 @@ export class MyService {
136138

137139
Whether you use the standalone components or the module, the LazyTranslateModuleConfig options are the same.
138140

139-
| Option | Type | Description | Mandatory | Default |
140-
| ------------------------- | ----------------------------------------------- | ------------------------------------------------------------------------------------------------------------ | --------- | ---------------------------- |
141-
| defaultLanguage | string | The default language to use if no language is specified | Yes | N/A |
142-
| languages | [Language[]](#language) | The list of languages to support | Yes | N/A |
143-
| translationAssetPaths | [TranslationAssetPaths](#translationassetpaths) | The list of translation assets to load. The key is the language and the translation file name. | Yes | N/A |
144-
| useDefaultLanguage | boolean | Whether to use the default language if the specified language is not found | No | `true` |
145-
| enableLogging | boolean | Whether to enable logging of missing translations | No | `true` |
146-
| 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 |
141+
| Option | Type | Description | Mandatory | Default |
142+
| ------------------------- | ----------------------------------------------- | ----------------------------------------------------------------------------------------------------------------- | --------- | ---------------------------- |
143+
| defaultLanguage | string | The default language to use if no language is specified | Yes | N/A |
144+
| 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 |
146+
| useDefaultLanguage | boolean | Whether to use the default language if the specified language is not found | No | `true` |
147+
| enableLogging | boolean | Whether to enable logging of missing translations | No | `true` |
148+
| 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 |
149+
| missingFileHandler | (namespace: string, language: string) => void | A custom handler to use when a translation file is not found. If not specified, the default handler will be used. | No | Will console.error a message |
147150

148151
## Language
149152

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './language.model';
2+
export * from './missing-file-handler-fn.model';
23
export * from './translate-module-config.model';
34
export * from './translation-asset-paths.model';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type MissingFileHandlerFn = (namespace: string, language: string) => void;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { MissingTranslationHandlerFn } from '@qntm-code/translation-key-store';
22
import { Language } from './language.model';
3+
import { MissingFileHandlerFn } from './missing-file-handler-fn.model';
34
import { TranslationAssetPaths } from './translation-asset-paths.model';
45

56
export interface LazyTranslateModuleConfig {
@@ -9,4 +10,5 @@ export interface LazyTranslateModuleConfig {
910
useDefaultLanguage?: boolean;
1011
enableLogging?: boolean;
1112
missingTranslationHandler?: MissingTranslationHandlerFn;
13+
missingFileHandler?: MissingFileHandlerFn;
1214
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,12 @@ export class LazyTranslateService {
167167
private downloadFile(language: string, namespace: string): Observable<Record<string, unknown> | undefined> {
168168
const path = this.config.translationAssetPaths[`${language}.${namespace}`];
169169

170-
if (!path && this.config.enableLogging) {
171-
console.error(`File with namespace ${namespace} not found for language ${language}`);
170+
if (!path) {
171+
if (this.config.missingFileHandler) {
172+
this.config.missingFileHandler(namespace, language);
173+
} else if (this.config.enableLogging) {
174+
console.error(`File with namespace ${namespace} not found for language ${language}`);
175+
}
172176
}
173177

174178
let observable = this.downloadedRequests[path];

0 commit comments

Comments
 (0)