forked from 1602/compound
/
locales.js
143 lines (128 loc) · 3.98 KB
/
locales.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
var fs = require('fs'),
yaml = require('yaml-js'),
coffee = require('coffee-script'),
path = require('path'),
localeData = {};
/**
* Initialize localization module
*/
exports.init = function () {
var dir = app.root + '/config/locales';
if (!path.existsSync(dir)) {
return false;
}
app.locales = app.locales || [];
exports.load(dir);
};
/**
* Load localization files from `dir`. Locales can be in yaml, json or coffee
* format
*
* Example locale.yml file:
*
* en:
* key: 'Value'
*
* @param {String} dir - absolute path to locales directory
*/
exports.load = function (dir) {
fs.readdirSync(dir).forEach(function (file) {
if (file.match(/^\./)) return;
var filename = dir + '/' + file;
var code = fs.readFileSync(filename, 'utf8').toString();
var obj;
try {
if (file.match(/\.ya?ml$/)) {
obj = require(filename).shift();
} else if (file.match(/\.json/)) {
obj = JSON.parse(code);
} else if (file.match(/\.coffee/)) {
obj = coffee.eval(code);
} else {
console.log('Unsupported extension of locale file ' + filename);
}
} catch (e) {
console.log('Parsing file ' + filename);
console.log(e);
console.log(e.stack);
}
if (obj) {
addTranslation(obj);
}
});
};
/**
* Add translation to `lang` to application locales collection
*/
function addTranslation(lang) {
Object.keys(lang).forEach(function (localeName) {
var translations = lang[localeName];
if (app.locales.indexOf(localeName) === -1) {
app.locales.push([localeName, translations.lang && translations.lang.name || localeName]);
}
localeData[localeName] = localeData[localeName] || {};
Object.keys(translations).forEach(function (namespace) {
localeData[localeName][namespace] = translations[namespace];
});
});
}
/**
* Global translation helper
*
* @param {Boolean} global
* @public
*/
function T(global) {
if (global) {
// helper for global scope (models, initializers, etc)
// requires two params (locale expected)
return function t(path, locale, defaultValue) {
if (!locale) {
throw new Error('Locale expected');
}
return translate (path, locale);
};
} else {
// helper for local scope (controllers, views, helpers)
// requires one param
return function t(path, defaultValue) {
return translate(path, t.locale, defaultValue);
};
}
function translate(path, locale, defaultValue) {
var translation = localeData[locale], substitute;
function nextPathItem(token) {
return (translation = translation[token]);
}
if (typeof path === 'string') {
substitute = false;
} else {
substitute = path;
path = substitute.shift();
}
if (!translation || !path.split('.').every(nextPathItem)) {
translation = defaultValue || translationMissing(locale, path);
}
if (translation && substitute && substitute.length) {
substitute.forEach(function (substitution) {
translation = translation.replace(/%/, substitution.toString().replace(/%/g, ''));
});
}
return translation;
}
function translationMissing(locale, path) {
switch (app.settings.translationMissing) {
case 'display':
return 'translation missing for ' + locale + '.' + path;
case 'default':
case undefined:
var defLocale = app.settings.defaultLocale;
return !defLocale || locale === defLocale ? '' : translate(path, defLocale);
}
}
};
T.localeSupported = function (localeName) {
return !!localeData[localeName];
};
global.t = T(true);
global.T = T;