From 49772bb571015e5466d34d2247589e7ac9e8f1d0 Mon Sep 17 00:00:00 2001 From: Greg Laabs Date: Sat, 24 May 2014 18:04:32 -0700 Subject: [PATCH 1/3] Translation loader now ignores system files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I was having an issue with habitrpg failing to load when OSX created a .DS_Store file in the locales directory. This code fixes that by ignoring all hidden files (files that start with “.”) --- src/i18n.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/i18n.js b/src/i18n.js index d0668814b1..044d20dd38 100644 --- a/src/i18n.js +++ b/src/i18n.js @@ -17,7 +17,7 @@ var loadTranslations = function(locale){ loadTranslations('en'); fs.readdirSync(path.join(__dirname, "/../node_modules/habitrpg-shared/locales/")).forEach(function(file) { - if(file === 'en' || file === 'README.md') return; + if(file === 'en' || file === 'README.md' || file.indexOf('.') === 0) return; loadTranslations(file); // Merge missing strings from english _.defaults(translations[file], translations.en); From 84e122380981020e839cc08d13f5217d4a5bb878 Mon Sep 17 00:00:00 2001 From: Greg Laabs Date: Sun, 25 May 2014 09:13:23 -0700 Subject: [PATCH 2/3] 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. --- src/i18n.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/i18n.js b/src/i18n.js index 044d20dd38..43cddf6f32 100644 --- a/src/i18n.js +++ b/src/i18n.js @@ -5,19 +5,21 @@ var fs = require('fs'), shared = require('habitrpg-shared'), translations = {}; +var localePath = path.join(__dirname, "/../node_modules/habitrpg-shared/locales/") + 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] = {}; _.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 loadTranslations('en'); -fs.readdirSync(path.join(__dirname, "/../node_modules/habitrpg-shared/locales/")).forEach(function(file) { - if(file === 'en' || file === 'README.md' || file.indexOf('.') === 0) return; +fs.readdirSync(localePath).forEach(function(file) { + if(file === 'en' || fs.statSync(path.join(localePath, file)).isDirectory() === false) return; loadTranslations(file); // Merge missing strings from english _.defaults(translations[file], translations.en); From 11595caeb5f8ea22b59153ae8b5bd238a47e33d5 Mon Sep 17 00:00:00 2001 From: Greg Laabs Date: Mon, 26 May 2014 08:18:48 -0700 Subject: [PATCH 3/3] Only load .json files from locale directories This opens up the possibility of putting READMEs or what have you inside locale directories, and will avoid problems if system files or folders end up in the locales. --- src/i18n.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/i18n.js b/src/i18n.js index 43cddf6f32..238df963df 100644 --- a/src/i18n.js +++ b/src/i18n.js @@ -11,6 +11,7 @@ var loadTranslations = function(locale){ var files = fs.readdirSync(path.join(localePath, locale)); translations[locale] = {}; _.each(files, function(file){ + if(path.extname(file) !== '.json') return; _.merge(translations[locale], require(path.join(localePath, locale, file))); }); };