mirror of
https://github.com/HackPlan/RootPanel.git
synced 2026-04-22 19:03:34 +08:00
自行编写国际化功能
This commit is contained in:
33
core/i18n.coffee
Normal file
33
core/i18n.coffee
Normal file
@@ -0,0 +1,33 @@
|
||||
fs = require 'fs'
|
||||
|
||||
options =
|
||||
default_language: null
|
||||
available_language: []
|
||||
|
||||
data = {}
|
||||
|
||||
exports.init = (data) ->
|
||||
options = data
|
||||
|
||||
exports.load = (path) ->
|
||||
for lang in options.available_language
|
||||
data[lang] = JSON.parse fs.readFileSync((require 'path').join(path, "#{lang}.json"), 'utf8')
|
||||
|
||||
exports.translate = (name, lang) ->
|
||||
unless lang
|
||||
lang = options.default_language
|
||||
|
||||
names = name.split '.'
|
||||
result = data[lang][names.shift()]
|
||||
|
||||
for item in names
|
||||
if result[item]
|
||||
result = result[item]
|
||||
else
|
||||
return name
|
||||
|
||||
return result
|
||||
|
||||
exports.getTranslator = (lang) ->
|
||||
return (name) ->
|
||||
return exports.translate name, lang
|
||||
@@ -1,5 +1,4 @@
|
||||
express = require 'express'
|
||||
i18next = require 'i18next'
|
||||
connect = require 'connect'
|
||||
path = require 'path'
|
||||
fs = require 'fs'
|
||||
@@ -7,17 +6,18 @@ fs = require 'fs'
|
||||
config = require './config'
|
||||
api = require './api'
|
||||
db = require './db'
|
||||
i18n = require './i18n'
|
||||
|
||||
exports.runWebServer = ->
|
||||
db.connect ->
|
||||
app = express()
|
||||
|
||||
i18next.init
|
||||
fallbackLng: config.i18n.defaultLanguage
|
||||
resGetPath: path.join(__dirname, 'locale/__lng__.json')
|
||||
i18n.init
|
||||
default_language: 'zh_CN'
|
||||
available_language: ['zh_CN']
|
||||
|
||||
i18n.load path.join(__dirname, 'locale')
|
||||
|
||||
i18next.registerAppHelper app
|
||||
app.use i18next.handle
|
||||
app.use connect.json()
|
||||
app.use connect.urlencoded()
|
||||
app.use connect.cookieParser()
|
||||
@@ -43,6 +43,7 @@ exports.runWebServer = ->
|
||||
|
||||
app.use (req, res, next) ->
|
||||
res.locals.app = app
|
||||
res.locals.t = i18n.getTranslator 'zh_CN'
|
||||
|
||||
next()
|
||||
|
||||
|
||||
@@ -27,7 +27,6 @@
|
||||
"express": "*",
|
||||
"jade": "*",
|
||||
"less": "*",
|
||||
"i18next": "*",
|
||||
"coffee-script": "*",
|
||||
"mongodb": "*",
|
||||
"underscore": "*",
|
||||
|
||||
Reference in New Issue
Block a user