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')); |
||
0 ignored issues
–
show
Compatibility
introduced
by
![]() |
|||
13 | |||
14 | l10n.extendRead = extendRead; |
||
15 | |||
16 | l10n.configure({ |
||
17 | locales: ['en', 'fr'], |
||
18 | cookie: COOKIE, |
||
19 | updateFiles: false, |
||
20 | directory: __dirname + '/../locales' |
||
0 ignored issues
–
show
|
|||
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
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
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 (a > 0)
b = 42;
If you or someone else later decides to put another statement in, only the first statement will be executed. if (a > 0)
console.log("a > 0");
b = 42;
In this case the statement if (a > 0) {
console.log("a > 0");
b = 42;
}
ensures that the proper code will be executed conditionally no matter how many statements are added or removed. ![]() |
|||
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'; |
||
0 ignored issues
–
show
The variable
SARAH seems to be never declared. If this is a global, consider adding a /** global: SARAH */ comment.
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed. To learn more about declaring variables in Javascript, see the MDN. ![]() |
|||
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'; |
||
0 ignored issues
–
show
|
|||
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); |
||
0 ignored issues
–
show
The variable
SARAH seems to be never declared. If this is a global, consider adding a /** global: SARAH */ comment.
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed. To learn more about declaring variables in Javascript, see the MDN. ![]() |
|||
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) { |
||
0 ignored issues
–
show
|
|||
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; |