Skip to content

Commit

Permalink
feat(app): add i18n support
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrownlees committed Mar 22, 2022
1 parent fe52691 commit 57a6e98
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ services:
DATA_DIR: '/data/tmp'
NODE_ENV: 'production'
REDIS_HOST: 'redis'
LOCALE: 'cn'
ports:
- '3000:3000'
depends_on:
Expand Down
1 change: 1 addition & 0 deletions docs/install-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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());
Expand Down
1 change: 1 addition & 0 deletions src/core/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions src/core/i81n.ts
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 57a6e98

Please sign in to comment.