From 67f476feb17aab404c180875552cfd2b953a1008 Mon Sep 17 00:00:00 2001 From: jysperm Date: Sat, 6 Sep 2014 02:08:31 +0800 Subject: [PATCH] refactor about i18n --- app.coffee | 21 ++++----------------- config.coffee | 5 +++-- core/i18n.coffee | 39 ++++++++++++++++++++------------------- core/pluggable.coffee | 2 +- package.json | 4 ++-- 5 files changed, 30 insertions(+), 41 deletions(-) diff --git a/app.coffee b/app.coffee index 49e108b..8947cb6 100644 --- a/app.coffee +++ b/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 diff --git a/config.coffee b/config.coffee index 45b070c..d75c64e 100644 --- a/config.coffee +++ b/config.coffee @@ -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'] diff --git a/core/i18n.coffee b/core/i18n.coffee index 688911e..877444d 100644 --- a/core/i18n.coffee +++ b/core/i18n.coffee @@ -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) -> diff --git a/core/pluggable.coffee b/core/pluggable.coffee index 9eb2435..4d1af58 100644 --- a/core/pluggable.coffee +++ b/core/pluggable.coffee @@ -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')) diff --git a/package.json b/package.json index 8fd1807..c290e0b 100644 --- a/package.json +++ b/package.json @@ -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",