salix/services/print/application/locale.js

43 lines
1.3 KiB
JavaScript
Raw Normal View History

2017-11-27 14:08:18 +00:00
var fs = require('fs');
var config = require('./config.js');
var path = require('path');
module.exports = {
/**
* Returns template locale
* @param {String} template - Template name
* @param {String} countryCode - Language code
* @param {Object} cb - Callback
*/
load: function(template, countryCode, cb) {
var localeFile = path.join(__dirname, 'template', `${template}`, 'locale', `${countryCode}.json`);
var defaultLocaleFile = path.join(__dirname, 'template', `${template}`, 'locale', `${config.app.defaultLanguage}.json`);
fs.stat(localeFile, (error, stats) => {
if (error) {
fs.stat(defaultLocaleFile, (error, stats) => {
if (error)
return cb(new Error('Translation not found for template ' + template));
cb(null, {locale: require(defaultLocaleFile)});
});
} else {
cb(null, {locale: require(localeFile)});
}
});
},
/**
* 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;
}
};