Skip to content

Commit

Permalink
Expose API to override serialization:
Browse files Browse the repository at this point in the history
i18n.serializeLocale(localeObj) - convert object to string
i18n.deserializeLocale(fileContent) - parse file content to object
  • Loading branch information
Paxa committed Feb 12, 2016
1 parent c6274f4 commit 0cd5d7f
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,14 @@ i18n._renderMustach = function renderMustach(msg, namedValues) {
return Mustache.render(msg, namedValues);
};

i18n.serializeLocale = function serializeLocal (localeObj) {
return JSON.stringify(localeObj, null, i18n.options.indent);
};

i18n.deserializeLocale = function serializeLocal (fileContent) {
return JSON.parse(fileContent);
};

// ===================
// = private methods =
// ===================
Expand Down Expand Up @@ -764,7 +772,7 @@ function read(locale) {
localeFile = fs.readFileSync(file);
try {
// parsing filecontents to locales[locale]
locales[locale] = JSON.parse(localeFile);
locales[locale] = i18n.deserializeLocale(localeFile);
} catch (parseError) {
logError('unable to parse locales from file (maybe ' + file + ' is empty or invalid json?): ', parseError);
}
Expand Down Expand Up @@ -813,7 +821,8 @@ function write(locale) {
try {
target = getStorageFilePath(locale);
tmp = target + ".tmp";
fs.writeFileSync(tmp, JSON.stringify(locales[locale], null, i18n.options.indent), "utf8");
var serialized = i18n.serializeLocale(locales[locale]);
fs.writeFileSync(tmp, serialized, "utf8");
stats = fs.statSync(tmp);
if (stats.isFile()) {
fs.renameSync(tmp, target);
Expand Down

0 comments on commit 0cd5d7f

Please sign in to comment.