Skip to content

Commit

Permalink
Add lazy evaluation for default message, since default message is not…
Browse files Browse the repository at this point in the history
… used most of the time.
  • Loading branch information
Jimmy Chao committed Apr 11, 2017
1 parent 2106dbc commit 3395177
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
28 changes: 21 additions & 7 deletions app/assets/javascripts/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,12 @@
// Borrowed from Underscore.js
var isObject = function(obj) {
var type = typeof obj;
return type === 'function' || type === 'object' && !!obj;
return type === 'function' || type === 'object'
};

var isFunction = function(func) {
var type = typeof func;
return type === 'function'
};

// Check if value is different than undefined and null;
Expand Down Expand Up @@ -100,6 +105,14 @@
return +(value[0] + 'e' + (value[1] ? (+value[1] + exp) : exp));
}

var lazyEvaluate = function(message, scope) {
if (isFunction(message)) {
return message(scope);
} else {
return message;
}
}

var merge = function (dest, obj) {
var key, value;
for (key in obj) if (obj.hasOwnProperty(key)) {
Expand Down Expand Up @@ -218,7 +231,7 @@
I18n.locales.get = function(locale) {
var result = this[locale] || this[I18n.locale] || this["default"];

if (typeof(result) === "function") {
if (isFunction(result)) {
result = result(locale);
}

Expand Down Expand Up @@ -256,7 +269,7 @@
// Compute each locale with its country code.
// So this will return an array containing both
// `de-DE` and `de` locales.
locales.forEach(function(locale){
locales.forEach(function(locale) {
countryCode = locale.split("-")[0];

if (!~list.indexOf(locale)) {
Expand Down Expand Up @@ -314,14 +327,15 @@
, requestedLocale = locales[0]
, locale
, scopes
, fullScope
, translations
;

scope = this.getFullScope(scope, options);
fullScope = this.getFullScope(scope, options);

while (locales.length) {
locale = locales.shift();
scopes = scope.split(this.defaultSeparator);
scopes = fullScope.split(this.defaultSeparator);
translations = this.translations[locale];

if (!translations) {
Expand All @@ -341,7 +355,7 @@
}

if (isSet(options.defaultValue)) {
return options.defaultValue;
return lazyEvaluate(options.defaultValue, scope);
}
};

Expand Down Expand Up @@ -504,7 +518,7 @@
if (isSet(translationOption.scope)) {
translation = this.lookup(translationOption.scope, options);
} else if (isSet(translationOption.message)) {
translation = translationOption.message;
translation = lazyEvaluate(translationOption.message, scope);
}

if (translation !== undefined && translation !== null) {
Expand Down
11 changes: 11 additions & 0 deletions spec/js/translate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,17 @@ describe("Translate", function(){
actual = I18n.t("foo", options);
expect(actual).toEqual("Hello all!");
});

it("uses default value with lazy evaluation", function () {
var options = {
defaults: [{scope: "bar"}]
, defaultValue: function(scope) {
return scope.toUpperCase();
}
};
actual = I18n.t("foo", options);
expect(actual).toEqual("FOO");
})
});

it("uses default value for simple translation", function(){
Expand Down

0 comments on commit 3395177

Please sign in to comment.