2017-06-07 13:28:42 +00:00
|
|
|
var fs = require('fs');
|
2017-11-27 14:08:18 +00:00
|
|
|
var config = require('./config.js');
|
2017-06-07 13:28:42 +00:00
|
|
|
var path = require('path');
|
|
|
|
|
|
|
|
module.exports = {
|
2017-09-27 15:44:51 +00:00
|
|
|
/**
|
|
|
|
* Returns template locale
|
|
|
|
* @param {String} template - Template name
|
|
|
|
* @param {String} countryCode - Language code
|
|
|
|
* @param {Object} cb - Callback
|
|
|
|
*/
|
2017-06-07 13:28:42 +00:00
|
|
|
load: function(template, countryCode, cb) {
|
|
|
|
var localeFile = path.join(__dirname, 'template', `${template}`, 'locale', `${countryCode}.json`);
|
2017-11-27 14:08:18 +00:00
|
|
|
var defaultLocaleFile = path.join(__dirname, 'template', `${template}`, 'locale', `${config.app.defaultLanguage}.json`);
|
2017-06-07 13:28:42 +00:00
|
|
|
|
|
|
|
fs.stat(localeFile, (error, stats) => {
|
2017-08-31 14:11:37 +00:00
|
|
|
if (error) {
|
2017-06-07 13:28:42 +00:00
|
|
|
fs.stat(defaultLocaleFile, (error, stats) => {
|
|
|
|
if (error)
|
2017-10-17 06:22:59 +00:00
|
|
|
return cb(new Error('Translation not found for template ' + template));
|
2017-08-31 14:11:37 +00:00
|
|
|
|
2017-10-17 06:22:59 +00:00
|
|
|
cb(null, {locale: require(defaultLocaleFile)});
|
2017-06-07 13:28:42 +00:00
|
|
|
});
|
2017-08-31 14:11:37 +00:00
|
|
|
} else {
|
2017-10-17 06:22:59 +00:00
|
|
|
cb(null, {locale: require(localeFile)});
|
2017-08-31 14:11:37 +00:00
|
|
|
}
|
2017-06-07 13:28:42 +00:00
|
|
|
});
|
2017-09-27 15:44:51 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse locale text
|
|
|
|
* @param {String} text - Locale text
|
|
|
|
* @param {Object} params - Locale params
|
|
|
|
* @return {String} - Returns parsed text
|
|
|
|
*/
|
|
|
|
parseText: function(text, params) {
|
|
|
|
for (var key in params) {
|
|
|
|
text = text.replace(`%${key}%`, params[key]);
|
|
|
|
}
|
|
|
|
return text;
|
2017-06-07 13:28:42 +00:00
|
|
|
}
|
|
|
|
};
|