Skip to content

Commit 83d4e0a

Browse files
committed
Setting of the languagehashes to the environment during server startup
1 parent 654224f commit 83d4e0a

1 file changed

Lines changed: 25 additions & 1 deletion

File tree

src/config/config.server.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { red, blue, green, bold } from 'colors';
2-
import { existsSync, readFileSync, writeFileSync } from 'fs';
2+
import { existsSync, readdirSync, readFileSync, statSync, writeFileSync } from 'fs';
33
import { load } from 'js-yaml';
44
import { join } from 'path';
55

@@ -9,6 +9,7 @@ import { DefaultAppConfig } from './default-app-config';
99
import { ServerConfig } from './server-config.interface';
1010
import { mergeConfig } from './config.util';
1111
import { isNotEmpty } from '../app/shared/empty.util';
12+
import { LanguageHashesConfig } from './languageHashes-config.interface';
1213

1314
const CONFIG_PATH = join(process.cwd(), 'config');
1415

@@ -231,6 +232,29 @@ export const buildAppConfig = (destConfigPath?: string): AppConfig => {
231232
// apply build defined production
232233
appConfig.production = env === 'production';
233234

235+
const useDynamicLanguageHashes = isNotEmpty(ENV('DYNAMIC_LANGUAGE_HASHES', true)) ? ENV('DYNAMIC_LANGUAGE_HASHES', true) : false;
236+
if (appConfig.production && useDynamicLanguageHashes) {
237+
const i18nAssetsDir = 'dist/server/assets/i18n/';
238+
const i18nFiles = readdirSync(i18nAssetsDir);
239+
//sort all the i18nFiles by mtime descending
240+
i18nFiles.sort(function(a, b) {
241+
return statSync(i18nAssetsDir + b).mtime.getTime() - statSync(i18nAssetsDir + a).mtime.getTime();
242+
});
243+
244+
i18nFiles.forEach(file => {
245+
const match = file.match(/^(.+)\.(.+)\.json$/);
246+
// only add language hash to config if no config exists for current language (ensuring the latest language file
247+
// is used)
248+
if (match && appConfig.languageHashes.filter(((languageHashConfig) => languageHashConfig.lang === match[1])).length === 0) {
249+
const languageConfig: LanguageHashesConfig = {
250+
lang: match[1],
251+
md5: match[2]
252+
};
253+
appConfig.languageHashes.push(languageConfig);
254+
}
255+
});
256+
}
257+
234258
// build base URLs
235259
buildBaseUrl(appConfig.ui);
236260
buildBaseUrl(appConfig.rest);

0 commit comments

Comments
 (0)