Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

YAML support #79

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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',
});

Expand Down
53 changes: 40 additions & 13 deletions i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -434,19 +452,28 @@ function getStorageFilePath(locale) {
var ext = extension || '.json',
filepath = path.normalize(directory + pathsep + locale + ext),
filepathJS = path.normalize(directory + pathsep + locale + '.js');

if (fileExists(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 (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
*/
Expand All @@ -461,4 +488,4 @@ function logWarn(msg) {

function logError(msg) {
error(msg);
}
}
19 changes: 19 additions & 0 deletions locales/de.yml
Original file line number Diff line number Diff line change
@@ -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"
19 changes: 19 additions & 0 deletions locales/en.yml
Original file line number Diff line number Diff line change
@@ -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"
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
},
"dependencies": {
"sprintf": ">=0.1.1",
"debug": "*"
"debug": "*",
"js-yaml": "~2.1.3"
},
"devDependencies": {
"mocha": ">=1.8.1",
Expand Down