diff --git a/.gitignore b/.gitignore index 5bef4823d..a8644b011 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,5 @@ results/ junit.xml __pycache__/ + +.DS_Store diff --git a/appveyor.yml b/appveyor.yml index b5014269d..213f59bf0 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -5,6 +5,7 @@ environment: install: # Get the latest stable version of Node.js or io.js - ps: Install-Product node $env:nodejs_version + - yarn add full-icu -W # install modules - yarn install - yarn global add yalc @@ -15,7 +16,7 @@ test_script: - node --version - yarn --version # run tests - - node scripts/test.js + - node --icu-data-dir=node_modules/full-icu scripts/test.js # Don't actually build. build: off diff --git a/circle.yml b/circle.yml index 10d6c925c..ccd849c47 100644 --- a/circle.yml +++ b/circle.yml @@ -6,9 +6,9 @@ workflows: jobs: - test-linter - - test-node8: - requires: - - test-linter + - test-node8 +# requires: +# - test-linter # - build-docs: # requires: @@ -23,6 +23,22 @@ jobs: steps: - checkout + - run: + name: Print node version + command: node -v + + - run: + name: Print node version + command: npm install full-icu + + - run: + name: pwd + command: pwd + + - run: + name: Testing full-icu + command: env NODE_ICU_DATA=./node_modules/full-icu node test-full-icu.js + - run: name: Install build tools command: sudo yarn global add codecov yalc @@ -30,16 +46,28 @@ jobs: - restore_cache: key: node-modules-{{ checksum "yarn.lock" }} + - run: + name: Testing full-icu + command: env NODE_ICU_DATA=./node_modules/full-icu node test-full-icu.js + - run: name: Install dependencies command: yarn install + - run: + name: Testing full-icu + command: env NODE_ICU_DATA=./node_modules/full-icu node test-full-icu.js + - save_cache: key: node-modules-{{ checksum "yarn.lock" }} paths: - node_modules - - run: node scripts/test.js + - run: + name: Testing full-icu + command: env NODE_ICU_DATA=./node_modules/full-icu node test-full-icu.js + + - run: env NODE_ICU_DATA=./node_modules/full-icu node scripts/test.js - run: name: Send coverage report diff --git a/packages/core/src/select.js b/packages/core/src/select.js index 935253f5f..690f0ae8d 100644 --- a/packages/core/src/select.js +++ b/packages/core/src/select.js @@ -11,21 +11,29 @@ type PluralForms = { type PluralProps = { value: number, - offset?: number + offset?: number, + culture?: string } & PluralForms +declare var Intl: { + NumberFormat: any +} + const _plural = type => (i18n: any) => ({ value, offset = 0, + culture = "en-UK", other, ...pluralForms }: PluralProps): string => { const diff = value - offset + const intl = new Intl.NumberFormat(culture) + const diffAsString = intl.format(diff) const translation = pluralForms[value.toString()] || // exact match pluralForms[i18n.pluralForm(diff, type)] || // plural form other // fallback - return translation.replace("#", diff.toString()) + return translation.replace("#", diffAsString) } const plural = _plural("cardinal") diff --git a/packages/core/src/select.test.js b/packages/core/src/select.test.js index 4d1cc38c4..de18c37cb 100644 --- a/packages/core/src/select.test.js +++ b/packages/core/src/select.test.js @@ -2,6 +2,19 @@ import { plural, select, selectOrdinal } from "./select" import { setupI18n } from "@lingui/core" +const hasFullICU = (() => { + try { + const january = new Date(9e8); + const spanish = new Intl.DateTimeFormat('es', { month: 'long' }); + return spanish.format(january) === 'enero'; + } catch (err) { + console.log(err) + return false; + } +})(); + +console.log("hasFullICU:", hasFullICU) + describe("plural", function() { const i18n = setupI18n({ language: "en", @@ -49,6 +62,22 @@ describe("plural", function() { other: "Their and # books" }) ).toEqual("Their and 4 books") + + expect( + p({ + value: 1, + other: "# كتاب", + culture: "en-UK" + }) + ).toEqual("1 كتاب") + + expect( + p({ + value: 1, + other: "لدي # كتاب", + culture: "ar-AS" + }) + ).toEqual("لدي ١ كتاب") }) }) diff --git a/packages/react/src/Select.js b/packages/react/src/Select.js index 69d52483f..262556670 100644 --- a/packages/react/src/Select.js +++ b/packages/react/src/Select.js @@ -14,7 +14,8 @@ type PluralProps = { two?: any, few?: any, many?: any, - other: any + other: any, + culture?: string } & withI18nProps & RenderProps diff --git a/packages/react/src/Select.test.js b/packages/react/src/Select.test.js index 9f12c6663..e82c1d637 100644 --- a/packages/react/src/Select.test.js +++ b/packages/react/src/Select.test.js @@ -74,6 +74,20 @@ describe("Categories", function() { expect(t()).toEqual("5 knih") }) + it("should use plural forms based on culture", function() { + const node = mount( + , + languageContext("de") + ) + + const t = () => node.find("Render").text() + + expect(t()).toEqual("1.000") + + node.setProps({ value: 2000 }) + expect(t()).toEqual("2.000") + }) + it("should offset value", function() { const node = mount(