From 6440b3fad43042feefcf2a4e501dd63c6f94153b Mon Sep 17 00:00:00 2001 From: Maxime Quandalle Date: Thu, 17 Oct 2019 08:49:06 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20Test=20de=20la=20traduction=20et=20?= =?UTF-8?q?pluralisation=20des=20unit=C3=A9s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Implémentés dans 139ca46 --- source/engine/format.js | 2 +- source/engine/format.test.js | 21 +++++++++++++++++++++ source/engine/units.js | 4 ++-- 3 files changed, 24 insertions(+), 3 deletions(-) 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),