From 23e7bce39501e2434271c34f2cd53eb670fffbb0 Mon Sep 17 00:00:00 2001 From: GoWale Date: Sun, 11 Feb 2024 23:06:13 +0100 Subject: [PATCH 1/5] feat: better local --- src/index.ts | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index 5902534..769b797 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,21 @@ export class cds_launchpad_plugin{ i18nPath += i18nsetting; } - if (options.locale) { - i18nPath = i18nPath.replace(/(\.properties)$/,`_${options.locale}$1`); + // define local to use + const local = [] + + // check for existing files + if(options.locale && local.indexOf(options.locale) < 0 && fs.existsSync(i18nPath.replace(/(\.properties)$/, `_${options.locale}$1`))) local.push(i18nPath.replace(/(\.properties)$/, `_${options.locale}$1`)) + if(cds.user?.local && local.indexOf(cds.user?.local) < 0 && fs.existsSync(i18nPath.replace(/(\.properties)$/, `_${cds.user?.local}$1`))) local.push(i18nPath.replace(/(\.properties)$/, `_${cds.user?.local}$1`)) // would be great but only exists after CAP logic.. + if(req_locale.default(request) && local.indexOf(req_locale.default(request)) < 0 && fs.existsSync(i18nPath.replace(/(\.properties)$/, `_${cds.user?.local}$1`))) local.push(i18nPath.replace(/(\.properties)$/, `_${req_locale.default(request)}$1`)) + if(cds.env.i18n.default_language && local.indexOf(cds.env.i18n.default_language) < 0 && fs.existsSync(i18nPath.replace(/(\.properties)$/, `_${cds.user?.local}$1`))) local.push(i18nPath.replace(/(\.properties)$/, `_${cds.env.i18n.default_language}$1`)) + if(local.indexOf('') < 0 && fs.existsSync(i18nPath)) local.push(i18nPath) // allow fallback i18n + + // use langu from settings options + if (local.length > 0){ + i18nPath = local[0] + } else { + return cdsLaunchpadLogger.error(`i18n file not found: ${i18nPath}`) } const i18n = parse(fs.readFileSync( i18nPath ).toString()); const tileconfigs = manifest["sap.app"]?.crossNavigation?.inbounds; From c697c0448bc272848217437715e2efc57988f8b5 Mon Sep 17 00:00:00 2001 From: GoWale Date: Sun, 11 Feb 2024 23:15:26 +0100 Subject: [PATCH 2/5] Update index.ts --- src/index.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/index.ts b/src/index.ts index 769b797..9a0c48d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -192,14 +192,14 @@ export class cds_launchpad_plugin{ } // define local to use - const local = [] + const local = null; // check for existing files - if(options.locale && local.indexOf(options.locale) < 0 && fs.existsSync(i18nPath.replace(/(\.properties)$/, `_${options.locale}$1`))) local.push(i18nPath.replace(/(\.properties)$/, `_${options.locale}$1`)) - if(cds.user?.local && local.indexOf(cds.user?.local) < 0 && fs.existsSync(i18nPath.replace(/(\.properties)$/, `_${cds.user?.local}$1`))) local.push(i18nPath.replace(/(\.properties)$/, `_${cds.user?.local}$1`)) // would be great but only exists after CAP logic.. - if(req_locale.default(request) && local.indexOf(req_locale.default(request)) < 0 && fs.existsSync(i18nPath.replace(/(\.properties)$/, `_${cds.user?.local}$1`))) local.push(i18nPath.replace(/(\.properties)$/, `_${req_locale.default(request)}$1`)) - if(cds.env.i18n.default_language && local.indexOf(cds.env.i18n.default_language) < 0 && fs.existsSync(i18nPath.replace(/(\.properties)$/, `_${cds.user?.local}$1`))) local.push(i18nPath.replace(/(\.properties)$/, `_${cds.env.i18n.default_language}$1`)) - if(local.indexOf('') < 0 && fs.existsSync(i18nPath)) local.push(i18nPath) // allow fallback i18n + if(!local && options.locale && fs.existsSync(i18nPath.replace(/(\.properties)$/, `_${options.locale}$1`))) local = i18nPath.replace(/(\.properties)$/, `_${options.locale}$1`) + if(!local && cds.user?.local && fs.existsSync(i18nPath.replace(/(\.properties)$/, `_${cds.user?.local}$1`))) local = i18nPath.replace(/(\.properties)$/, `_${cds.user?.local}$1`) // would be great but only exists after CAP logic.. + if(!local && req_locale.default(request) && fs.existsSync(i18nPath.replace(/(\.properties)$/, `_${cds.user?.local}$1`))) local = i18nPath.replace(/(\.properties)$/, `_${req_locale.default(request)}$1`) + if(!local && cds.env.i18n.default_language && fs.existsSync(i18nPath.replace(/(\.properties)$/, `_${cds.user?.local}$1`))) local = i18nPath.replace(/(\.properties)$/, `_${cds.env.i18n.default_language}$1`) + if(!local && fs.existsSync(i18nPath)) local = i18nPath // allow fallback i18n // use langu from settings options if (local.length > 0){ From 96b7d5856551088d131d643f7e413590d9b15fa4 Mon Sep 17 00:00:00 2001 From: GoWale Date: Sun, 11 Feb 2024 23:23:27 +0100 Subject: [PATCH 3/5] typo --- src/index.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/index.ts b/src/index.ts index 9a0c48d..f8a35dd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -191,19 +191,19 @@ export class cds_launchpad_plugin{ i18nPath += i18nsetting; } - // define local to use - const local = null; + // define locale to use + const locale = null; // check for existing files - if(!local && options.locale && fs.existsSync(i18nPath.replace(/(\.properties)$/, `_${options.locale}$1`))) local = i18nPath.replace(/(\.properties)$/, `_${options.locale}$1`) - if(!local && cds.user?.local && fs.existsSync(i18nPath.replace(/(\.properties)$/, `_${cds.user?.local}$1`))) local = i18nPath.replace(/(\.properties)$/, `_${cds.user?.local}$1`) // would be great but only exists after CAP logic.. - if(!local && req_locale.default(request) && fs.existsSync(i18nPath.replace(/(\.properties)$/, `_${cds.user?.local}$1`))) local = i18nPath.replace(/(\.properties)$/, `_${req_locale.default(request)}$1`) - if(!local && cds.env.i18n.default_language && fs.existsSync(i18nPath.replace(/(\.properties)$/, `_${cds.user?.local}$1`))) local = i18nPath.replace(/(\.properties)$/, `_${cds.env.i18n.default_language}$1`) - if(!local && fs.existsSync(i18nPath)) local = i18nPath // allow fallback i18n + if(!locale && options.locale && fs.existsSync(i18nPath.replace(/(\.properties)$/, `_${options.locale}$1`))) local = i18nPath.replace(/(\.properties)$/, `_${options.locale}$1`) + // https://cap.cloud.sap/docs/node.js/events#locale => req.locale or old (cds.user.locale) + if(!locale && req_locale.default(request) && fs.existsSync(i18nPath.replace(/(\.properties)$/, `_${cds.user?.local}$1`))) locale = i18nPath.replace(/(\.properties)$/, `_${req_locale.default(request)}$1`) + if(!locale && cds.env.i18n.default_language && fs.existsSync(i18nPath.replace(/(\.properties)$/, `_${cds.user?.local}$1`))) locale = i18nPath.replace(/(\.properties)$/, `_${cds.env.i18n.default_language}$1`) + if(!locale && fs.existsSync(i18nPath)) locale = i18nPath // allow fallback i18n // use langu from settings options - if (local.length > 0){ - i18nPath = local[0] + if (locale){ + i18nPath = locale } else { return cdsLaunchpadLogger.error(`i18n file not found: ${i18nPath}`) } From 72ed7ff5dcbe166f59931b8aa6fcc43c342b934a Mon Sep 17 00:00:00 2001 From: GoWale Date: Sun, 11 Feb 2024 23:25:50 +0100 Subject: [PATCH 4/5] update tsc --- src/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index f8a35dd..b65c8c6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -192,10 +192,10 @@ export class cds_launchpad_plugin{ } // define locale to use - const locale = null; + let locale = null; // check for existing files - if(!locale && options.locale && fs.existsSync(i18nPath.replace(/(\.properties)$/, `_${options.locale}$1`))) local = i18nPath.replace(/(\.properties)$/, `_${options.locale}$1`) + if(!locale && options.locale && fs.existsSync(i18nPath.replace(/(\.properties)$/, `_${options.locale}$1`))) locale = i18nPath.replace(/(\.properties)$/, `_${options.locale}$1`) // https://cap.cloud.sap/docs/node.js/events#locale => req.locale or old (cds.user.locale) if(!locale && req_locale.default(request) && fs.existsSync(i18nPath.replace(/(\.properties)$/, `_${cds.user?.local}$1`))) locale = i18nPath.replace(/(\.properties)$/, `_${req_locale.default(request)}$1`) if(!locale && cds.env.i18n.default_language && fs.existsSync(i18nPath.replace(/(\.properties)$/, `_${cds.user?.local}$1`))) locale = i18nPath.replace(/(\.properties)$/, `_${cds.env.i18n.default_language}$1`) From 88c46b1c8d080e78dff8459d71b5b274c565201f Mon Sep 17 00:00:00 2001 From: GoWale Date: Tue, 13 Feb 2024 02:44:39 +0100 Subject: [PATCH 5/5] fix: typo & cleanup --- src/index.ts | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index b65c8c6..9952613 100644 --- a/src/index.ts +++ b/src/index.ts @@ -195,11 +195,10 @@ export class cds_launchpad_plugin{ let locale = null; // check for existing files - if(!locale && options.locale && fs.existsSync(i18nPath.replace(/(\.properties)$/, `_${options.locale}$1`))) locale = i18nPath.replace(/(\.properties)$/, `_${options.locale}$1`) - // https://cap.cloud.sap/docs/node.js/events#locale => req.locale or old (cds.user.locale) - if(!locale && req_locale.default(request) && fs.existsSync(i18nPath.replace(/(\.properties)$/, `_${cds.user?.local}$1`))) locale = i18nPath.replace(/(\.properties)$/, `_${req_locale.default(request)}$1`) - if(!locale && cds.env.i18n.default_language && fs.existsSync(i18nPath.replace(/(\.properties)$/, `_${cds.user?.local}$1`))) locale = i18nPath.replace(/(\.properties)$/, `_${cds.env.i18n.default_language}$1`) - if(!locale && fs.existsSync(i18nPath)) locale = i18nPath // allow fallback i18n + 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){ @@ -268,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; + } }