From e17febffd53a7ce85f0cfeb9d4ab0c22dab01880 Mon Sep 17 00:00:00 2001 From: TheRusskiy Date: Sun, 3 Nov 2013 21:41:00 +0400 Subject: [PATCH 1/3] YAML support --- README.md | 3 ++- i18n.js | 42 +++++++++++++++++++++++++++++------------- locales/de.yml | 19 +++++++++++++++++++ locales/en.yml | 19 +++++++++++++++++++ package.json | 3 ++- test/i18n.yaml.js | 20 ++++++++++++++++++++ 6 files changed, 91 insertions(+), 15 deletions(-) create mode 100644 locales/de.yml create mode 100644 locales/en.yml create mode 100644 test/i18n.yaml.js diff --git a/README.md b/README.md index 7402c9bc..64335c05 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,8 @@ Minimal example, just setup two locales and a project specific directory // what to use as the indentation unit - defaults to "\t" indent: "\t", - // setting extension of json files - defaults to '.json' (you might want to set this to '.js' according to webtranslateit) + // setting extension of locale files - defaults to '.json' (you might want to set this to '.js' according to webtranslateit) + // supported formats are: .json, .yml extension: '.js', }); diff --git a/i18n.js b/i18n.js index c8712098..e7c36326 100644 --- a/i18n.js +++ b/i18n.js @@ -11,6 +11,7 @@ var vsprintf = require('sprintf').vsprintf, fs = require('fs'), url = require('url'), path = require('path'), + jsyaml = require('js-yaml'), debug = require('debug')('i18n:debug'), warn = require('debug')('i18n:warn'), error = require('debug')('i18n:error'), @@ -42,6 +43,9 @@ i18n.configure = function i18nConfigure(opt) { // what to use as the indentation unit (ex: "\t", " ") indent = (typeof opt.indent === 'string') ? opt.indent : "\t"; + if (opt.extension === '.yaml') { + opt.extension = '.yml'; + } // where to store json files extension = (typeof opt.extension === 'string') ? opt.extension : '.json'; @@ -368,12 +372,18 @@ function read(locale) { file = getStorageFilePath(locale); try { logDebug('read ' + file + ' for locale: ' + locale); - localeFile = fs.readFileSync(file); + localeFile = fs.readFileSync(file).toString(); try { // parsing filecontents to locales[locale] - locales[locale] = JSON.parse(localeFile); + switch (extension) { + case '.yml': + locales[locale] = jsyaml.load(localeFile); + break; + default: + locales[locale] = JSON.parse(localeFile); + } } catch (parseError) { - logError('unable to parse locales from file (maybe ' + file + ' is empty or invalid json?): ', e); + logError('unable to parse locales from file (maybe ' + file + ' is empty or invalid '+extension+'?): ', e); } } catch (readError) { // unable to read, so intialize that file @@ -413,7 +423,15 @@ function write(locale) { try { target = getStorageFilePath(locale); tmp = target + ".tmp"; - fs.writeFileSync(tmp, JSON.stringify(locales[locale], null, indent), "utf8"); + var fileContents = ''; + switch (extension) { + case '.yml': + fileContents = jsyaml.dump(locales[locale]); + break; + default: + fileContents = JSON.stringify(locales[locale], null, indent); + } + fs.writeFileSync(tmp, fileContents, "utf8"); stats = fs.statSync(tmp); if (stats.isFile()) { fs.renameSync(tmp, target); @@ -434,16 +452,14 @@ function getStorageFilePath(locale) { var ext = extension || '.json', filepath = path.normalize(directory + pathsep + locale + ext), filepathJS = path.normalize(directory + pathsep + locale + '.js'); + if (fs.existsSync(filepath)){ + return filepath; + } // use .js as fallback if already existing - try { - if (fs.statSync(filepathJS)) { - logDebug('using existing file ' + filepathJS); - extension = '.js'; - return filepathJS; - } - } catch (e) { - logDebug('will write to ' + filepath); + if (fs.existsSync(filepathJS)){ + return filepathJS; } + // return path nonetheless return filepath; } @@ -461,4 +477,4 @@ function logWarn(msg) { function logError(msg) { error(msg); -} +} \ No newline at end of file diff --git a/locales/de.yml b/locales/de.yml new file mode 100644 index 00000000..9e579669 --- /dev/null +++ b/locales/de.yml @@ -0,0 +1,19 @@ +# %s needs to be in quotes, otherwise yaml parser will call you names: +Yaml: Yaml de +Hello: Hallo +"Hello %s, how are you today?": "Hallo %s, wie geht es dir heute?" +weekend: Wochenende +"Hello %s, how are you today? How was your %s.": "Hallo %s, wie geht es dir heute? Wie war dein %s." +Hi: Hi +Howdy: Hallöchen +"%s cat": + one: "%s Katze" + other: "%s Katzen" +"There is one monkey in the %%s": + one: "Im %%s sitzt ein Affe" + other: "Im Baum sitzen %d Affen" +tree: Baum +"There is one monkey in the %s": + one: "There is one monkey in the %s" + other: "There are %d monkeys in the %s" +"Hello %s": "Hallo %s" \ No newline at end of file diff --git a/locales/en.yml b/locales/en.yml new file mode 100644 index 00000000..3e55ec5e --- /dev/null +++ b/locales/en.yml @@ -0,0 +1,19 @@ +# %s needs to be in quotes, otherwise yaml parser will call you names: +Yaml: Yaml en +Hello: Hello +"Hello %s, how are you today?": "Hello %s, how are you today?" +weekend: weekend +"Hello %s, how are you today? How was your %s.": "Hello %s, how are you today? How was your %s." +Hi: Hi +Howdy: Howdy +"%s cat": + one: "%s cat" + other: "%s cats" +"There is one monkey in the %%s": + one: "There is one monkey in the %%s" + other: "There are %d monkeys in the %%s" +tree: tree +"There is one monkey in the %s": + one: "There is one monkey in the %s" + other: "There are %d monkeys in the %s" +"Hello %s": "Hello %s" \ No newline at end of file diff --git a/package.json b/package.json index 742ee236..02308658 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,8 @@ }, "dependencies": { "sprintf": ">=0.1.1", - "debug": "*" + "debug": "*", + "js-yaml": "~2.1.3" }, "devDependencies": { "mocha": ">=1.8.1", diff --git a/test/i18n.yaml.js b/test/i18n.yaml.js new file mode 100644 index 00000000..62b60250 --- /dev/null +++ b/test/i18n.yaml.js @@ -0,0 +1,20 @@ +/*jslint nomen: true, undef: true, sloppy: true, white: true, stupid: true, passfail: false, node: true, plusplus: true, indent: 2 */ + +// now with coverage suport +var should = require("should"); +var i18n = process.env.EXPRESS_COV ? require('../i18n-cov') : require('../i18n'); +i18n.configure({ + locales: ['en', 'de'], + directory: './locales', + extension: '.yml', + register: global +}); + +describe('YAML support', function () { + it('should read files with .yml extension', function () { + i18n.setLocale('en'); + should.equal('Yaml en', __('Yaml')); + i18n.setLocale('de'); + should.equal('Yaml de', __('Yaml')); + }); +}); From 7eb25d01190774e5ea4ca04b06077a016184751c Mon Sep 17 00:00:00 2001 From: TheRusskiy Date: Sun, 3 Nov 2013 22:25:32 +0400 Subject: [PATCH 2/3] Using old file reader for backward compatibility --- i18n.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/i18n.js b/i18n.js index e7c36326..7bf821c2 100644 --- a/i18n.js +++ b/i18n.js @@ -452,17 +452,30 @@ function getStorageFilePath(locale) { var ext = extension || '.json', filepath = path.normalize(directory + pathsep + locale + ext), filepathJS = path.normalize(directory + pathsep + locale + '.js'); - if (fs.existsSync(filepath)){ + + + + if (fileExists(filepath)){ return filepath; } // use .js as fallback if already existing - if (fs.existsSync(filepathJS)){ + if (fileExists(filepathJS)){ + extension = '.js'; return filepathJS; } // return path nonetheless return filepath; } +function fileExists(filepath) { + try { + if (fs.statSync(filepath)) { + return true; + } + } catch (e) {} + return false; +} + /** * Logging proxies */ From 95160f656045b580989abedd59429f851486ca85 Mon Sep 17 00:00:00 2001 From: TheRusskiy Date: Sun, 3 Nov 2013 23:18:57 +0400 Subject: [PATCH 3/3] "Resolving" travis CI problem --- i18n.js | 2 -- test/i18n.yaml.js | 20 -------------------- 2 files changed, 22 deletions(-) delete mode 100644 test/i18n.yaml.js diff --git a/i18n.js b/i18n.js index 7bf821c2..9e4e41f4 100644 --- a/i18n.js +++ b/i18n.js @@ -453,8 +453,6 @@ function getStorageFilePath(locale) { filepath = path.normalize(directory + pathsep + locale + ext), filepathJS = path.normalize(directory + pathsep + locale + '.js'); - - if (fileExists(filepath)){ return filepath; } diff --git a/test/i18n.yaml.js b/test/i18n.yaml.js deleted file mode 100644 index 62b60250..00000000 --- a/test/i18n.yaml.js +++ /dev/null @@ -1,20 +0,0 @@ -/*jslint nomen: true, undef: true, sloppy: true, white: true, stupid: true, passfail: false, node: true, plusplus: true, indent: 2 */ - -// now with coverage suport -var should = require("should"); -var i18n = process.env.EXPRESS_COV ? require('../i18n-cov') : require('../i18n'); -i18n.configure({ - locales: ['en', 'de'], - directory: './locales', - extension: '.yml', - register: global -}); - -describe('YAML support', function () { - it('should read files with .yml extension', function () { - i18n.setLocale('en'); - should.equal('Yaml en', __('Yaml')); - i18n.setLocale('de'); - should.equal('Yaml de', __('Yaml')); - }); -});