aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStas Vilchik <vilchiks@gmail.com>2016-01-06 17:07:05 +0100
committerStas Vilchik <vilchiks@gmail.com>2016-01-06 17:07:05 +0100
commit69301bd966d7f6836158ff8cc56dee119dd7f63f (patch)
tree8899e8cccb5332b9a785d0fb92ef7b2ba21786e7
parentd0205df442e158fe30a5d5d8131dc4ccf9a9f872 (diff)
downloadsonarqube-69301bd966d7f6836158ff8cc56dee119dd7f63f.tar.gz
sonarqube-69301bd966d7f6836158ff8cc56dee119dd7f63f.zip
fix translations
-rw-r--r--server/sonar-web/src/main/js/helpers/l10n.js12
-rw-r--r--server/sonar-web/tests/helpers/l10n-test.js57
-rw-r--r--server/sonar-web/tests/helpers/measures-test.js24
3 files changed, 68 insertions, 25 deletions
diff --git a/server/sonar-web/src/main/js/helpers/l10n.js b/server/sonar-web/src/main/js/helpers/l10n.js
index e3c2c2a9b91..2b57dd45631 100644
--- a/server/sonar-web/src/main/js/helpers/l10n.js
+++ b/server/sonar-web/src/main/js/helpers/l10n.js
@@ -30,11 +30,11 @@ export function translate (...keys) {
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('.')}`;
}
}
@@ -80,6 +80,10 @@ export function requestMessages () {
});
}
+export function resetBundle (bundle) {
+ messages = bundle;
+}
+
export function installGlobal () {
window.t = translate;
window.tp = translateWithParameters;
diff --git a/server/sonar-web/tests/helpers/l10n-test.js b/server/sonar-web/tests/helpers/l10n-test.js
new file mode 100644
index 00000000000..068cb5b22e8
--- /dev/null
+++ b/server/sonar-web/tests/helpers/l10n-test.js
@@ -0,0 +1,57 @@
+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');
+ });
+ });
+});
diff --git a/server/sonar-web/tests/helpers/measures-test.js b/server/sonar-web/tests/helpers/measures-test.js
index 2caabb3cdf5..3e4569916d7 100644
--- a/server/sonar-web/tests/helpers/measures-test.js
+++ b/server/sonar-web/tests/helpers/measures-test.js
@@ -1,5 +1,6 @@
import { expect } from 'chai';
+import { resetBundle } from '../../src/main/js/helpers/l10n';
import { formatMeasure, formatMeasureVariation } from '../../src/main/js/helpers/measures';
@@ -10,7 +11,7 @@ describe('Measures', function () {
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',
@@ -18,26 +19,7 @@ describe('Measures', function () {
'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 };
});