Skip to content

Commit

Permalink
Add support for falling back onto messages from default locale
Browse files Browse the repository at this point in the history
Inspired by mashpie@ee0da09
but fixing infinite recusion bug in that approach
  • Loading branch information
eloquence committed May 16, 2017
1 parent 4e96e61 commit b2e67a8
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ module.exports = (function() {
autoReload,
cookiename,
defaultLocale,
retryInDefaultLocale,
directory,
directoryPermissions,
extension,
Expand Down Expand Up @@ -131,6 +132,9 @@ module.exports = (function() {
// setting defaultLocale
defaultLocale = (typeof opt.defaultLocale === 'string') ? opt.defaultLocale : 'en';

retryInDefaultLocale = (typeof opt.retryInDefaultLocale === 'boolean') ?
opt.retryInDefaultLocale : false;

// auto reload locale files when changed
autoReload = (typeof opt.autoReload === 'boolean') ? opt.autoReload : false;

Expand Down Expand Up @@ -829,7 +833,7 @@ module.exports = (function() {
/**
* read locale file, translate a msg and write to fs if new
*/
var translate = function(locale, singular, plural, skipSyncToAllFiles) {
var translate = function(locale, singular, plural, skipSyncToAllFiles, skipDefaultCheck) {

// add same key to all translations
if (!skipSyncToAllFiles && syncFiles) {
Expand Down Expand Up @@ -885,21 +889,20 @@ module.exports = (function() {
var accessor = localeAccessor(locale, singular);
var mutator = localeMutator(locale, singular);

if (plural) {
if (!accessor()) {
mutator({
'one': defaultSingular || singular,
'other': defaultPlural || plural
});
write(locale);
}
}

if (!accessor()) {
mutator(defaultSingular || singular);
if (retryInDefaultLocale && !skipDefaultCheck) {
mutator(translate(defaultLocale, singular, plural, true, true));
} else {
if (plural)
mutator({
'one': defaultSingular || singular,
'other': defaultPlural || plural
});
else
mutator(defaultSingular || singular);
}
write(locale);
}

return accessor();
};

Expand Down

0 comments on commit b2e67a8

Please sign in to comment.