Implemented i18n cache busting

This commit is contained in:
Alexandre Vryghem
2023-08-26 22:02:33 +02:00
parent 404ccd9b0e
commit 07a2e333ca
5 changed files with 53 additions and 14 deletions

View File

@@ -5,6 +5,7 @@ import { NGX_TRANSLATE_STATE, NgxTranslateState } from './ngx-translate-state';
import { hasValue } from '../app/shared/empty.util';
import { map } from 'rxjs/operators';
import { of as observableOf, Observable } from 'rxjs';
import { environment } from '../environments/environment';
/**
* A TranslateLoader for ngx-translate to retrieve i18n messages from the TransferState, or download
@@ -33,9 +34,10 @@ export class TranslateBrowserLoader implements TranslateLoader {
if (hasValue(messages)) {
return observableOf(messages);
} else {
const translationHash: string = environment.production ? `.${(process.env.languageHashes as any)[lang + '.json5']}` : '';
// If they're not available on the transfer state (e.g. when running in dev mode), retrieve
// them using HttpClient
return this.http.get('' + this.prefix + lang + this.suffix, { responseType: 'text' }).pipe(
return this.http.get(`${this.prefix}${lang}${translationHash}${this.suffix}`, { responseType: 'text' }).pipe(
map((json: any) => JSON.parse(json))
);
}

View File

@@ -23,8 +23,9 @@ export class TranslateServerLoader implements TranslateLoader {
* @param lang the language code
*/
public getTranslation(lang: string): Observable<any> {
const translationHash: string = (process.env.languageHashes as any)[lang + '.json5'];
// Retrieve the file for the given language, and parse it
const messages = JSON.parse(readFileSync(`${this.prefix}${lang}${this.suffix}`, 'utf8'));
const messages = JSON.parse(readFileSync(`${this.prefix}${lang}.${translationHash}${this.suffix}`, 'utf8'));
// Store the parsed messages in the transfer state so they'll be available immediately when the
// app loads on the client
this.storeInTransferState(lang, messages);