mirror of
https://github.com/HackPlan/RootPanel.git
synced 2026-01-12 22:27:09 +08:00
refactor about i18n
This commit is contained in:
21
app.coffee
21
app.coffee
@@ -6,19 +6,13 @@ moment = require 'moment'
|
||||
mongomin = require 'mongo-min'
|
||||
redis = require 'redis'
|
||||
|
||||
global._ = require 'underscore'
|
||||
global.ObjectID = require('mongodb').ObjectID
|
||||
global.express = require 'express'
|
||||
global.async = require 'async'
|
||||
global.path = require 'path'
|
||||
|
||||
global.app = express()
|
||||
global.config = require './config'
|
||||
global.i18n = require './core/i18n'
|
||||
global.utils = require './core/router/utils'
|
||||
global.pluggable = require './core/pluggable'
|
||||
|
||||
if fs.existsSync(config.web.listen)
|
||||
if fs.existsSync config.web.listen
|
||||
fs.unlinkSync config.web.listen
|
||||
|
||||
fs.chmodSync path.join(__dirname, 'config.coffee'), 0o750
|
||||
@@ -50,12 +44,6 @@ exports.run = ->
|
||||
exports.connectDatabase (err) ->
|
||||
throw err if err
|
||||
|
||||
i18n.init
|
||||
default_language: 'zh_CN'
|
||||
available_language: ['zh_CN']
|
||||
|
||||
i18n.load path.join(__dirname, 'core/locale')
|
||||
|
||||
app.package = require './package.json'
|
||||
|
||||
app.use connect.json()
|
||||
@@ -67,9 +55,8 @@ exports.run = ->
|
||||
|
||||
app.use (req, res, next) ->
|
||||
res.locals.app = app
|
||||
res.locals.moment = moment
|
||||
res.locals.t = i18n.getTranslator 'zh_CN'
|
||||
res.locals.mAccount = require './core/model/account'
|
||||
res.locals.t = i18n.getTranslator req.cookies.language
|
||||
res.moment = moment().locale(req.cookies.language ? config.i18n.default_language).tz(req.cookies.timezone ? config.i18n.default_timezone)
|
||||
|
||||
next()
|
||||
|
||||
@@ -78,7 +65,7 @@ exports.run = ->
|
||||
|
||||
bindRouters app
|
||||
|
||||
app.use harp.mount(path.join(__dirname, 'core/static'))
|
||||
app.use harp.mount './core/static'
|
||||
|
||||
app.listen config.web.listen, ->
|
||||
fs.chmodSync config.web.listen, 0o770
|
||||
|
||||
@@ -9,8 +9,9 @@ module.exports =
|
||||
cookie_time: 30 * 24 * 3600 * 1000
|
||||
|
||||
i18n:
|
||||
defaultLanguage: 'zh_CN'
|
||||
availableLanguage: ['zh_CN']
|
||||
available_language: ['zh_CN']
|
||||
default_language: 'zh_CN'
|
||||
default_timezone: 'Asia/Shanghai'
|
||||
|
||||
plugin:
|
||||
available_extensions: ['rpvhost']
|
||||
|
||||
@@ -1,36 +1,37 @@
|
||||
path = require 'path'
|
||||
fs = require 'fs'
|
||||
|
||||
options =
|
||||
default_language: null
|
||||
available_language: []
|
||||
config = require '../config'
|
||||
|
||||
data = {}
|
||||
i18n_data = {}
|
||||
|
||||
exports.init = (data) ->
|
||||
options = data
|
||||
for lang in config.i18n.available_language
|
||||
i18n_data[lang] = require "./locale/#{lang}"
|
||||
|
||||
exports.load = (path) ->
|
||||
exports.loadForPlugin = (plugin) ->
|
||||
for lang in options.available_language
|
||||
data[lang] = JSON.parse fs.readFileSync((require 'path').join(path, "#{lang}.json"), 'utf8')
|
||||
path = "../plugin/#{plugin.name}/locale/#{lang}.json"
|
||||
|
||||
exports.loadForPlugin = (name, path) ->
|
||||
for lang in options.available_language
|
||||
data[lang]['plugins'][name] = JSON.parse fs.readFileSync((require 'path').join(path, "#{lang}.json"), 'utf8')
|
||||
if fs.existsSync path
|
||||
i18n_data[lang]['plugins'][plugin.name] = require lang
|
||||
|
||||
exports.translate = (name, lang) ->
|
||||
unless lang
|
||||
lang = options.default_language
|
||||
lang = config.i18n.default_language
|
||||
|
||||
names = name.split '.'
|
||||
result = data[lang][names.shift()]
|
||||
keys = key.split '.'
|
||||
keys.unshift lang
|
||||
|
||||
for item in names
|
||||
if result and result[item]
|
||||
result = object
|
||||
|
||||
for item in keys
|
||||
unless result[item] == undefined
|
||||
result = result[item]
|
||||
else
|
||||
return name
|
||||
|
||||
return result
|
||||
if result == undefined and lang != config.i18n.default_language
|
||||
return exports.translate name, config.i18n.default_language
|
||||
else
|
||||
return result
|
||||
|
||||
exports.getTranslator = (lang) ->
|
||||
return (name) ->
|
||||
|
||||
@@ -20,7 +20,7 @@ exports.initializePlugins = (callback) ->
|
||||
plugin = require plugin_path
|
||||
|
||||
if fs.existsSync path.join(plugin_path, 'locale')
|
||||
i18n.loadForPlugin name, path.join(plugin_path, 'locale')
|
||||
i18n.loadForPlugin plugin
|
||||
|
||||
if fs.existsSync path.join(plugin_path, 'static')
|
||||
app.use harp.mount("/plugin/#{name}", path.join(plugin_path, 'static'))
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "rootpanel",
|
||||
"version": "0.7.1",
|
||||
"version": "0.7.2",
|
||||
"description": "A linux virtual host management system with extensibility",
|
||||
"homepage": "http://rpvhost.net",
|
||||
"license": "GPL-3.0",
|
||||
@@ -38,7 +38,7 @@
|
||||
"middleware-injector": "~0.1.1",
|
||||
"tmp": "~0.0.24",
|
||||
"mysql": "~2.4.2",
|
||||
"moment": "~2.8.1",
|
||||
"moment-timezone": "~0.2.2",
|
||||
"request": "~2.40.0",
|
||||
"mongo-min": "~0.2.2",
|
||||
"redis": "~0.12.1",
|
||||
|
||||
Reference in New Issue
Block a user