Skip to content

Commit

Permalink
Added preload option (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
bameyrick authored Jan 30, 2025
1 parent 64b5e87 commit 1be87e7
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
17 changes: 15 additions & 2 deletions packages/ng-lazy-translate/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export class MyService {

## Dynamically adding translation asset paths

You can dynamically add translation asset paths by calling the `addTranslationAssetPaths` method on the `LazyTranslateService`:
You can dynamically add translation asset paths by calling the `addTranslationPaths` method on the `LazyTranslateService`:

```typescript
import { LazyTranslateService } from '@qntm-code/ng-lazy-translate';
Expand All @@ -162,14 +162,26 @@ export class MyService {
private readonly translateService = inject(LazyTranslateService);

constructor() {
this.translateService.addTranslationAssetPaths({
this.translateService.addTranslationPaths({
'en.home': 'assets/i18n/en/home.json',
'fr.home': 'assets/i18n/fr/home.json',
});
}
}
```

You can also preload all the translation files in the current language by adding the `preload` paramater to the call:

```typescript
this.translateService.addTranslationPaths(
{
'en.home': 'assets/i18n/en/home.json',
'fr.home': 'assets/i18n/fr/home.json',
},
true
);
```

## LazyTranslateModuleConfig

Whether you use the standalone components or the module, the LazyTranslateModuleConfig options are the same.
Expand All @@ -179,6 +191,7 @@ Whether you use the standalone components or the module, the LazyTranslateModule
| defaultLanguage | string | The default language to use if no language is specified | Yes | N/A |
| languages | [Language[]](#language) | The list of languages to support | Yes | N/A |
| translationAssetPaths | [TranslationAssetPaths](#translationassetpaths) | The list of translation assets to load. The key is the language and the translation file name. | No | N/A |
| preload | boolean | Whether to preload all the translation files in the current language. | No | `false` |
| useDefaultLanguage | boolean | Whether to use the default language if the specified language is not found | No | `true` |
| enableLogging | boolean | Whether to enable logging of missing translations | No | `true` |
| 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 |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface LazyTranslateModuleConfig {
languages: Language[];
defaultLanguage: string;
translationAssetPaths?: TranslationAssetPaths;
preload?: boolean;
useDefaultLanguage?: boolean;
enableLogging?: boolean;
missingTranslationHandler?: MissingTranslationHandlerFn;
Expand Down
17 changes: 17 additions & 0 deletions packages/ng-lazy-translate/src/lib/translate.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,21 @@ describe(`LazyTranslateService`, () => {
expect(result).toEqual({ test: 'test' });
});
});

describe('addTranslationPaths', () => {
it('should preload the translations if preload is true', () => {
const downloadFileSpy = jest.spyOn(service as never, 'downloadFile');

service.addTranslationPaths(
{
'en.common': 'assets/i18n/en/common.json',
'cy.common': 'assets/i18n/cy/common.json',
},
true
);

expect(downloadFileSpy).toHaveBeenCalledWith('en', 'common');
expect(downloadFileSpy).not.toHaveBeenCalledWith('cy', 'common');
});
});
});
15 changes: 12 additions & 3 deletions packages/ng-lazy-translate/src/lib/translate.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class LazyTranslateService {
this.config = { useDefaultLanguage: true, enableLogging: true, ...config };

if (this.config.translationAssetPaths) {
this.addTranslationPaths(this.config.translationAssetPaths);
this.addTranslationPaths(this.config.translationAssetPaths, this.config.preload);
}

this.defaultLanguage$ = new BehaviorSubject<string>(this.config.defaultLanguage);
Expand All @@ -82,8 +82,17 @@ export class LazyTranslateService {
/**
* Adds translation asset paths
*/
public addTranslationPaths(paths: TranslationAssetPaths): void {
Object.entries(paths).forEach(([key, value]) => this.translationAssetPaths.set(key, value));
public addTranslationPaths(paths: TranslationAssetPaths, preload?: boolean): void {
Object.entries(paths).forEach(([key, value]) => {
this.translationAssetPaths.set(key, value);
if (preload) {
const [language, namespace] = key.split('.');

if (language === this.language$.getValue()) {
this.downloadFile(language, namespace);
}
}
});
}

public setLanguage(language: string): void {
Expand Down

0 comments on commit 1be87e7

Please sign in to comment.