server/lang.js   A
last analyzed

Complexity

Total Complexity 10
Complexity/F 1.67

Size

Lines of Code 104
Function Count 6

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
nc 2
dl 0
loc 104
rs 10
c 1
b 0
f 0
wmc 10
mnd 2
bc 10
fnc 6
bpm 1.6666
cpm 1.6666
noi 7

5 Functions

Rating   Name   Duplication   Size   Complexity  
A Router.get(ꞌ*ꞌ) 0 6 1
A Router.get(ꞌ/lang/:langꞌ) 0 7 1
A lang.js ➔ markdown 0 9 2
B lang.js ➔ init 0 25 1
A lang.js ➔ extendRead 0 22 3
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
Consider using the path module for constructing paths since they are otherwise not cross-OS compatible.
Loading history...
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
Compatibility introduced by
Consider using the path module for constructing paths since they are otherwise not cross-OS compatible.
Loading history...
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
Coding Style Best Practice introduced by
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 b = 42 will always be executed, while the logging statement will be executed conditionally.

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.

Loading history...
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
Bug introduced by
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.

Loading history...
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
Compatibility introduced by
Consider using the path module for constructing paths since they are otherwise not cross-OS compatible.
Loading history...
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
Bug introduced by
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.

Loading history...
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
Unused Code introduced by
The parameter next is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
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;