From 4f649bb359d9bdce44a27d16bcaaf12f201ec132 Mon Sep 17 00:00:00 2001 From: Ben Meyrick Date: Thu, 30 Jan 2025 16:35:37 +0000 Subject: [PATCH] Added preload option --- packages/ng-lazy-translate/README.md | 17 +++++++++++++++-- .../lib/models/translate-module-config.model.ts | 1 + .../src/lib/translate.service.spec.ts | 17 +++++++++++++++++ .../src/lib/translate.service.ts | 15 ++++++++++++--- 4 files changed, 45 insertions(+), 5 deletions(-) diff --git a/packages/ng-lazy-translate/README.md b/packages/ng-lazy-translate/README.md index e639012..fcf021e 100644 --- a/packages/ng-lazy-translate/README.md +++ b/packages/ng-lazy-translate/README.md @@ -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'; @@ -162,7 +162,7 @@ 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', }); @@ -170,6 +170,18 @@ export class MyService { } ``` +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. @@ -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 | diff --git a/packages/ng-lazy-translate/src/lib/models/translate-module-config.model.ts b/packages/ng-lazy-translate/src/lib/models/translate-module-config.model.ts index f793a29..1c65337 100644 --- a/packages/ng-lazy-translate/src/lib/models/translate-module-config.model.ts +++ b/packages/ng-lazy-translate/src/lib/models/translate-module-config.model.ts @@ -7,6 +7,7 @@ export interface LazyTranslateModuleConfig { languages: Language[]; defaultLanguage: string; translationAssetPaths?: TranslationAssetPaths; + preload?: boolean; useDefaultLanguage?: boolean; enableLogging?: boolean; missingTranslationHandler?: MissingTranslationHandlerFn; diff --git a/packages/ng-lazy-translate/src/lib/translate.service.spec.ts b/packages/ng-lazy-translate/src/lib/translate.service.spec.ts index 4ae1606..d6c2b6e 100644 --- a/packages/ng-lazy-translate/src/lib/translate.service.spec.ts +++ b/packages/ng-lazy-translate/src/lib/translate.service.spec.ts @@ -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'); + }); + }); }); diff --git a/packages/ng-lazy-translate/src/lib/translate.service.ts b/packages/ng-lazy-translate/src/lib/translate.service.ts index f6193c5..c3dc13f 100644 --- a/packages/ng-lazy-translate/src/lib/translate.service.ts +++ b/packages/ng-lazy-translate/src/lib/translate.service.ts @@ -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(this.config.defaultLanguage); @@ -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 {