2014-03-11 15:01:12 +08:00
|
|
|
/*global I18n:true */
|
|
|
|
|
2013-03-01 03:31:39 +08:00
|
|
|
// Instantiate the object
|
|
|
|
var I18n = I18n || {};
|
|
|
|
|
|
|
|
// Set default locale to english
|
|
|
|
I18n.defaultLocale = "en";
|
|
|
|
|
2017-02-24 18:31:21 +08:00
|
|
|
// Set default pluralization rule
|
2013-03-01 03:31:39 +08:00
|
|
|
I18n.pluralizationRules = {
|
2014-04-15 19:59:05 +08:00
|
|
|
en: function(n) {
|
2014-03-11 15:01:12 +08:00
|
|
|
return n === 0 ? ["zero", "none", "other"] : n === 1 ? "one" : "other";
|
2013-03-01 03:31:39 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-02-24 18:31:21 +08:00
|
|
|
// Set current locale to null
|
|
|
|
I18n.locale = null;
|
2018-01-25 19:09:18 +08:00
|
|
|
I18n.fallbackLocale = null;
|
2013-03-01 03:31:39 +08:00
|
|
|
|
2017-02-24 18:31:21 +08:00
|
|
|
// Set the placeholder format. Accepts `{{placeholder}}` and `%{placeholder}`.
|
|
|
|
I18n.PLACEHOLDER = /(?:\{\{|%\{)(.*?)(?:\}\}?)/gm;
|
2013-03-01 03:31:39 +08:00
|
|
|
|
2017-02-24 18:31:21 +08:00
|
|
|
I18n.SEPARATOR = ".";
|
2013-03-01 03:31:39 +08:00
|
|
|
|
2017-02-24 18:31:21 +08:00
|
|
|
I18n.noFallbacks = false;
|
2013-03-01 03:31:39 +08:00
|
|
|
|
|
|
|
I18n.isValidNode = function(obj, node, undefined) {
|
|
|
|
return obj[node] !== null && obj[node] !== undefined;
|
|
|
|
};
|
|
|
|
|
|
|
|
I18n.lookup = function(scope, options) {
|
2014-03-11 15:01:12 +08:00
|
|
|
options = options || {};
|
2017-02-24 18:31:21 +08:00
|
|
|
|
2017-11-25 03:11:01 +08:00
|
|
|
var translations = this.prepareOptions(I18n.translations),
|
2014-03-11 15:01:12 +08:00
|
|
|
locale = options.locale || I18n.currentLocale(),
|
|
|
|
messages = translations[locale] || {},
|
|
|
|
currentScope;
|
2013-03-01 03:31:39 +08:00
|
|
|
|
2014-03-11 15:01:12 +08:00
|
|
|
options = this.prepareOptions(options);
|
|
|
|
|
|
|
|
if (typeof scope === "object") {
|
2017-02-24 18:31:21 +08:00
|
|
|
scope = scope.join(this.SEPARATOR);
|
2013-03-01 03:31:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (options.scope) {
|
2017-02-24 18:31:21 +08:00
|
|
|
scope = options.scope.toString() + this.SEPARATOR + scope;
|
2013-03-01 03:31:39 +08:00
|
|
|
}
|
|
|
|
|
2019-05-24 03:23:31 +08:00
|
|
|
var originalScope = scope;
|
|
|
|
scope = scope.split(this.SEPARATOR);
|
2016-08-26 04:33:29 +08:00
|
|
|
|
2019-05-24 03:23:31 +08:00
|
|
|
if (scope.length > 0 && scope[0] !== "js") {
|
|
|
|
scope.unshift("js");
|
|
|
|
}
|
2013-03-01 03:31:39 +08:00
|
|
|
|
|
|
|
while (messages && scope.length > 0) {
|
|
|
|
currentScope = scope.shift();
|
|
|
|
messages = messages[currentScope];
|
|
|
|
}
|
|
|
|
|
2019-05-24 03:23:31 +08:00
|
|
|
if (messages === undefined && this.extras && this.extras[locale]) {
|
|
|
|
messages = this.extras[locale];
|
|
|
|
scope = originalScope.split(this.SEPARATOR);
|
|
|
|
|
|
|
|
while (messages && scope.length > 0) {
|
|
|
|
currentScope = scope.shift();
|
|
|
|
messages = messages[currentScope];
|
|
|
|
}
|
2017-03-01 18:30:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (messages === undefined) {
|
|
|
|
messages = options.defaultValue;
|
|
|
|
}
|
2013-03-01 03:31:39 +08:00
|
|
|
|
|
|
|
return messages;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Merge serveral hash options, checking if value is set before
|
|
|
|
// overwriting any value. The precedence is from left to right.
|
|
|
|
//
|
|
|
|
// I18n.prepareOptions({name: "John Doe"}, {name: "Mary Doe", role: "user"});
|
|
|
|
// #=> {name: "John Doe", role: "user"}
|
|
|
|
//
|
|
|
|
I18n.prepareOptions = function() {
|
2014-03-11 15:01:12 +08:00
|
|
|
var options = {},
|
|
|
|
opts,
|
|
|
|
count = arguments.length;
|
2013-03-01 03:31:39 +08:00
|
|
|
|
|
|
|
for (var i = 0; i < count; i++) {
|
|
|
|
opts = arguments[i];
|
|
|
|
|
|
|
|
if (!opts) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var key in opts) {
|
|
|
|
if (!this.isValidNode(options, key)) {
|
|
|
|
options[key] = opts[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return options;
|
|
|
|
};
|
|
|
|
|
|
|
|
I18n.interpolate = function(message, options) {
|
|
|
|
options = this.prepareOptions(options);
|
2017-02-24 18:31:21 +08:00
|
|
|
|
2014-03-11 15:01:12 +08:00
|
|
|
var matches = message.match(this.PLACEHOLDER),
|
|
|
|
placeholder,
|
|
|
|
value,
|
|
|
|
name;
|
2013-03-01 03:31:39 +08:00
|
|
|
|
2017-02-24 18:31:21 +08:00
|
|
|
if (!matches) { return message; }
|
2013-03-01 03:31:39 +08:00
|
|
|
|
|
|
|
for (var i = 0; placeholder = matches[i]; i++) {
|
|
|
|
name = placeholder.replace(this.PLACEHOLDER, "$1");
|
|
|
|
|
|
|
|
value = options[name];
|
|
|
|
|
|
|
|
if (!this.isValidNode(options, name)) {
|
|
|
|
value = "[missing " + placeholder + " value]";
|
|
|
|
}
|
|
|
|
|
2013-06-21 00:47:43 +08:00
|
|
|
var regex = new RegExp(placeholder.replace(/\{/gm, "\\{").replace(/\}/gm, "\\}"));
|
2013-03-01 03:31:39 +08:00
|
|
|
message = message.replace(regex, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return message;
|
|
|
|
};
|
|
|
|
|
|
|
|
I18n.translate = function(scope, options) {
|
|
|
|
options = this.prepareOptions(options);
|
2016-08-26 04:33:29 +08:00
|
|
|
|
2013-03-01 03:31:39 +08:00
|
|
|
var translation = this.lookup(scope, options);
|
2017-02-24 18:31:21 +08:00
|
|
|
|
|
|
|
if (!this.noFallbacks) {
|
2018-01-25 19:09:18 +08:00
|
|
|
if (!translation && this.fallbackLocale) {
|
|
|
|
options.locale = this.fallbackLocale;
|
|
|
|
translation = this.lookup(scope, options);
|
|
|
|
}
|
2017-02-24 18:31:21 +08:00
|
|
|
if (!translation && this.currentLocale() !== this.defaultLocale) {
|
|
|
|
options.locale = this.defaultLocale;
|
|
|
|
translation = this.lookup(scope, options);
|
|
|
|
}
|
|
|
|
if (!translation && this.currentLocale() !== 'en') {
|
|
|
|
options.locale = 'en';
|
|
|
|
translation = this.lookup(scope, options);
|
|
|
|
}
|
2015-07-15 23:56:46 +08:00
|
|
|
}
|
2013-03-01 03:31:39 +08:00
|
|
|
|
|
|
|
try {
|
2014-03-11 15:01:12 +08:00
|
|
|
if (typeof translation === "object") {
|
|
|
|
if (typeof options.count === "number") {
|
2017-02-28 17:02:29 +08:00
|
|
|
return this.pluralize(translation, scope, options);
|
2013-03-01 03:31:39 +08:00
|
|
|
} else {
|
|
|
|
return translation;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return this.interpolate(translation, options);
|
|
|
|
}
|
2013-11-04 18:52:12 +08:00
|
|
|
} catch (error) {
|
2013-03-01 03:31:39 +08:00
|
|
|
return this.missingTranslation(scope);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
I18n.toNumber = function(number, options) {
|
|
|
|
options = this.prepareOptions(
|
|
|
|
options,
|
|
|
|
this.lookup("number.format"),
|
2017-02-24 18:31:21 +08:00
|
|
|
{precision: 3, separator: this.SEPARATOR, delimiter: ",", strip_insignificant_zeros: false}
|
2013-03-01 03:31:39 +08:00
|
|
|
);
|
|
|
|
|
2014-03-11 15:01:12 +08:00
|
|
|
var negative = number < 0,
|
|
|
|
string = Math.abs(number).toFixed(options.precision).toString(),
|
2017-02-24 18:31:21 +08:00
|
|
|
parts = string.split(this.SEPARATOR),
|
2014-03-11 15:01:12 +08:00
|
|
|
precision,
|
|
|
|
buffer = [],
|
|
|
|
formattedNumber;
|
2013-03-01 03:31:39 +08:00
|
|
|
|
|
|
|
number = parts[0];
|
|
|
|
precision = parts[1];
|
|
|
|
|
|
|
|
while (number.length > 0) {
|
|
|
|
buffer.unshift(number.substr(Math.max(0, number.length - 3), 3));
|
|
|
|
number = number.substr(0, number.length -3);
|
|
|
|
}
|
|
|
|
|
|
|
|
formattedNumber = buffer.join(options.delimiter);
|
|
|
|
|
|
|
|
if (options.precision > 0) {
|
|
|
|
formattedNumber += options.separator + parts[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (negative) {
|
|
|
|
formattedNumber = "-" + formattedNumber;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.strip_insignificant_zeros) {
|
|
|
|
var regex = {
|
2014-03-11 15:01:12 +08:00
|
|
|
separator: new RegExp(options.separator.replace(/\./, "\\.") + "$"),
|
|
|
|
zeros: /0+$/
|
2013-03-01 03:31:39 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
formattedNumber = formattedNumber
|
|
|
|
.replace(regex.zeros, "")
|
|
|
|
.replace(regex.separator, "")
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
return formattedNumber;
|
|
|
|
};
|
|
|
|
|
|
|
|
I18n.toHumanSize = function(number, options) {
|
2014-03-11 15:01:12 +08:00
|
|
|
var kb = 1024,
|
|
|
|
size = number,
|
|
|
|
iterations = 0,
|
|
|
|
unit,
|
|
|
|
precision;
|
2013-03-01 03:31:39 +08:00
|
|
|
|
|
|
|
while (size >= kb && iterations < 4) {
|
|
|
|
size = size / kb;
|
|
|
|
iterations += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
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]);
|
|
|
|
precision = (size - Math.floor(size) === 0) ? 0 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
options = this.prepareOptions(
|
|
|
|
options,
|
|
|
|
{precision: precision, format: "%n%u", delimiter: ""}
|
|
|
|
);
|
|
|
|
|
|
|
|
number = this.toNumber(size, options);
|
|
|
|
number = options.format
|
|
|
|
.replace("%u", unit)
|
|
|
|
.replace("%n", number)
|
|
|
|
;
|
|
|
|
|
|
|
|
return number;
|
|
|
|
};
|
|
|
|
|
|
|
|
I18n.pluralizer = function(locale) {
|
2013-06-21 00:47:43 +08:00
|
|
|
var pluralizer = this.pluralizationRules[locale];
|
2013-03-01 03:31:39 +08:00
|
|
|
if (pluralizer !== undefined) return pluralizer;
|
|
|
|
return this.pluralizationRules["en"];
|
|
|
|
};
|
|
|
|
|
|
|
|
I18n.findAndTranslateValidNode = function(keys, translation) {
|
2013-06-21 00:47:43 +08:00
|
|
|
for (var i = 0; i < keys.length; i++) {
|
|
|
|
var key = keys[i];
|
2013-03-01 03:31:39 +08:00
|
|
|
if (this.isValidNode(translation, key)) return translation[key];
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
2017-02-28 17:02:29 +08:00
|
|
|
I18n.pluralize = function(translation, scope, options) {
|
2013-03-01 03:31:39 +08:00
|
|
|
options = this.prepareOptions(options);
|
2017-02-28 17:02:29 +08:00
|
|
|
var count = options.count.toString();
|
2013-03-01 03:31:39 +08:00
|
|
|
|
2017-02-28 17:02:29 +08:00
|
|
|
var pluralizer = this.pluralizer(options.locale || this.currentLocale());
|
2013-06-21 00:47:43 +08:00
|
|
|
var key = pluralizer(Math.abs(count));
|
2014-03-11 15:01:12 +08:00
|
|
|
var keys = ((typeof key === "object") && (key instanceof Array)) ? key : [key];
|
2013-03-01 03:31:39 +08:00
|
|
|
|
2013-11-04 18:52:12 +08:00
|
|
|
var message = this.findAndTranslateValidNode(keys, translation);
|
2013-03-01 03:31:39 +08:00
|
|
|
if (message == null) message = this.missingTranslation(scope, keys[0]);
|
|
|
|
|
|
|
|
return this.interpolate(message, options);
|
|
|
|
};
|
|
|
|
|
2013-11-04 18:52:12 +08:00
|
|
|
I18n.missingTranslation = function(scope, key) {
|
2017-02-24 18:31:21 +08:00
|
|
|
var message = '[' + this.currentLocale() + this.SEPARATOR + scope;
|
|
|
|
if (key) { message += this.SEPARATOR + key; }
|
2013-11-04 18:52:12 +08:00
|
|
|
return message + ']';
|
2013-03-01 03:31:39 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
I18n.currentLocale = function() {
|
2017-02-24 18:31:21 +08:00
|
|
|
return I18n.locale || I18n.defaultLocale;
|
2013-03-01 03:31:39 +08:00
|
|
|
};
|
|
|
|
|
2018-01-26 06:29:51 +08:00
|
|
|
I18n.enableVerboseLocalization = function() {
|
|
|
|
var counter = 0;
|
|
|
|
var keys = {};
|
|
|
|
var t = I18n.t;
|
|
|
|
|
|
|
|
I18n.noFallbacks = true;
|
|
|
|
|
|
|
|
I18n.t = I18n.translate = function(scope, value){
|
|
|
|
var current = keys[scope];
|
|
|
|
if (!current) {
|
|
|
|
current = keys[scope] = ++counter;
|
|
|
|
var message = "Translation #" + current + ": " + scope;
|
|
|
|
if (!_.isEmpty(value)) {
|
|
|
|
message += ", parameters: " + JSON.stringify(value);
|
|
|
|
}
|
2019-01-12 00:54:23 +08:00
|
|
|
Ember.Logger.info(message);
|
2018-01-26 06:29:51 +08:00
|
|
|
}
|
|
|
|
return t.apply(I18n, [scope, value]) + " (#" + current + ")";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
I18n.enableVerboseLocalizationSession = function() {
|
|
|
|
sessionStorage.setItem("verbose_localization", "true");
|
|
|
|
I18n.enableVerboseLocalization();
|
|
|
|
|
|
|
|
return 'Verbose localization is enabled. Close the browser tab to turn it off. Reload the page to see the translation keys.';
|
|
|
|
};
|
|
|
|
|
2013-03-01 03:31:39 +08:00
|
|
|
// shortcuts
|
|
|
|
I18n.t = I18n.translate;
|