app/assets/javascripts/i18n.js in i18n-js-3.0.0.rc7 vs app/assets/javascripts/i18n.js in i18n-js-3.0.0.rc8

- old
+ new

@@ -9,13 +9,30 @@ // hello: "Hello World" // }; // // See tests for specific formatting like numbers and dates. // -;(function(I18n){ + +;(function(factory) { + if (typeof module !== 'undefined' && module.exports) { + // Node/CommonJS + module.exports = factory(this); + + } else if (typeof define === 'function' && define.amd) { + // AMD + define((function(global){ return function(){ factory(global); }})(this)); + + } else { + // Browser globals + this.I18n = factory(this); + } +}(function(global) { "use strict"; + // Use previously defined object if exists in current scope + var I18n = global && global.I18n || {}; + // Just cache the Array#slice function. var slice = Array.prototype.slice; // Apply number padding. var padding = function(number) { @@ -405,10 +422,12 @@ placeholder = matches.shift(); name = placeholder.replace(this.placeholder, "$1"); if (this.isSet(options[name])) { value = options[name].toString().replace(/\$/gm, "_#$#_"); + } else if (name in options) { + value = this.nullPlaceholder(placeholder, message); } else { value = this.missingPlaceholder(placeholder, message); } regex = new RegExp(placeholder.replace(/\{/gm, "\\{").replace(/\}/gm, "\\}")); @@ -434,11 +453,11 @@ if (!translations) { return this.missingTranslation(scope); } pluralizer = this.pluralization.get(options.locale); - keys = pluralizer(Math.abs(count)); + keys = pluralizer(count); while (keys.length) { key = keys.shift(); if (this.isSet(translations[key])) { @@ -465,10 +484,14 @@ // Return a missing placeholder message for given parameters I18n.missingPlaceholder = function(placeholder, message) { return "[missing " + placeholder + " value]"; }; + I18n.nullPlaceholder = function() { + return I18n.missingPlaceholder.apply(I18n, arguments); + }; + // Format number using localization rules. // The options will be retrieved from the `number.format` scope. // If this isn't present, then the following options will be used: // // - `precision`: `3` @@ -555,25 +578,31 @@ // If you provide a scope that matches the `/^(date|time)/` regular expression // then the `value` will be converted by using the `I18n.toTime` function. // // It will default to the value's `toString` function. // - I18n.localize = function(scope, value) { + I18n.localize = function(scope, value, options) { + options || (options = {}); + switch (scope) { case "currency": return this.toCurrency(value); case "number": scope = this.lookup("number.format"); return this.toNumber(value, scope); case "percentage": return this.toPercentage(value); default: + var localizedValue; + if (scope.match(/^(date|time)/)) { - return this.toTime(scope, value); + localizedValue = this.toTime(scope, value); } else { - return value.toString(); + localizedValue = value.toString(); } + + return this.interpolate(localizedValue, options); } }; // Parse a given `date` string into a JavaScript Date object. // This function is time zone aware. @@ -795,6 +824,8 @@ // Set aliases, so we can save some typing. I18n.t = I18n.translate; I18n.l = I18n.localize; I18n.p = I18n.pluralize; -})(typeof(exports) === "undefined" ? (this.I18n || (this.I18n = {})) : exports); + + return I18n; +}));