export function translateWithParameters (messageKey, ...parameters) {
const message = messages[messageKey];
if (message) {
- return parameters.reduce((acc, parameter, index) => (
- acc.replace(`{${index}}`, parameter)
- ));
+ return parameters.reduce((acc, parameter, index) => {
+ return acc.replace(`{${index}}`, parameter);
+ }, message);
} else {
- return `${messageKey} ${parameters.join(' ')}`;
+ return `${messageKey}.${parameters.join('.')}`;
}
}
});
}
+export function resetBundle (bundle) {
+ messages = bundle;
+}
+
export function installGlobal () {
window.t = translate;
window.tp = translateWithParameters;
--- /dev/null
+import { expect } from 'chai';
+import { resetBundle, translate, translateWithParameters } from '../../src/main/js/helpers/l10n';
+
+describe('l10n', () => {
+ afterEach(() => {
+ resetBundle({});
+ });
+
+ describe('#translate', () => {
+ it('should translate simple message', () => {
+ resetBundle({ 'my_key': 'my message' });
+ expect(translate('my_key')).to.equal('my message');
+ });
+
+ it('should translate message with composite key', () => {
+ resetBundle({ 'my.composite.message': 'my message' });
+ expect(translate('my', 'composite', 'message')).to.equal('my message');
+ expect(translate('my.composite', 'message')).to.equal('my message');
+ expect(translate('my', 'composite.message')).to.equal('my message');
+ expect(translate('my.composite.message')).to.equal('my message');
+ });
+
+ it('should not translate message but return its key', () => {
+ expect(translate('random')).to.equal('random');
+ expect(translate('random', 'key')).to.equal('random.key');
+ expect(translate('composite.random', 'key')).to.equal('composite.random.key');
+ });
+ });
+
+ describe('#translateWithParameters', () => {
+ it('should translate message with one parameter in the beginning', () => {
+ resetBundle({ 'x_apples': '{0} apples' });
+ expect(translateWithParameters('x_apples', 5)).to.equal('5 apples');
+ });
+
+ it('should translate message with one parameter in the middle', () => {
+ resetBundle({ 'x_apples': 'I have {0} apples' });
+ expect(translateWithParameters('x_apples', 5)).to.equal('I have 5 apples');
+ });
+
+ it('should translate message with one parameter in the end', () => {
+ resetBundle({ 'x_apples': 'Apples: {0}' });
+ expect(translateWithParameters('x_apples', 5)).to.equal('Apples: 5');
+ });
+
+ it('should translate message with several parameters', () => {
+ resetBundle({ 'x_apples': '{0}: I have {2} apples in my {1} baskets - {3}' });
+ expect(translateWithParameters('x_apples', 1, 2, 3, 4)).to.equal('1: I have 3 apples in my 2 baskets - 4');
+ });
+
+ it('should not translate message but return its key', () => {
+ expect(translateWithParameters('random', 5)).to.equal('random.5');
+ expect(translateWithParameters('random', 1, 2, 3)).to.equal('random.1.2.3');
+ expect(translateWithParameters('composite.random', 1, 2)).to.equal('composite.random.1.2');
+ });
+ });
+});
import { expect } from 'chai';
+import { resetBundle } from '../../src/main/js/helpers/l10n';
import { formatMeasure, formatMeasureVariation } from '../../src/main/js/helpers/measures';
ONE_DAY = HOURS_IN_DAY * ONE_HOUR;
before(function () {
- window.messages = {
+ resetBundle({
'work_duration.x_days': '{0}d',
'work_duration.x_hours': '{0}h',
'work_duration.x_minutes': '{0}min',
'metric.level.ERROR': 'Error',
'metric.level.WARN': 'Warning',
'metric.level.OK': 'Ok'
- };
- window.t = function() {
- if (!window.messages) {
- return window.translate.apply(this, arguments);
- }
- var args = Array.prototype.slice.call(arguments, 0),
- key = args.join('.');
- return window.messages[key] != null ? window.messages[key] : key;
- };
- window.tp = function () {
- var args = Array.prototype.slice.call(arguments, 0),
- key = args.shift(),
- message = window.messages[key];
- if (message) {
- args.forEach(function (p, i) {
- message = message.replace('{' + i + '}', p);
- });
- }
- return message || (key + ' ' + args.join(' '));
- };
+ });
window.SS = { hoursInDay: HOURS_IN_DAY };
});