From 90e34e0104d1413e4573332993ea61bbe813e58d Mon Sep 17 00:00:00 2001 From: AllForNothing Date: Wed, 6 May 2020 11:23:29 +0800 Subject: [PATCH] Improve i18n service Signed-off-by: AllForNothing --- make/photon/portal/Dockerfile | 1 + src/portal/package.json | 3 ++- .../scripts/generate-build-timestamp.js | 19 +++++++++++++++++++ .../src/environments/environment.prod.ts | 3 ++- src/portal/src/environments/environment.ts | 3 ++- .../src/lib/utils/shared/shared.module.ts | 6 +++++- 6 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 src/portal/scripts/generate-build-timestamp.js diff --git a/make/photon/portal/Dockerfile b/make/photon/portal/Dockerfile index abbb7d76b..c65746652 100644 --- a/make/photon/portal/Dockerfile +++ b/make/photon/portal/Dockerfile @@ -25,6 +25,7 @@ COPY src/portal /build_dir ENV NPM_CONFIG_REGISTRY=${npm_registry} RUN npm install --unsafe-perm +RUN npm run generate-build-timestamp RUN node --max_old_space_size=2048 'node_modules/@angular/cli/bin/ng' build --prod FROM ${harbor_base_namespace}/harbor-portal-base:${harbor_base_image_version} diff --git a/src/portal/package.json b/src/portal/package.json index 46539b76f..c07040d23 100644 --- a/src/portal/package.json +++ b/src/portal/package.json @@ -19,7 +19,8 @@ "build": "ng build --aot", "release": "ng build --prod", "build-mock-api-server": "tsc -p server", - "mock-api-server": "npm run build-mock-api-server && node server/dist/server/src/mock-api.js" + "mock-api-server": "npm run build-mock-api-server && node server/dist/server/src/mock-api.js", + "generate-build-timestamp": "node scripts/generate-build-timestamp.js" }, "private": true, "dependencies": { diff --git a/src/portal/scripts/generate-build-timestamp.js b/src/portal/scripts/generate-build-timestamp.js new file mode 100644 index 000000000..a3fcbf035 --- /dev/null +++ b/src/portal/scripts/generate-build-timestamp.js @@ -0,0 +1,19 @@ +/** + * generate timestamp for each production build + */ +const fs = require('fs'); +const data = fs.readFileSync('src/environments/environment.prod.ts', 'utf8').split('\n'); + +let buildTimestampIndex = 0; +data.forEach((item,index) => { + if(item.indexOf('buildTimestamp') !== -1) { + buildTimestampIndex = index; + } +}); +if (buildTimestampIndex > 0) { + const timestamp = new Date().getTime(); + data[buildTimestampIndex] = ` buildTimestamp: ${timestamp},`; + fs.writeFileSync('src/environments/environment.prod.ts', data.join('\n'), 'utf8'); +} + + diff --git a/src/portal/src/environments/environment.prod.ts b/src/portal/src/environments/environment.prod.ts index 8362ec0d2..83a5ce91e 100644 --- a/src/portal/src/environments/environment.prod.ts +++ b/src/portal/src/environments/environment.prod.ts @@ -12,5 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. export const environment = { - production: true + production: true, + buildTimestamp: 0, }; diff --git a/src/portal/src/environments/environment.ts b/src/portal/src/environments/environment.ts index 51dfd4f8f..39dd10098 100644 --- a/src/portal/src/environments/environment.ts +++ b/src/portal/src/environments/environment.ts @@ -17,5 +17,6 @@ // The list of which env maps to which file can be found in `angular-cli.json`. export const environment = { - production: false + production: false, + buildTimestamp: 0, }; diff --git a/src/portal/src/lib/utils/shared/shared.module.ts b/src/portal/src/lib/utils/shared/shared.module.ts index 1d83abb30..0d8990574 100644 --- a/src/portal/src/lib/utils/shared/shared.module.ts +++ b/src/portal/src/lib/utils/shared/shared.module.ts @@ -11,11 +11,15 @@ import { TranslateHttpLoader } from "@ngx-translate/http-loader"; import { MyMissingTranslationHandler } from "../../i18n/missing-trans.handler"; import { TranslatorJsonLoader } from "../../i18n/local-json.loader"; import { ClipboardModule } from "../../components/third-party/ngx-clipboard"; +import { environment } from '../../../environments/environment'; export function GeneralTranslatorLoader(http: HttpClient, config: IServiceConfig) { if (config && config.langMessageLoader === 'http') { - let prefix: string = config.langMessagePathForHttpLoader ? config.langMessagePathForHttpLoader : "i18n/lang/"; + const prefix: string = config.langMessagePathForHttpLoader ? config.langMessagePathForHttpLoader : "i18n/lang/"; let suffix: string = config.langMessageFileSuffixForHttpLoader ? config.langMessageFileSuffixForHttpLoader : "-lang.json"; + if (environment && environment.buildTimestamp) { + suffix += `?buildTimeStamp=${environment.buildTimestamp}`; + } return new TranslateHttpLoader(http, prefix, suffix); } else { return new TranslatorJsonLoader(config);