mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-11-23 23:58:31 -05:00
JSHint i18n.js
- Mark the I18n object as an overridable global - Normalize comma first style rather than turning on lax comma - Add missing semicolons - Remove unnecessary semicolons - Fix options overloading in “lookup” - Use strict comparison for typeof checks and remove unnecessary parens
This commit is contained in:
parent
62d5a10873
commit
635c0dbf7a
2 changed files with 57 additions and 62 deletions
|
@ -1,5 +1,4 @@
|
||||||
app/assets/javascripts/defer/html-sanitizer-bundle.js
|
app/assets/javascripts/defer/html-sanitizer-bundle.js
|
||||||
app/assets/javascripts/locales/
|
|
||||||
lib/autospec/run-qunit.js
|
lib/autospec/run-qunit.js
|
||||||
lib/headless-ember.js
|
lib/headless-ember.js
|
||||||
lib/javascripts/locale/
|
lib/javascripts/locale/
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
/*global I18n:true */
|
||||||
|
|
||||||
// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf
|
// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf
|
||||||
if (!Array.prototype.indexOf) {
|
if (!Array.prototype.indexOf) {
|
||||||
Array.prototype.indexOf = function (searchElement, fromIndex) {
|
Array.prototype.indexOf = function (searchElement, fromIndex) {
|
||||||
|
@ -52,7 +54,7 @@ I18n.fallbackRules = {};
|
||||||
|
|
||||||
I18n.pluralizationRules = {
|
I18n.pluralizationRules = {
|
||||||
en: function (n) {
|
en: function (n) {
|
||||||
return n == 0 ? ["zero", "none", "other"] : n == 1 ? "one" : "other";
|
return n === 0 ? ["zero", "none", "other"] : n === 1 ? "one" : "other";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -60,8 +62,8 @@ I18n.getFallbacks = function(locale) {
|
||||||
if (locale === I18n.defaultLocale) {
|
if (locale === I18n.defaultLocale) {
|
||||||
return [];
|
return [];
|
||||||
} else if (!I18n.fallbackRules[locale]) {
|
} else if (!I18n.fallbackRules[locale]) {
|
||||||
var rules = []
|
var rules = [],
|
||||||
, components = locale.split("-");
|
components = locale.split("-");
|
||||||
|
|
||||||
for (var l = 1; l < components.length; l++) {
|
for (var l = 1; l < components.length; l++) {
|
||||||
rules.push(components.slice(0, l).join("-"));
|
rules.push(components.slice(0, l).join("-"));
|
||||||
|
@ -73,23 +75,23 @@ I18n.getFallbacks = function(locale) {
|
||||||
}
|
}
|
||||||
|
|
||||||
return I18n.fallbackRules[locale];
|
return I18n.fallbackRules[locale];
|
||||||
}
|
};
|
||||||
|
|
||||||
I18n.isValidNode = function(obj, node, undefined) {
|
I18n.isValidNode = function(obj, node, undefined) {
|
||||||
return obj[node] !== null && obj[node] !== undefined;
|
return obj[node] !== null && obj[node] !== undefined;
|
||||||
};
|
};
|
||||||
|
|
||||||
I18n.lookup = function(scope, options) {
|
I18n.lookup = function(scope, options) {
|
||||||
var options = options || {}
|
options = options || {};
|
||||||
, lookupInitialScope = scope
|
var lookupInitialScope = scope,
|
||||||
, translations = this.prepareOptions(I18n.translations)
|
translations = this.prepareOptions(I18n.translations),
|
||||||
, locale = options.locale || I18n.currentLocale()
|
locale = options.locale || I18n.currentLocale(),
|
||||||
, messages = translations[locale] || {}
|
messages = translations[locale] || {},
|
||||||
, options = this.prepareOptions(options)
|
currentScope;
|
||||||
, currentScope
|
|
||||||
;
|
|
||||||
|
|
||||||
if (typeof(scope) == "object") {
|
options = this.prepareOptions(options);
|
||||||
|
|
||||||
|
if (typeof scope === "object") {
|
||||||
scope = scope.join(this.defaultSeparator);
|
scope = scope.join(this.defaultSeparator);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -130,10 +132,9 @@ I18n.lookup = function(scope, options) {
|
||||||
// #=> {name: "John Doe", role: "user"}
|
// #=> {name: "John Doe", role: "user"}
|
||||||
//
|
//
|
||||||
I18n.prepareOptions = function() {
|
I18n.prepareOptions = function() {
|
||||||
var options = {}
|
var options = {},
|
||||||
, opts
|
opts,
|
||||||
, count = arguments.length
|
count = arguments.length;
|
||||||
;
|
|
||||||
|
|
||||||
for (var i = 0; i < count; i++) {
|
for (var i = 0; i < count; i++) {
|
||||||
opts = arguments[i];
|
opts = arguments[i];
|
||||||
|
@ -154,11 +155,10 @@ I18n.prepareOptions = function() {
|
||||||
|
|
||||||
I18n.interpolate = function(message, options) {
|
I18n.interpolate = function(message, options) {
|
||||||
options = this.prepareOptions(options);
|
options = this.prepareOptions(options);
|
||||||
var matches = message.match(this.PLACEHOLDER)
|
var matches = message.match(this.PLACEHOLDER),
|
||||||
, placeholder
|
placeholder,
|
||||||
, value
|
value,
|
||||||
, name
|
name;
|
||||||
;
|
|
||||||
|
|
||||||
if (!matches) {
|
if (!matches) {
|
||||||
return message;
|
return message;
|
||||||
|
@ -185,8 +185,8 @@ I18n.translate = function(scope, options) {
|
||||||
var translation = this.lookup(scope, options);
|
var translation = this.lookup(scope, options);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (typeof(translation) == "object") {
|
if (typeof translation === "object") {
|
||||||
if (typeof(options.count) == "number") {
|
if (typeof options.count === "number") {
|
||||||
return this.pluralize(options.count, scope, options);
|
return this.pluralize(options.count, scope, options);
|
||||||
} else {
|
} else {
|
||||||
return translation;
|
return translation;
|
||||||
|
@ -221,9 +221,9 @@ I18n.parseDate = function(date) {
|
||||||
var matches, convertedDate;
|
var matches, convertedDate;
|
||||||
|
|
||||||
// we have a date, so just return it.
|
// we have a date, so just return it.
|
||||||
if (typeof(date) == "object") {
|
if (typeof date === "object") {
|
||||||
return date;
|
return date;
|
||||||
};
|
}
|
||||||
|
|
||||||
// it matches the following formats:
|
// it matches the following formats:
|
||||||
// yyyy-mm-dd
|
// yyyy-mm-dd
|
||||||
|
@ -247,14 +247,14 @@ I18n.parseDate = function(date) {
|
||||||
} else {
|
} else {
|
||||||
convertedDate = new Date(matches[1], matches[2], matches[3], matches[4], matches[5], matches[6]);
|
convertedDate = new Date(matches[1], matches[2], matches[3], matches[4], matches[5], matches[6]);
|
||||||
}
|
}
|
||||||
} else if (typeof(date) == "number") {
|
} else if (typeof date === "number") {
|
||||||
// UNIX timestamp
|
// UNIX timestamp
|
||||||
convertedDate = new Date();
|
convertedDate = new Date();
|
||||||
convertedDate.setTime(date);
|
convertedDate.setTime(date);
|
||||||
} else if (date.match(/\d+ \d+:\d+:\d+ [+-]\d+ \d+/)) {
|
} else if (date.match(/\d+ \d+:\d+:\d+ [+-]\d+ \d+/)) {
|
||||||
// a valid javascript format with timezone info
|
// a valid javascript format with timezone info
|
||||||
convertedDate = new Date();
|
convertedDate = new Date();
|
||||||
convertedDate.setTime(Date.parse(date))
|
convertedDate.setTime(Date.parse(date));
|
||||||
} else {
|
} else {
|
||||||
// an arbitrary javascript string
|
// an arbitrary javascript string
|
||||||
convertedDate = new Date();
|
convertedDate = new Date();
|
||||||
|
@ -265,9 +265,8 @@ I18n.parseDate = function(date) {
|
||||||
};
|
};
|
||||||
|
|
||||||
I18n.toTime = function(scope, d) {
|
I18n.toTime = function(scope, d) {
|
||||||
var date = this.parseDate(d)
|
var date = this.parseDate(d),
|
||||||
, format = this.lookup(scope)
|
format = this.lookup(scope);
|
||||||
;
|
|
||||||
|
|
||||||
if (date.toString().match(/invalid/i)) {
|
if (date.toString().match(/invalid/i)) {
|
||||||
return date.toString();
|
return date.toString();
|
||||||
|
@ -289,20 +288,19 @@ I18n.strftime = function(date, format) {
|
||||||
|
|
||||||
options.meridian = options.meridian || ["AM", "PM"];
|
options.meridian = options.meridian || ["AM", "PM"];
|
||||||
|
|
||||||
var weekDay = date.getDay()
|
var weekDay = date.getDay(),
|
||||||
, day = date.getDate()
|
day = date.getDate(),
|
||||||
, year = date.getFullYear()
|
year = date.getFullYear(),
|
||||||
, month = date.getMonth() + 1
|
month = date.getMonth() + 1,
|
||||||
, hour = date.getHours()
|
hour = date.getHours(),
|
||||||
, hour12 = hour
|
hour12 = hour,
|
||||||
, meridian = hour > 11 ? 1 : 0
|
meridian = hour > 11 ? 1 : 0,
|
||||||
, secs = date.getSeconds()
|
secs = date.getSeconds(),
|
||||||
, mins = date.getMinutes()
|
mins = date.getMinutes(),
|
||||||
, offset = date.getTimezoneOffset()
|
offset = date.getTimezoneOffset(),
|
||||||
, absOffsetHours = Math.floor(Math.abs(offset / 60))
|
absOffsetHours = Math.floor(Math.abs(offset / 60)),
|
||||||
, absOffsetMinutes = Math.abs(offset) - (absOffsetHours * 60)
|
absOffsetMinutes = Math.abs(offset) - (absOffsetHours * 60),
|
||||||
, timezoneoffset = (offset > 0 ? "-" : "+") + (absOffsetHours.toString().length < 2 ? "0" + absOffsetHours : absOffsetHours) + (absOffsetMinutes.toString().length < 2 ? "0" + absOffsetMinutes : absOffsetMinutes)
|
timezoneoffset = (offset > 0 ? "-" : "+") + (absOffsetHours.toString().length < 2 ? "0" + absOffsetHours : absOffsetHours) + (absOffsetMinutes.toString().length < 2 ? "0" + absOffsetMinutes : absOffsetMinutes);
|
||||||
;
|
|
||||||
|
|
||||||
if (hour12 > 12) {
|
if (hour12 > 12) {
|
||||||
hour12 = hour12 - 12;
|
hour12 = hour12 - 12;
|
||||||
|
@ -350,13 +348,12 @@ I18n.toNumber = function(number, options) {
|
||||||
{precision: 3, separator: ".", delimiter: ",", strip_insignificant_zeros: false}
|
{precision: 3, separator: ".", delimiter: ",", strip_insignificant_zeros: false}
|
||||||
);
|
);
|
||||||
|
|
||||||
var negative = number < 0
|
var negative = number < 0,
|
||||||
, string = Math.abs(number).toFixed(options.precision).toString()
|
string = Math.abs(number).toFixed(options.precision).toString(),
|
||||||
, parts = string.split(".")
|
parts = string.split("."),
|
||||||
, precision
|
precision,
|
||||||
, buffer = []
|
buffer = [],
|
||||||
, formattedNumber
|
formattedNumber;
|
||||||
;
|
|
||||||
|
|
||||||
number = parts[0];
|
number = parts[0];
|
||||||
precision = parts[1];
|
precision = parts[1];
|
||||||
|
@ -378,8 +375,8 @@ I18n.toNumber = function(number, options) {
|
||||||
|
|
||||||
if (options.strip_insignificant_zeros) {
|
if (options.strip_insignificant_zeros) {
|
||||||
var regex = {
|
var regex = {
|
||||||
separator: new RegExp(options.separator.replace(/\./, "\\.") + "$")
|
separator: new RegExp(options.separator.replace(/\./, "\\.") + "$"),
|
||||||
, zeros: /0+$/
|
zeros: /0+$/
|
||||||
};
|
};
|
||||||
|
|
||||||
formattedNumber = formattedNumber
|
formattedNumber = formattedNumber
|
||||||
|
@ -409,12 +406,11 @@ I18n.toCurrency = function(number, options) {
|
||||||
};
|
};
|
||||||
|
|
||||||
I18n.toHumanSize = function(number, options) {
|
I18n.toHumanSize = function(number, options) {
|
||||||
var kb = 1024
|
var kb = 1024,
|
||||||
, size = number
|
size = number,
|
||||||
, iterations = 0
|
iterations = 0,
|
||||||
, unit
|
unit,
|
||||||
, precision
|
precision;
|
||||||
;
|
|
||||||
|
|
||||||
while (size >= kb && iterations < 4) {
|
while (size >= kb && iterations < 4) {
|
||||||
size = size / kb;
|
size = size / kb;
|
||||||
|
@ -480,7 +476,7 @@ I18n.pluralize = function(count, scope, options) {
|
||||||
|
|
||||||
var pluralizer = this.pluralizer(this.currentLocale());
|
var pluralizer = this.pluralizer(this.currentLocale());
|
||||||
var key = pluralizer(Math.abs(count));
|
var key = pluralizer(Math.abs(count));
|
||||||
var keys = ((typeof key == "object") && (key instanceof Array)) ? key : [key];
|
var keys = ((typeof key === "object") && (key instanceof Array)) ? key : [key];
|
||||||
|
|
||||||
var message = this.findAndTranslateValidNode(keys, translation);
|
var message = this.findAndTranslateValidNode(keys, translation);
|
||||||
if (message == null) message = this.missingTranslation(scope, keys[0]);
|
if (message == null) message = this.missingTranslation(scope, keys[0]);
|
||||||
|
|
Loading…
Reference in a new issue