app/assets/javascripts/i18n.js in i18n-js-3.0.0.rc4 vs app/assets/javascripts/i18n.js in i18n-js-3.0.0.rc5

- old
+ new

@@ -12,10 +12,56 @@ // See tests for specific formatting like numbers and dates. // ;(function(I18n){ "use strict"; + // Just cache the Array#slice function. + var slice = Array.prototype.slice; + + // Apply number padding. + var padding = function(number) { + return ("0" + number.toString()).substr(-2); + }; + + // Set default days/months translations. + var DAYS_AND_MONTHS = { + day_names: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"] + , abbr_day_names: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"] + , month_names: [null, "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"] + , abbr_month_names: [null, "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] + }; + + // Set default number format. + var NUMBER_FORMAT = { + precision: 3 + , separator: "." + , delimiter: "," + , strip_insignificant_zeros: false + }; + + // Set default currency format. + var CURRENCY_FORMAT = { + unit: "$" + , precision: 2 + , format: "%u%n" + , delimiter: "," + , separator: "." + }; + + // Set default percentage format. + var PERCENTAGE_FORMAT = { + precision: 3 + , separator: "." + , delimiter: "" + }; + + // Set default size units. + var SIZE_UNITS = [null, "kb", "mb", "gb", "tb"]; + + // Set meridian. + var MERIDIAN = ["AM", "PM"]; + I18n.reset = function() { // Set default locale. This locale will be used when fallback is enabled and // the translation doesn't exist in a particular locale. this.defaultLocale = "en"; @@ -195,31 +241,32 @@ // // I18n.prepareOptions({name: "John Doe"}, {name: "Mary Doe", role: "user"}); // #=> {name: "John Doe", role: "user"} // I18n.prepareOptions = function() { - var args = Array.prototype.slice.call(arguments) + var args = slice.call(arguments) , options = {} + , subject ; - for (var i = 0, count = args.length; i < count; i++) { - var o = args.shift(); + while (args.length) { + subject = args.shift(); - if (typeof(o) != "object") { + if (typeof(subject) != "object") { continue; } - for (var attr in o) { - if (!o.hasOwnProperty(attr)) { + for (var attr in subject) { + if (!subject.hasOwnProperty(attr)) { continue; } if (this.isSet(options[attr])) { continue; } - options[attr] = o[attr]; + options[attr] = subject[attr]; } } return options; }; @@ -305,16 +352,14 @@ return this.interpolate(message, options); }; // Return a missing translation message for the given parameters. I18n.missingTranslation = function(scope) { - var message = '[missing "' + this.currentLocale(); + var message = '[missing "'; - for (var i = 0; i < arguments.length; i++) { - message += "." + arguments[i]; - } - + message += this.currentLocale() + "."; + message += slice.call(arguments).join("."); message += '" translation]'; return message; }; @@ -331,11 +376,11 @@ // I18n.toNumber = function(number, options) { options = this.prepareOptions( options , this.lookup("number.format") - , {precision: 3, separator: ".", delimiter: ",", strip_insignificant_zeros: false} + , NUMBER_FORMAT ); var negative = number < 0 , string = Math.abs(number).toFixed(options.precision).toString() , parts = string.split(".") @@ -387,11 +432,11 @@ I18n.toCurrency = function(number, options) { options = this.prepareOptions( options , this.lookup("number.currency.format") , this.lookup("number.format") - , {unit: "$", precision: 2, format: "%u%n", delimiter: ",", separator: "."} + , CURRENCY_FORMAT ); number = this.toNumber(number, options); number = options.format .replace("%u", options.unit) @@ -509,20 +554,15 @@ // I18n.strftime = function(date, format) { var options = this.lookup("date"); if (!options) { - options = { - day_names: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"] - , abbr_day_names: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"] - , month_names: [null, "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"] - , abbr_month_names: [null, "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] - } + options = DAYS_AND_MONTHS; } if (!options.meridian) { - options.meridian = ["AM", "PM"]; + options.meridian = MERIDIAN; } var weekDay = date.getDay() , day = date.getDate() , year = date.getFullYear() @@ -542,14 +582,10 @@ hour12 = hour12 - 12; } else if (hour12 === 0) { hour12 = 12; } - var padding = function(number) { - return ("0" + number.toString()).substr(-2); - }; - format = format.replace("%a", options.abbr_day_names[weekDay]); format = format.replace("%A", options.day_names[weekDay]); format = format.replace("%b", options.abbr_month_names[month]); format = format.replace("%B", options.month_names[month]); format = format.replace("%d", padding(day)); @@ -596,11 +632,11 @@ I18n.toPercentage = function(number, options) { options = this.prepareOptions( options , this.lookup("number.percentage.format") , this.lookup("number.format") - , {precision: 3, separator: ".", delimiter: ""} + , PERCENTAGE_FORMAT ); number = this.toNumber(number, options); return number + "%"; }; @@ -621,11 +657,11 @@ if (iterations === 0) { unit = this.t("number.human.storage_units.units.byte", {count: size}); precision = 0; } else { - unit = this.t("number.human.storage_units.units." + [null, "kb", "mb", "gb", "tb"][iterations]); + unit = this.t("number.human.storage_units.units." + SIZE_UNITS[iterations]); precision = (size - Math.floor(size) === 0) ? 0 : 1; } options = this.prepareOptions( options @@ -643,6 +679,6 @@ // Set aliases, so we can save some typing. I18n.t = I18n.translate; I18n.l = I18n.localize; I18n.p = I18n.pluralize; -})(typeof(exports) === "undefined" ? {} : exports); +})(typeof(exports) === "undefined" ? (this.I18n = {}) : exports);