This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/discourse/initializers/localization.js.es6
Régis Hanol a2c04be718 FIX: eradicate I18n fallback issues 💣
FIX: client's translation overrides were not working when the current locale was missing a key
FIX: ExtraLocalesController.show was not properly handling multiple translations
FIX: JsLocaleHelper#output_locale was not properly handling multiple translations

FIX: ExtraLocalesController.show's spec which was randomly failing
FIX: JsLocaleHelper#output_locale was muting cached translations hashes

REFACTOR: move 'enableVerboseLocalization' to the 'localization' initializer
REFACTOR: remove unused I18n.js methods (getFallbacks, localize, parseDate, toTime, strftime, toCurrency, toPercentage)
REFACTOR: remove all I18n.pluralizationRules and instead use MessageFormat's pluralization rules

TEST: add tests for localization initializer
TEST: add tests for I18n.js
2017-02-24 11:31:21 +01:00

63 lines
1.6 KiB
JavaScript

import PreloadStore from 'preload-store';
export default {
name: 'localization',
after: 'inject-objects',
enableVerboseLocalization() {
let counter = 0;
let keys = {};
let t = I18n.t;
I18n.noFallbacks = true;
I18n.t = I18n.translate = function(scope, value){
let current = keys[scope];
if (!current) {
current = keys[scope] = ++counter;
let message = "Translation #" + current + ": " + scope;
if (!_.isEmpty(value)) {
message += ", parameters: " + JSON.stringify(value);
}
Em.Logger.info(message);
}
return t.apply(I18n, [scope, value]) + " (#" + current + ")";
};
},
initialize(container) {
const siteSettings = container.lookup('site-settings:main');
if (siteSettings.verbose_localization) {
this.enableVerboseLocalization();
}
// Merge any overrides into our object
const overrides = PreloadStore.get('translationOverrides') || {};
Object.keys(overrides).forEach(k => {
const v = overrides[k];
// Special case: Message format keys are functions
if (/_MF$/.test(k)) {
k = k.replace(/^[a-z_]*js\./, '');
I18n._compiledMFs[k] = new Function('transKey', `return (${v})(transKey);`);
return;
}
k = k.replace('admin_js', 'js');
const segs = k.split('.');
let node = I18n.translations[I18n.locale];
let i = 0;
for (; i < segs.length - 1; i++) {
if (!(segs[i] in node)) node[segs[i]] = {};
node = node[segs[i]];
}
node[segs[segs.length-1]] = v;
});
}
};