From 57a6e98df10bfcfc14badd3b4a399815e3c4349a Mon Sep 17 00:00:00 2001 From: Kieran Brownlees Date: Tue, 22 Mar 2022 14:12:19 +1300 Subject: [PATCH] feat(app): add i18n support --- docker-compose.yml | 1 + docs/install-server.md | 1 + src/app.ts | 18 ++++++++++++++++++ src/core/config.ts | 1 + src/core/i81n.ts | 11 +++++++++++ 5 files changed, 32 insertions(+) create mode 100644 src/core/i81n.ts diff --git a/docker-compose.yml b/docker-compose.yml index d97b720f..06f78f42 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -16,6 +16,7 @@ services: DATA_DIR: '/data/tmp' NODE_ENV: 'production' REDIS_HOST: 'redis' + LOCALE: 'cn' ports: - '3000:3000' depends_on: diff --git a/docs/install-server.md b/docs/install-server.md index 43a7de13..42e0198b 100644 --- a/docs/install-server.md +++ b/docs/install-server.md @@ -47,6 +47,7 @@ some config items have to be changed: - `local`.`storageDir` change to your directory, make sure have read/write permissions. - `local`.`downloadUrl` replace `127.0.0.1` to your machine ip. +- `local`.`locale` either `cn` or `en`. - `common`.`dataDir` change to your directory,make sure have read/write permissions. - `jwt`.`tokenSecret` get the random string from `https://www.grc.com/passwords.htm`, and replace the value `INSERT_RANDOM_TOKEN_KEY`. - `db` config: `username`,`password`,`host`,`port` change to your own diff --git a/src/app.ts b/src/app.ts index db0818ae..ff1bf361 100644 --- a/src/app.ts +++ b/src/app.ts @@ -7,6 +7,7 @@ import helmet from 'helmet'; import { logger } from 'kv-logger'; import { AppError, NotFound } from './core/app-error'; import { config } from './core/config'; +import { i18n } from "./core/i81n" import { Req, Res, withLogger } from './core/middleware'; import { accessKeysRouter } from './routes/accessKeys'; import { accountRouter } from './routes/account'; @@ -23,10 +24,27 @@ app.use( contentSecurityPolicy: false, }), ); + + + // view engine setup app.set('views', path.join(__dirname, '../views')); app.set('view engine', 'pug'); +// translations + +app.use(i18n.init) + +app.use(function(req, res, next) { + // express helper for natively supported engines + // @ts-ignore + res.locals.__ = res.__ = function() { + return i18n.__.apply(req, arguments); + }; + + next(); +}); + app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: false })); app.use(cookieParser()); diff --git a/src/core/config.ts b/src/core/config.ts index dacae058..ebc4b33b 100644 --- a/src/core/config.ts +++ b/src/core/config.ts @@ -85,6 +85,7 @@ export const config = { 'http://127.0.0.1:3000/download', // public static download spacename. public: '/download', + locale: process.env.LOCALE || 'cn' }, jwt: { // Recommended: 63 random alpha-numeric characters diff --git a/src/core/i81n.ts b/src/core/i81n.ts new file mode 100644 index 00000000..8aee6450 --- /dev/null +++ b/src/core/i81n.ts @@ -0,0 +1,11 @@ +import { I18n } from 'i18n' +import path from "path" +import { config } from "./config" + +export const i18n = new I18n({ + locales: ['cn', 'en'], + directory: path.join(__dirname, '../../locales'), + defaultLocale: 'cn' +}) + +i18n.setLocale(config.local.locale)