1 | var fs = require('fs'); |
||
2 | var extend = require('extend'); |
||
3 | var express = require('express'); |
||
4 | var l10n = require('i18n'); |
||
5 | var marked = require('marked'); |
||
6 | |||
7 | // ------------------------------------------ |
||
8 | // CONSTRUCTOR |
||
9 | // ------------------------------------------ |
||
10 | |||
11 | var init = function(){ |
||
12 | info('Starting LangManager ...', (__dirname + '/../locales')); |
||
13 | |||
14 | l10n.extendRead = extendRead; |
||
15 | |||
16 | l10n.configure({ |
||
17 | locales: ['en', 'fr'], |
||
18 | cookie: COOKIE, |
||
19 | updateFiles: false, |
||
20 | directory: __dirname + '/../locales' |
||
21 | }); |
||
22 | |||
23 | // Set global variable i18n |
||
24 | global.i18n = function(key){ |
||
25 | var value = l10n.__.apply(this, arguments); |
||
26 | var def = arguments[arguments.length - 1]; |
||
27 | if (value == key) return def; |
||
0 ignored issues
–
show
|
|||
28 | return value; |
||
29 | }; |
||
30 | |||
31 | // Set global variable markdown |
||
32 | global.marked = markdown; |
||
33 | |||
34 | return LangManager; |
||
35 | } |
||
36 | |||
37 | var markdown = function(path){ |
||
38 | var lang = l10n.getLocale() |
||
39 | |||
40 | var path = SARAH.ConfigManager.PLUGIN + '/' + path.replace('/','/locales/') + '.' + lang + '.md'; |
||
41 | if (!fs.existsSync(path)){ return; } |
||
42 | |||
43 | var markdown = fs.readFileSync(path, 'utf8'); |
||
44 | return '<div class="markdown">' + marked(markdown) + '</div>'; |
||
45 | } |
||
46 | |||
47 | var extendRead = function(locale){ |
||
48 | info('Loading all locales %s', locale); |
||
49 | |||
50 | var prop = {}; |
||
51 | |||
52 | // Load core locales |
||
53 | var path = __dirname + '/../locales/' + locale + '.js'; |
||
54 | if (fs.existsSync(path)){ |
||
55 | try { |
||
56 | var json = fs.readFileSync(path, 'utf8'); |
||
57 | var core = JSON.parse(json); |
||
58 | extend(true, prop, core); |
||
59 | } |
||
60 | catch(ex){ warn("Can't parse core locale in %s", locale); } |
||
61 | } |
||
62 | |||
63 | // Load plugins locales |
||
64 | var plugins = SARAH.PluginManager.getLocales(locale); |
||
65 | extend(true, prop, plugins); |
||
66 | |||
67 | return prop; |
||
68 | } |
||
69 | |||
70 | // ------------------------------------------ |
||
71 | // ROUTER |
||
72 | // ------------------------------------------ |
||
73 | |||
74 | var COOKIE = 'sarah-lang' |
||
75 | var Router = express.Router(); |
||
76 | |||
77 | // Set lang according to cookie |
||
78 | Router.get('*', function(req, res, next) { |
||
79 | var lang = req.cookies[COOKIE] || 'fr'; |
||
80 | l10n.setLocale(lang); |
||
81 | res.locals.lang = lang; |
||
82 | next(); |
||
83 | }) |
||
84 | |||
85 | // Set lang according to URL |
||
86 | Router.get('/lang/:lang', function(req, res, next) { |
||
87 | var lang = req.params.lang; |
||
88 | info('Update language to %s', lang); |
||
89 | l10n.setLocale(lang); |
||
90 | res.cookie(COOKIE, lang, { maxAge: 900000 }); |
||
91 | res.redirect('/'); |
||
92 | }); |
||
93 | |||
94 | // ------------------------------------------ |
||
95 | // PUBLIC |
||
96 | // ------------------------------------------ |
||
97 | |||
98 | var LangManager = { |
||
99 | 'init' : init, |
||
100 | 'Router' : Router |
||
101 | } |
||
102 | |||
103 | // Exports Manager |
||
104 | exports.init = LangManager.init; |
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.
Consider:
If you or someone else later decides to put another statement in, only the first statement will be executed.
In this case the statement
b = 42
will always be executed, while the logging statement will be executed conditionally.ensures that the proper code will be executed conditionally no matter how many statements are added or removed.