diff --git a/src/index.ts b/src/index.ts index 5902534..9952613 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,6 +3,7 @@ import * as fs from 'fs'; import * as path from 'path'; import * as fsAsync from 'fs/promises' import * as appindex from '@sap/cds/app/index' +import * as req_locale from '@sap/cds/lib/req/locale' //import * as cds from '@sap/cds-dk'; import { parse, parseLines, stringify } from 'dot-properties'; const cds = require('@sap/cds-dk'); @@ -43,7 +44,7 @@ export class cds_launchpad_plugin{ // Mount path for launchpad sandbox configuration router.use('/appconfig/fioriSandboxConfig.json', async (request, response, next) => { // debugger; - response.send(await this.prepareAppConfigJSON(options)); + response.send(await this.prepareAppConfigJSON(request, options)); }); // Component preload generation @@ -131,7 +132,7 @@ export class cds_launchpad_plugin{ return { appDirs, cwd, depsPaths }; } - async prepareAppConfigJSON(options: LaunchpadConfig): Promise { + async prepareAppConfigJSON(request: express.Request, options: LaunchpadConfig): Promise { let template = options.template === 'legacy' || options.template === '' || options.template === undefined ? 'legacy' : options.template; // Read app config template @@ -190,8 +191,20 @@ export class cds_launchpad_plugin{ i18nPath += i18nsetting; } - if (options.locale) { - i18nPath = i18nPath.replace(/(\.properties)$/,`_${options.locale}$1`); + // define locale to use + let locale = null; + + // check for existing files + if (!locale) locale = this.checkI18nFile(i18nPath, options.locale); + if (!locale) locale = this.checkI18nFile(i18nPath, req_locale.default(request)); // https://cap.cloud.sap/docs/node.js/events#locale => req.locale or old (cds.user.locale) + if (!locale) locale = this.checkI18nFile(i18nPath, cds.env.i18n.default_language); + if (!locale && fs.existsSync(i18nPath)) locale = i18nPath; // allow fallback i18n + + // use langu from settings options + if (locale){ + i18nPath = locale + } else { + return cdsLaunchpadLogger.error(`i18n file not found: ${i18nPath}`) } const i18n = parse(fs.readFileSync( i18nPath ).toString()); const tileconfigs = manifest["sap.app"]?.crossNavigation?.inbounds; @@ -254,4 +267,14 @@ export class cds_launchpad_plugin{ return config; } + + checkI18nFile(i18nPath: String, locale: string){ + const fileName = i18nPath.replace(/(\.properties)$/, `_${locale}$1`); + if(fs.existsSync(fileName)){ + return fileName; + } else{ + cdsLaunchpadLogger.debug(`could not read i18n file: ${fileName}`) + } + return undefined; + } }