diff --git a/source/engine/format.js b/source/engine/format.js index 79e23bb21..ea9c0e889 100644 --- a/source/engine/format.js +++ b/source/engine/format.js @@ -58,7 +58,7 @@ export function formatValue({ if (typeof value !== 'number') { return value } - const serializedUnit = serialiseUnit(unit, value) + const serializedUnit = serialiseUnit(unit, value, language) switch (serializedUnit) { case '€': diff --git a/source/engine/format.test.js b/source/engine/format.test.js index 577dfeab9..a6db4380f 100644 --- a/source/engine/format.test.js +++ b/source/engine/format.test.js @@ -1,5 +1,6 @@ import { expect } from 'chai' import { formatCurrency, formatPercentage, formatValue } from './format' +import { parseUnit } from 'Engine/units' describe('format engine values', () => { it('format currencies', () => { @@ -25,3 +26,23 @@ describe('format engine values', () => { expect(formatValue({ value: 1200, language: 'fr' })).to.equal('1 200') }) }) + +describe('Units handling', () => { + it('translate unit', () => { + expect(formatValue({ value: 1, unit: 'jour', language: 'fr' })).to.equal( + '1 jour' + ) + expect(formatValue({ value: 1, unit: 'jour', language: 'en' })).to.equal( + '1 day' + ) + }) + + it('pluralize unit', () => { + expect(formatValue({ value: 2, unit: 'jour', language: 'fr' })).to.equal( + '2 jours' + ) + expect( + formatValue({ value: 7, unit: parseUnit('jour/semaine'), language: 'fr' }) + ).to.equal('7 jours / semaine') + }) +}) diff --git a/source/engine/units.js b/source/engine/units.js index 987a1fd0e..0dc26fd11 100644 --- a/source/engine/units.js +++ b/source/engine/units.js @@ -18,10 +18,10 @@ let printUnits = (units, count) => .join('-') const plural = 2 -export let serialiseUnit = (rawUnit, count = plural) => { +export let serialiseUnit = (rawUnit, count = plural, lng = undefined) => { if (typeof rawUnit !== 'object') { return typeof rawUnit === 'string' - ? i18n.t(`units:${rawUnit}`, { count }) + ? i18n.t(`units:${rawUnit}`, { count, lng }) : rawUnit } let unit = simplify(rawUnit),