Skip to content

Commit 57a6e98

Browse files
feat(app): add i18n support
1 parent fe52691 commit 57a6e98

File tree

5 files changed

+32
-0
lines changed

5 files changed

+32
-0
lines changed

docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ services:
1616
DATA_DIR: '/data/tmp'
1717
NODE_ENV: 'production'
1818
REDIS_HOST: 'redis'
19+
LOCALE: 'cn'
1920
ports:
2021
- '3000:3000'
2122
depends_on:

docs/install-server.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ some config items have to be changed:
4747

4848
- `local`.`storageDir` change to your directory, make sure have read/write permissions.
4949
- `local`.`downloadUrl` replace `127.0.0.1` to your machine ip.
50+
- `local`.`locale` either `cn` or `en`.
5051
- `common`.`dataDir` change to your directory,make sure have read/write permissions.
5152
- `jwt`.`tokenSecret` get the random string from `https://www.grc.com/passwords.htm`, and replace the value `INSERT_RANDOM_TOKEN_KEY`.
5253
- `db` config: `username`,`password`,`host`,`port` change to your own

src/app.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import helmet from 'helmet';
77
import { logger } from 'kv-logger';
88
import { AppError, NotFound } from './core/app-error';
99
import { config } from './core/config';
10+
import { i18n } from "./core/i81n"
1011
import { Req, Res, withLogger } from './core/middleware';
1112
import { accessKeysRouter } from './routes/accessKeys';
1213
import { accountRouter } from './routes/account';
@@ -23,10 +24,27 @@ app.use(
2324
contentSecurityPolicy: false,
2425
}),
2526
);
27+
28+
29+
2630
// view engine setup
2731
app.set('views', path.join(__dirname, '../views'));
2832
app.set('view engine', 'pug');
2933

34+
// translations
35+
36+
app.use(i18n.init)
37+
38+
app.use(function(req, res, next) {
39+
// express helper for natively supported engines
40+
// @ts-ignore
41+
res.locals.__ = res.__ = function() {
42+
return i18n.__.apply(req, arguments);
43+
};
44+
45+
next();
46+
});
47+
3048
app.use(bodyParser.json());
3149
app.use(bodyParser.urlencoded({ extended: false }));
3250
app.use(cookieParser());

src/core/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ export const config = {
8585
'http://127.0.0.1:3000/download',
8686
// public static download spacename.
8787
public: '/download',
88+
locale: process.env.LOCALE || 'cn'
8889
},
8990
jwt: {
9091
// Recommended: 63 random alpha-numeric characters

src/core/i81n.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { I18n } from 'i18n'
2+
import path from "path"
3+
import { config } from "./config"
4+
5+
export const i18n = new I18n({
6+
locales: ['cn', 'en'],
7+
directory: path.join(__dirname, '../../locales'),
8+
defaultLocale: 'cn'
9+
})
10+
11+
i18n.setLocale(config.local.locale)

0 commit comments

Comments
 (0)