From 2e76ba3cca40da320e3a341f0ba34048fcc6c219 Mon Sep 17 00:00:00 2001 From: Andreas Mahnke Date: Thu, 7 Dec 2023 14:06:28 +0100 Subject: [PATCH] Allow retrieval from languageHashes using environment (#2702) --- src/config/app-config.interface.ts | 2 ++ src/config/default-app-config.ts | 3 +++ src/config/languageHashes-config.interface.ts | 11 +++++++++++ src/ngx-translate-loaders/translate-browser.loader.ts | 6 +++++- src/ngx-translate-loaders/translate-server.loader.ts | 5 ++++- 5 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 src/config/languageHashes-config.interface.ts diff --git a/src/config/app-config.interface.ts b/src/config/app-config.interface.ts index 69aeab7cbf8..620ac0d09fb 100644 --- a/src/config/app-config.interface.ts +++ b/src/config/app-config.interface.ts @@ -22,6 +22,7 @@ import { HomeConfig } from './homepage-config.interface'; import { MarkdownConfig } from './markdown-config.interface'; import { FilterVocabularyConfig } from './filter-vocabulary-config'; import { DiscoverySortConfig } from './discovery-sort.config'; +import { LanguageHashesConfig } from './languageHashes-config.interface'; interface AppConfig extends Config { ui: UIServerConfig; @@ -35,6 +36,7 @@ interface AppConfig extends Config { debug: boolean; defaultLanguage: string; languages: LangConfig[]; + languageHashes: LanguageHashesConfig[]; browseBy: BrowseByConfig; communityList: CommunityListConfig; homePage: HomeConfig; diff --git a/src/config/default-app-config.ts b/src/config/default-app-config.ts index 5eced97ee20..a6cffa82af7 100644 --- a/src/config/default-app-config.ts +++ b/src/config/default-app-config.ts @@ -22,6 +22,7 @@ import { HomeConfig } from './homepage-config.interface'; import { MarkdownConfig } from './markdown-config.interface'; import { FilterVocabularyConfig } from './filter-vocabulary-config'; import { DiscoverySortConfig } from './discovery-sort.config'; +import { LanguageHashesConfig } from './languageHashes-config.interface'; export class DefaultAppConfig implements AppConfig { production = false; @@ -242,6 +243,8 @@ export class DefaultAppConfig implements AppConfig { { code: 'uk', label: 'Yкраї́нська', active: true} ]; + languageHashes: LanguageHashesConfig[] = []; + // Browse-By Pages browseBy: BrowseByConfig = { // Amount of years to display using jumps of one year (current year - oneYearLimit) diff --git a/src/config/languageHashes-config.interface.ts b/src/config/languageHashes-config.interface.ts new file mode 100644 index 00000000000..71c23bae542 --- /dev/null +++ b/src/config/languageHashes-config.interface.ts @@ -0,0 +1,11 @@ +import { Config } from './config.interface'; + +/** + * An interface to represent a language in the configuration. A LangConfig has a code which should be the official + * language code for the language (e.g. ‘fr’), a label which should be the name of the language in that language + * (e.g. ‘Français’), and a boolean to determine whether or not it should be listed in the language select. + */ +export interface LanguageHashesConfig extends Config { + lang: string; + md5: string; +} diff --git a/src/ngx-translate-loaders/translate-browser.loader.ts b/src/ngx-translate-loaders/translate-browser.loader.ts index 85d59c96f0a..1681aac4725 100644 --- a/src/ngx-translate-loaders/translate-browser.loader.ts +++ b/src/ngx-translate-loaders/translate-browser.loader.ts @@ -34,7 +34,11 @@ export class TranslateBrowserLoader implements TranslateLoader { if (hasValue(messages)) { return observableOf(messages); } else { - const translationHash: string = environment.production ? `.${(process.env.languageHashes as any)[lang + '.json5']}` : ''; + let translationHash = ''; + if (environment.production) { + const languageHashesConfigFromEnvironment = environment.languageHashes.filter((languageHashConfig) => languageHashConfig.lang === lang); + translationHash = `.${languageHashesConfigFromEnvironment.length > 0 ? languageHashesConfigFromEnvironment[0].md5 : (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}${translationHash}${this.suffix}`, { responseType: 'text' }).pipe( diff --git a/src/ngx-translate-loaders/translate-server.loader.ts b/src/ngx-translate-loaders/translate-server.loader.ts index 1f47dfe95b5..fcbd7aa800e 100644 --- a/src/ngx-translate-loaders/translate-server.loader.ts +++ b/src/ngx-translate-loaders/translate-server.loader.ts @@ -3,6 +3,7 @@ import { Observable, of as observableOf } from 'rxjs'; import { readFileSync } from 'fs'; import { TransferState } from '@angular/platform-browser'; import { NGX_TRANSLATE_STATE, NgxTranslateState } from './ngx-translate-state'; +import { environment } from '../environments/environment'; /** * A TranslateLoader for ngx-translate to parse json5 files server-side, and store them in the @@ -23,7 +24,9 @@ export class TranslateServerLoader implements TranslateLoader { * @param lang the language code */ public getTranslation(lang: string): Observable { - const translationHash: string = (process.env.languageHashes as any)[lang + '.json5']; + const languageHashesConfigFromEnvironment = environment.languageHashes.filter((languageHashConfig) => languageHashConfig.lang === lang); + const translationHash = `.${languageHashesConfigFromEnvironment.length > 0 ? languageHashesConfigFromEnvironment[0].md5 : (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}.${translationHash}${this.suffix}`, 'utf8')); // Store the parsed messages in the transfer state so they'll be available immediately when the