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

Add YAML compatibility. #73

Closed
wants to merge 2 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
31 changes: 25 additions & 6 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 @@ -43,8 +44,12 @@ 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';
extension = (typeof opt.extension === 'string') ? opt.extension : 'json';

// setting defaultLocale
defaultLocale = (typeof opt.defaultLocale === 'string') ? opt.defaultLocale : 'en';
Expand Down Expand Up @@ -368,10 +373,16 @@ 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);
}
Expand Down Expand Up @@ -413,7 +424,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 @@ -431,8 +450,8 @@ function write(locale) {

function getStorageFilePath(locale) {
// changed API to use .json as default, #16
var ext = extension || '.json',
filepath = path.normalize(directory + pathsep + locale + ext),
var ext = extension || 'json',
filepath = path.normalize(directory + pathsep + locale + '.' + ext),
filepathJS = path.normalize(directory + pathsep + locale + '.js');
// use .js as fallback if already existing
try {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
},
"dependencies": {
"sprintf": ">=0.1.1",
"js-yaml": "2.1.0",
"debug": "*"
},
"devDependencies": {
Expand Down