Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for culture #167

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ results/
junit.xml

__pycache__/

.DS_Store
3 changes: 2 additions & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
36 changes: 32 additions & 4 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ workflows:
jobs:
- test-linter

- test-node8:
requires:
- test-linter
- test-node8
# requires:
# - test-linter

# - build-docs:
# requires:
Expand All @@ -23,23 +23,51 @@ 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

- 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
Expand Down
12 changes: 10 additions & 2 deletions packages/core/src/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
29 changes: 29 additions & 0 deletions packages/core/src/select.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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("لدي ١ كتاب")
})
})

Expand Down
3 changes: 2 additions & 1 deletion packages/react/src/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ type PluralProps = {
two?: any,
few?: any,
many?: any,
other: any
other: any,
culture?: string
} & withI18nProps &
RenderProps

Expand Down
14 changes: 14 additions & 0 deletions packages/react/src/Select.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,20 @@ describe("Categories", function() {
expect(t()).toEqual("5 knih")
})

it("should use plural forms based on culture", function() {
const node = mount(
<Plural value="1000" one="#" other="#" culture="de-DE" />,
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(
<Plural
Expand Down
2 changes: 2 additions & 0 deletions test-full-icu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
console.log("*** Testing full-icu ***");
console.log(new Intl.DateTimeFormat('es',{month:'long'}).format(new Date(9E8)));