Skip to content

Commit b40dbe7

Browse files
WHeirstrateWouter Heirstrate
andauthored
feat(ngx-i18n): add the option to cache bust the translation files using the root config (#415)
Co-authored-by: Wouter Heirstrate <[email protected]>
1 parent ebd7901 commit b40dbe7

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

apps/i18n-test/src/main.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ bootstrapApplication(AppComponent, {
1111
importNgxI18nProviders({
1212
defaultLanguage: 'nl',
1313
defaultAssetPaths: ['./assets/shared/'],
14+
cacheBust: '1.0.0',
1415
}),
1516
provideHttpClient(withInterceptorsFromDi()),
1617
],

libs/angular/i18n/src/lib/i18n.types.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,18 @@ export interface NgxI18nConfiguration {
33
availableLanguages?: string[];
44
defaultAssetPaths: string[];
55
languageRouteParam?: string;
6+
/**
7+
* The cache busting parameter to append to the translation file requests.
8+
* This can be useful when you want to force the browser to fetch the latest
9+
* version of the translation files by appending this value to the `v` query
10+
* parameter.
11+
*
12+
* When not provided, no query parameter will be added to the request.
13+
*
14+
* @example
15+
* `?v=1` when `cacheBust: '' + 1`
16+
* `?v=1.0.0` when `cacheBust: '1.0.0'`
17+
* `?v=1743428073628` when `cacheBust: String(Date.now())`
18+
*/
19+
cacheBust?: string;
620
}

libs/angular/i18n/src/lib/loader/multi-translation/multi-translation.loader.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,18 @@ import { forkJoin, Observable, of } from 'rxjs';
66
import { catchError, map, tap } from 'rxjs/operators';
77

88
import { NgxI18nLoadingService } from '../../services';
9+
import { NgxI18nConfiguration } from '../../i18n.types';
10+
import { NgxI18nConfigurationToken } from '../../tokens';
911

1012
export class NgxI18nMultiTranslationHttpLoader implements TranslateLoader {
1113
private readonly translationLoadingService: NgxI18nLoadingService =
1214
inject(NgxI18nLoadingService);
1315

16+
/**
17+
* The configuration for the NgxI18nModule.
18+
*/
19+
private readonly config: NgxI18nConfiguration = inject(NgxI18nConfigurationToken);
20+
1421
constructor(
1522
private readonly httpBackend: HttpBackend,
1623
private readonly translationsPaths: string[]
@@ -37,7 +44,9 @@ export class NgxI18nMultiTranslationHttpLoader implements TranslateLoader {
3744
});
3845
} else {
3946
// Iben: If the translations aren't available in the store, we fetch them from the server
40-
const fetchPath = `${path}${lang}.json`;
47+
// Wouter: When provided, add a cache busting param so that each request is fetched from the server instead of the browser cache
48+
const fetchPath = `${path}${lang}.json${this.config.cacheBust ? '?v=' + this.config.cacheBust : ''}`;
49+
4150
return new HttpClient(this.httpBackend).get(fetchPath).pipe(
4251
// Iben: Map this to an object so we can track which results corresponds with which path
4352
map((translations) => {

0 commit comments

Comments
 (0)