Run prettier on a couple of files

This commit is contained in:
Robin Ward 2020-03-12 14:07:38 -04:00
parent c61ebc9ba6
commit 3339b91079
2 changed files with 59 additions and 50 deletions

View File

@ -1,4 +1,4 @@
// ensure Discourse is added as a global // ensure Discourse is added as a global
(function() { (function() {
window.Discourse = requirejs('discourse').default; window.Discourse = requirejs("discourse").default;
})(); })();

View File

@ -32,9 +32,9 @@ I18n.lookup = function(scope, options) {
options = options || {}; options = options || {};
var translations = this.prepareOptions(I18n.translations), var translations = this.prepareOptions(I18n.translations),
locale = options.locale || I18n.currentLocale(), locale = options.locale || I18n.currentLocale(),
messages = translations[locale] || {}, messages = translations[locale] || {},
currentScope; currentScope;
options = this.prepareOptions(options); options = this.prepareOptions(options);
@ -83,8 +83,8 @@ I18n.lookup = function(scope, options) {
// //
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];
@ -107,13 +107,15 @@ 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) { return message; } if (!matches) {
return message;
}
for (var i = 0; placeholder = matches[i]; i++) { for (var i = 0; (placeholder = matches[i]); i++) {
name = placeholder.replace(this.PLACEHOLDER, "$1"); name = placeholder.replace(this.PLACEHOLDER, "$1");
if (typeof options[name] === "string") { if (typeof options[name] === "string") {
@ -130,7 +132,9 @@ I18n.interpolate = function(message, options) {
value = "[missing " + placeholder + " value]"; value = "[missing " + placeholder + " value]";
} }
var regex = new RegExp(placeholder.replace(/\{/gm, "\\{").replace(/\}/gm, "\\}")); var regex = new RegExp(
placeholder.replace(/\{/gm, "\\{").replace(/\}/gm, "\\}")
);
message = message.replace(regex, value); message = message.replace(regex, value);
} }
@ -157,8 +161,8 @@ I18n.translate = function(scope, options) {
translation = this.findTranslation(scope, options); translation = this.findTranslation(scope, options);
} }
if (!translation && this.currentLocale() !== 'en') { if (!translation && this.currentLocale() !== "en") {
options.locale = 'en'; options.locale = "en";
translation = this.findTranslation(scope, options); translation = this.findTranslation(scope, options);
} }
} }
@ -181,25 +185,28 @@ I18n.findTranslation = function(scope, options) {
}; };
I18n.toNumber = function(number, options) { I18n.toNumber = function(number, options) {
options = this.prepareOptions( options = this.prepareOptions(options, this.lookup("number.format"), {
options, precision: 3,
this.lookup("number.format"), separator: this.SEPARATOR,
{precision: 3, separator: this.SEPARATOR, delimiter: ",", strip_insignificant_zeros: false} 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)
parts = string.split(this.SEPARATOR), .toFixed(options.precision)
precision, .toString(),
buffer = [], parts = string.split(this.SEPARATOR),
formattedNumber; precision,
buffer = [],
formattedNumber;
number = parts[0]; number = parts[0];
precision = parts[1]; precision = parts[1];
while (number.length > 0) { while (number.length > 0) {
buffer.unshift(number.substr(Math.max(0, number.length - 3), 3)); buffer.unshift(number.substr(Math.max(0, number.length - 3), 3));
number = number.substr(0, number.length -3); number = number.substr(0, number.length - 3);
} }
formattedNumber = buffer.join(options.delimiter); formattedNumber = buffer.join(options.delimiter);
@ -214,14 +221,13 @@ 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
.replace(regex.zeros, "") .replace(regex.zeros, "")
.replace(regex.separator, "") .replace(regex.separator, "");
;
} }
return formattedNumber; return formattedNumber;
@ -229,10 +235,10 @@ I18n.toNumber = 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;
@ -240,23 +246,24 @@ I18n.toHumanSize = function(number, options) {
} }
if (iterations === 0) { if (iterations === 0) {
unit = this.t("number.human.storage_units.units.byte", {count: size}); unit = this.t("number.human.storage_units.units.byte", { count: size });
precision = 0; precision = 0;
} else { } else {
unit = this.t("number.human.storage_units.units." + [null, "kb", "mb", "gb", "tb"][iterations]); unit = this.t(
precision = (size - Math.floor(size) === 0) ? 0 : 1; "number.human.storage_units.units." +
[null, "kb", "mb", "gb", "tb"][iterations]
);
precision = size - Math.floor(size) === 0 ? 0 : 1;
} }
options = this.prepareOptions( options = this.prepareOptions(options, {
options, precision: precision,
{precision: precision, format: "%n%u", delimiter: ""} format: "%n%u",
); delimiter: ""
});
number = this.toNumber(size, options); number = this.toNumber(size, options);
number = options.format number = options.format.replace("%u", unit).replace("%n", number);
.replace("%u", unit)
.replace("%n", number)
;
return number; return number;
}; };
@ -283,7 +290,7 @@ I18n.pluralize = function(translation, scope, options) {
var pluralizer = this.pluralizer(options.locale || this.currentLocale()); var pluralizer = this.pluralizer(options.locale || 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);
@ -295,9 +302,11 @@ I18n.pluralize = function(translation, scope, options) {
}; };
I18n.missingTranslation = function(scope, key) { I18n.missingTranslation = function(scope, key) {
var message = '[' + this.currentLocale() + this.SEPARATOR + scope; var message = "[" + this.currentLocale() + this.SEPARATOR + scope;
if (key) { message += this.SEPARATOR + key; } if (key) {
return message + ']'; message += this.SEPARATOR + key;
}
return message + "]";
}; };
I18n.currentLocale = function() { I18n.currentLocale = function() {
@ -311,7 +320,7 @@ I18n.enableVerboseLocalization = function() {
I18n.noFallbacks = true; I18n.noFallbacks = true;
I18n.t = I18n.translate = function(scope, value){ I18n.t = I18n.translate = function(scope, value) {
var current = keys[scope]; var current = keys[scope];
if (!current) { if (!current) {
current = keys[scope] = ++counter; current = keys[scope] = ++counter;
@ -330,7 +339,7 @@ I18n.enableVerboseLocalizationSession = function() {
sessionStorage.setItem("verbose_localization", "true"); sessionStorage.setItem("verbose_localization", "true");
I18n.enableVerboseLocalization(); I18n.enableVerboseLocalization();
return 'Verbose localization is enabled. Close the browser tab to turn it off. Reload the page to see the translation keys.'; return "Verbose localization is enabled. Close the browser tab to turn it off. Reload the page to see the translation keys.";
}; };
// shortcuts // shortcuts