-
Notifications
You must be signed in to change notification settings - Fork 100
/
server.js
95 lines (79 loc) · 2.66 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
var express = require('express'),
app = express(),
path = require('path'),
i18n = require('i18next'),
i18nFsBackend = require('i18next-node-fs-backend'),
sprintf = require('i18next-sprintf-postprocessor')
i18nMiddleware = require('i18next-express-middleware'),
router = require('express').Router(),
port = 3000;
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.static('public'));
app.use(express.static('files'));
// i18next 初始設定
i18n.use(i18nMiddleware.LanguageDetector) // 自動偵測用戶端語系
.use(i18nFsBackend)
.use(sprintf)
.init({
fallbackLng: "en",
compatibilityAPI: 'v1',
compatibilityJSON: 'v1',
initImmediate: true,
load: "all",
interpolation:
{ escapeValue: true,
prefix: '{{',
suffix: '}}',
unescapePrefix: '-',
nestingPrefix: '$t(',
nestingSuffix: ')',
escapeInterpolation: false,
esscape: false,
defaultVariables: undefined },
detection: {
// order and from where user language should be detected
order: [/*'path', 'session', */ 'querystring', 'cookie', 'header'],
// keys or params to lookup language from
lookupPath: 'lng',
lookupFromPathIndex: 0,
// cache user language
caches: false, // ['cookie']
// optional expire and domain for set cookie
cookieExpirationDate: new Date(),
cookieDomain: 'popcorntime.sh'
},
resSetPath: 'locales/{{lng}}/__ns__.json',
saveMissing: true,
debug: true,
detection : {
// order and from where user language should be detected
order: ['path', 'session', 'querystring', 'cookie', 'header'],
// keys or params to lookup language from
lookupPath: 'lng',
lookupQuerystring: 'lng',
lookupCookie: 'i18next',
lookupSession: 'lng',
lookupFromPathIndex: 0,
// cache user language
caches: false, // ['cookie']
// optional expire and domain for set cookie
cookieExpirationDate: new Date(),
cookieDomain: 'myDomain'
},
sendMissingTo: 'fallback', // 備用語系,擷取失敗時會使用到這裡
backend: {
loadPath: path.join(__dirname, 'locales/{{lng}}/translation.json'),
}
});
app.use(i18nMiddleware.handle(i18n, {
ignoreRoutes: ["/images", "/css", "/js", "/fonts"],
removeLngFromUrl: false
}));
var routes = require('./routes/index');
app.use('/', routes);
//app.use(express.static('public'));
//app.use(express.static('files'));
app.listen(port, function() {
console.log("HTTP 伺服器在 http://127.0.0.1:3000/ 上運行 ");
});