Locale loader now ignores non-directories

Locales must be stored in directories and the code previously crashed
if there were any unexpected files in the locale folder. Now the code
simply ignores all non-directories.
This commit is contained in:
Greg Laabs 2014-05-25 09:13:23 -07:00
parent 49772bb571
commit 84e1223809

View file

@ -5,19 +5,21 @@ var fs = require('fs'),
shared = require('habitrpg-shared'), shared = require('habitrpg-shared'),
translations = {}; translations = {};
var localePath = path.join(__dirname, "/../node_modules/habitrpg-shared/locales/")
var loadTranslations = function(locale){ var loadTranslations = function(locale){
var files = fs.readdirSync(path.join(__dirname, "/../node_modules/habitrpg-shared/locales/", locale)); var files = fs.readdirSync(path.join(localePath, locale));
translations[locale] = {}; translations[locale] = {};
_.each(files, function(file){ _.each(files, function(file){
_.merge(translations[locale], require(path.join(__dirname, "/../node_modules/habitrpg-shared/locales/", locale, file))); _.merge(translations[locale], require(path.join(localePath, locale, file)));
}); });
}; };
// First fetch english so we can merge with missing strings in other languages // First fetch english so we can merge with missing strings in other languages
loadTranslations('en'); loadTranslations('en');
fs.readdirSync(path.join(__dirname, "/../node_modules/habitrpg-shared/locales/")).forEach(function(file) { fs.readdirSync(localePath).forEach(function(file) {
if(file === 'en' || file === 'README.md' || file.indexOf('.') === 0) return; if(file === 'en' || fs.statSync(path.join(localePath, file)).isDirectory() === false) return;
loadTranslations(file); loadTranslations(file);
// Merge missing strings from english // Merge missing strings from english
_.defaults(translations[file], translations.en); _.defaults(translations[file], translations.en);