mirror of
https://github.com/discourse/discourse.git
synced 2025-02-18 09:15:19 +00:00
FIX: Handle missing plural keys on client
This commit is contained in:
parent
f88dced0b7
commit
d1228f47bb
@ -131,39 +131,47 @@ I18n.interpolate = function(message, options) {
|
|||||||
|
|
||||||
I18n.translate = function(scope, options) {
|
I18n.translate = function(scope, options) {
|
||||||
options = this.prepareOptions(options);
|
options = this.prepareOptions(options);
|
||||||
|
options.needsPluralization = typeof options.count === "number";
|
||||||
|
options.ignoreMissing = !this.noFallbacks;
|
||||||
|
|
||||||
var translation = this.lookup(scope, options);
|
var translation = this.findTranslation(scope, options);
|
||||||
|
|
||||||
if (!this.noFallbacks) {
|
if (!this.noFallbacks) {
|
||||||
if (!translation && this.fallbackLocale) {
|
if (!translation && this.fallbackLocale) {
|
||||||
options.locale = this.fallbackLocale;
|
options.locale = this.fallbackLocale;
|
||||||
translation = this.lookup(scope, options);
|
translation = this.findTranslation(scope, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
options.ignoreMissing = false;
|
||||||
|
|
||||||
if (!translation && this.currentLocale() !== this.defaultLocale) {
|
if (!translation && this.currentLocale() !== this.defaultLocale) {
|
||||||
options.locale = this.defaultLocale;
|
options.locale = this.defaultLocale;
|
||||||
translation = this.lookup(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.lookup(scope, options);
|
translation = this.findTranslation(scope, options);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (typeof translation === "object") {
|
|
||||||
if (typeof options.count === "number") {
|
|
||||||
return this.pluralize(translation, scope, options);
|
|
||||||
} else {
|
|
||||||
return translation;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return this.interpolate(translation, options);
|
return this.interpolate(translation, options);
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return this.missingTranslation(scope);
|
return this.missingTranslation(scope);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
I18n.findTranslation = function(scope, options) {
|
||||||
|
var translation = this.lookup(scope, options);
|
||||||
|
|
||||||
|
if (translation && options.needsPluralization) {
|
||||||
|
translation = this.pluralize(translation, scope, options);
|
||||||
|
}
|
||||||
|
|
||||||
|
return translation;
|
||||||
|
};
|
||||||
|
|
||||||
I18n.toNumber = function(number, options) {
|
I18n.toNumber = function(number, options) {
|
||||||
options = this.prepareOptions(
|
options = this.prepareOptions(
|
||||||
options,
|
options,
|
||||||
@ -260,6 +268,8 @@ I18n.findAndTranslateValidNode = function(keys, translation) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
I18n.pluralize = function(translation, scope, options) {
|
I18n.pluralize = function(translation, scope, options) {
|
||||||
|
if (typeof translation !== "object") return translation;
|
||||||
|
|
||||||
options = this.prepareOptions(options);
|
options = this.prepareOptions(options);
|
||||||
var count = options.count.toString();
|
var count = options.count.toString();
|
||||||
|
|
||||||
@ -268,9 +278,12 @@ I18n.pluralize = function(translation, scope, options) {
|
|||||||
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]);
|
|
||||||
|
|
||||||
return this.interpolate(message, options);
|
if (message !== null || options.ignoreMissing) {
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.missingTranslation(scope, keys[0]);
|
||||||
};
|
};
|
||||||
|
|
||||||
I18n.missingTranslation = function(scope, key) {
|
I18n.missingTranslation = function(scope, key) {
|
||||||
|
@ -2,6 +2,8 @@ QUnit.module("lib:i18n", {
|
|||||||
_locale: I18n.locale,
|
_locale: I18n.locale,
|
||||||
_fallbackLocale: I18n.fallbackLocale,
|
_fallbackLocale: I18n.fallbackLocale,
|
||||||
_translations: I18n.translations,
|
_translations: I18n.translations,
|
||||||
|
_extras: I18n.extras,
|
||||||
|
_pluralizationRules: Object.assign({}, I18n.pluralizationRules),
|
||||||
|
|
||||||
beforeEach() {
|
beforeEach() {
|
||||||
I18n.locale = "fr";
|
I18n.locale = "fr";
|
||||||
@ -34,6 +36,9 @@ QUnit.module("lib:i18n", {
|
|||||||
few: "{{count}} FEW",
|
few: "{{count}} FEW",
|
||||||
many: "{{count}} MANY",
|
many: "{{count}} MANY",
|
||||||
other: "{{count}} OTHER"
|
other: "{{count}} OTHER"
|
||||||
|
},
|
||||||
|
days: {
|
||||||
|
other: "%{count} jours"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -51,12 +56,17 @@ QUnit.module("lib:i18n", {
|
|||||||
word_count: {
|
word_count: {
|
||||||
one: "1 word",
|
one: "1 word",
|
||||||
other: "{{count}} words"
|
other: "{{count}} words"
|
||||||
|
},
|
||||||
|
days: {
|
||||||
|
one: "%{count} day",
|
||||||
|
other: "%{count} days"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// fake pluralization rules
|
// fake pluralization rules
|
||||||
|
I18n.pluralizationRules = Object.assign({}, I18n.pluralizationRules);
|
||||||
I18n.pluralizationRules.fr = function(n) {
|
I18n.pluralizationRules.fr = function(n) {
|
||||||
if (n === 0) return "zero";
|
if (n === 0) return "zero";
|
||||||
if (n === 1) return "one";
|
if (n === 1) return "one";
|
||||||
@ -71,6 +81,8 @@ QUnit.module("lib:i18n", {
|
|||||||
I18n.locale = this._locale;
|
I18n.locale = this._locale;
|
||||||
I18n.fallbackLocale = this._fallbackLocale;
|
I18n.fallbackLocale = this._fallbackLocale;
|
||||||
I18n.translations = this._translations;
|
I18n.translations = this._translations;
|
||||||
|
I18n.extras = this._extras;
|
||||||
|
I18n.pluralizationRules = this._pluralizationRules;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -181,6 +193,17 @@ QUnit.test("pluralizations", assert => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
QUnit.test("fallback", assert => {
|
QUnit.test("fallback", assert => {
|
||||||
|
assert.equal(
|
||||||
|
I18n.t("days", { count: 1 }),
|
||||||
|
"1 day",
|
||||||
|
"uses fallback locale for missing plural key"
|
||||||
|
);
|
||||||
|
assert.equal(
|
||||||
|
I18n.t("days", { count: 200 }),
|
||||||
|
"200 jours",
|
||||||
|
"uses existing French plural key"
|
||||||
|
);
|
||||||
|
|
||||||
I18n.locale = "fr_FOO";
|
I18n.locale = "fr_FOO";
|
||||||
I18n.fallbackLocale = "fr";
|
I18n.fallbackLocale = "fr";
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user