mirror of
https://github.com/cupcakearmy/svelte-i18n.git
synced 2024-06-30 18:54:49 +02:00
Add simple pluralization
This commit is contained in:
parent
5662f0401c
commit
be3f7b50fb
72
README.md
72
README.md
|
@ -1,8 +1,6 @@
|
||||||
# svelte-i18n
|
# svelte-i18n
|
||||||
|
|
||||||
> Internationalization for svelte
|
> Internationalization for Svelte
|
||||||
|
|
||||||
**Work-in-progress**
|
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
|
@ -13,10 +11,16 @@ import i18n from 'svelte-i18n'
|
||||||
import { Store } from 'svelte/store'
|
import { Store } from 'svelte/store'
|
||||||
|
|
||||||
/** i18n(svelteStore, { dictionary }) */
|
/** i18n(svelteStore, { dictionary }) */
|
||||||
const store = i18n(new Store(), {
|
let store = new Store()
|
||||||
|
|
||||||
|
store = i18n(store, {
|
||||||
dictionary: {
|
dictionary: {
|
||||||
'pt-BR': {
|
'pt-BR': {
|
||||||
message: 'Mensagem',
|
message: 'Mensagem',
|
||||||
|
greeting: 'Olá {name}, como vai?',
|
||||||
|
greetingIndex: 'Olá {0}, como vai?',
|
||||||
|
meter: 'metros | metro | metros',
|
||||||
|
book: 'livro | livros',
|
||||||
messages: {
|
messages: {
|
||||||
alert: 'Alerta',
|
alert: 'Alerta',
|
||||||
error: 'Erro',
|
error: 'Erro',
|
||||||
|
@ -24,6 +28,10 @@ const store = i18n(new Store(), {
|
||||||
},
|
},
|
||||||
'en-US': {
|
'en-US': {
|
||||||
message: 'Message',
|
message: 'Message',
|
||||||
|
greeting: 'Hello {name}, how are you?',
|
||||||
|
greetingIndex: 'Hello {0}, how are you?',
|
||||||
|
meter: 'meters | meter | meters',
|
||||||
|
book: 'book | books',
|
||||||
messages: {
|
messages: {
|
||||||
alert: 'Alert',
|
alert: 'Alert',
|
||||||
error: 'Error',
|
error: 'Error',
|
||||||
|
@ -57,14 +65,64 @@ store.i18n.setLocale('en-US')
|
||||||
|
|
||||||
### On `templates`
|
### On `templates`
|
||||||
|
|
||||||
|
#### Basic usage
|
||||||
|
|
||||||
```html
|
```html
|
||||||
<div>
|
<div>
|
||||||
{$_('message')}: {$_.upper('messages.success'))}
|
{$_('message')}: {$_('messages.success'))}
|
||||||
|
<!-- Message: SUCCESS-->
|
||||||
</div>
|
</div>
|
||||||
```
|
```
|
||||||
|
|
||||||
Renders:
|
#### Interpolation
|
||||||
|
|
||||||
```html
|
```html
|
||||||
Message: SUCCESS
|
<div>
|
||||||
|
<!-- Named interpolation -->
|
||||||
|
{$_('greeting', { name: 'John' }))}
|
||||||
|
<!-- Hello John, how are you?-->
|
||||||
|
|
||||||
|
<!-- List interpolation -->
|
||||||
|
{$_('greetingIndex', ['John']))}
|
||||||
|
<!-- Hello John, how are you?-->
|
||||||
|
</div>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### Pluralization
|
||||||
|
|
||||||
|
```html
|
||||||
|
<div>
|
||||||
|
0 {$_.plural('meter', 0))}
|
||||||
|
<!-- 0 meters -->
|
||||||
|
|
||||||
|
1 {$_.plural('meter', 1))}
|
||||||
|
<!-- 1 meter -->
|
||||||
|
|
||||||
|
100 {$_.plural('meter', 100))}
|
||||||
|
<!-- 100 meters -->
|
||||||
|
|
||||||
|
0 {$_.plural('book', 0))}
|
||||||
|
<!-- 0 books -->
|
||||||
|
|
||||||
|
10 {$_.plural('book', 10))}
|
||||||
|
<!-- 10 books -->
|
||||||
|
</div>
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Utilities
|
||||||
|
|
||||||
|
```html
|
||||||
|
<div>
|
||||||
|
{$_.upper('message'))}
|
||||||
|
<!-- MESSAGE -->
|
||||||
|
|
||||||
|
{$_.lower('message'))}
|
||||||
|
<!-- message -->
|
||||||
|
|
||||||
|
{$_.capital('message'))}
|
||||||
|
<!-- Message -->
|
||||||
|
|
||||||
|
{$_.title('greeting', { name: 'John' }))}
|
||||||
|
<!-- Hello John, How Are You?-->
|
||||||
|
</div>
|
||||||
|
```
|
30
package-lock.json
generated
30
package-lock.json
generated
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "svelte-i18n",
|
"name": "svelte-i18n",
|
||||||
"version": "0.0.1",
|
"version": "0.0.3",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
@ -3401,12 +3401,14 @@
|
||||||
"balanced-match": {
|
"balanced-match": {
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true
|
"dev": true,
|
||||||
|
"optional": true
|
||||||
},
|
},
|
||||||
"brace-expansion": {
|
"brace-expansion": {
|
||||||
"version": "1.1.11",
|
"version": "1.1.11",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true,
|
"dev": true,
|
||||||
|
"optional": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
"balanced-match": "^1.0.0",
|
"balanced-match": "^1.0.0",
|
||||||
"concat-map": "0.0.1"
|
"concat-map": "0.0.1"
|
||||||
|
@ -3421,17 +3423,20 @@
|
||||||
"code-point-at": {
|
"code-point-at": {
|
||||||
"version": "1.1.0",
|
"version": "1.1.0",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true
|
"dev": true,
|
||||||
|
"optional": true
|
||||||
},
|
},
|
||||||
"concat-map": {
|
"concat-map": {
|
||||||
"version": "0.0.1",
|
"version": "0.0.1",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true
|
"dev": true,
|
||||||
|
"optional": true
|
||||||
},
|
},
|
||||||
"console-control-strings": {
|
"console-control-strings": {
|
||||||
"version": "1.1.0",
|
"version": "1.1.0",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true
|
"dev": true,
|
||||||
|
"optional": true
|
||||||
},
|
},
|
||||||
"core-util-is": {
|
"core-util-is": {
|
||||||
"version": "1.0.2",
|
"version": "1.0.2",
|
||||||
|
@ -3548,7 +3553,8 @@
|
||||||
"inherits": {
|
"inherits": {
|
||||||
"version": "2.0.3",
|
"version": "2.0.3",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true
|
"dev": true,
|
||||||
|
"optional": true
|
||||||
},
|
},
|
||||||
"ini": {
|
"ini": {
|
||||||
"version": "1.3.5",
|
"version": "1.3.5",
|
||||||
|
@ -3560,6 +3566,7 @@
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true,
|
"dev": true,
|
||||||
|
"optional": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
"number-is-nan": "^1.0.0"
|
"number-is-nan": "^1.0.0"
|
||||||
}
|
}
|
||||||
|
@ -3574,6 +3581,7 @@
|
||||||
"version": "3.0.4",
|
"version": "3.0.4",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true,
|
"dev": true,
|
||||||
|
"optional": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
"brace-expansion": "^1.1.7"
|
"brace-expansion": "^1.1.7"
|
||||||
}
|
}
|
||||||
|
@ -3581,12 +3589,14 @@
|
||||||
"minimist": {
|
"minimist": {
|
||||||
"version": "0.0.8",
|
"version": "0.0.8",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true
|
"dev": true,
|
||||||
|
"optional": true
|
||||||
},
|
},
|
||||||
"minipass": {
|
"minipass": {
|
||||||
"version": "2.2.4",
|
"version": "2.2.4",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true,
|
"dev": true,
|
||||||
|
"optional": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
"safe-buffer": "^5.1.1",
|
"safe-buffer": "^5.1.1",
|
||||||
"yallist": "^3.0.0"
|
"yallist": "^3.0.0"
|
||||||
|
@ -3605,6 +3615,7 @@
|
||||||
"version": "0.5.1",
|
"version": "0.5.1",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true,
|
"dev": true,
|
||||||
|
"optional": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
"minimist": "0.0.8"
|
"minimist": "0.0.8"
|
||||||
}
|
}
|
||||||
|
@ -3685,7 +3696,8 @@
|
||||||
"number-is-nan": {
|
"number-is-nan": {
|
||||||
"version": "1.0.1",
|
"version": "1.0.1",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true
|
"dev": true,
|
||||||
|
"optional": true
|
||||||
},
|
},
|
||||||
"object-assign": {
|
"object-assign": {
|
||||||
"version": "4.1.1",
|
"version": "4.1.1",
|
||||||
|
@ -3697,6 +3709,7 @@
|
||||||
"version": "1.4.0",
|
"version": "1.4.0",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true,
|
"dev": true,
|
||||||
|
"optional": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
"wrappy": "1"
|
"wrappy": "1"
|
||||||
}
|
}
|
||||||
|
@ -3818,6 +3831,7 @@
|
||||||
"version": "1.0.2",
|
"version": "1.0.2",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true,
|
"dev": true,
|
||||||
|
"optional": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
"code-point-at": "^1.0.0",
|
"code-point-at": "^1.0.0",
|
||||||
"is-fullwidth-code-point": "^1.0.0",
|
"is-fullwidth-code-point": "^1.0.0",
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "svelte-i18n",
|
"name": "svelte-i18n",
|
||||||
"version": "0.0.3",
|
"version": "0.0.4",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"main": "dist/i18n.js",
|
"main": "dist/i18n.js",
|
||||||
"module": "dist/i18n.m.js",
|
"module": "dist/i18n.m.js",
|
||||||
|
|
0
src/index.d.ts
vendored
0
src/index.d.ts
vendored
17
src/index.js
17
src/index.js
|
@ -49,9 +49,20 @@ export function i18n(store, { dictionary: initialDictionary }) {
|
||||||
plural(path, counter, interpolations, locale) {
|
plural(path, counter, interpolations, locale) {
|
||||||
return getLocalizedMessage(path, interpolations, locale, [
|
return getLocalizedMessage(path, interpolations, locale, [
|
||||||
message => {
|
message => {
|
||||||
const choice =
|
const parts = message.split('|')
|
||||||
typeof counter === 'number' ? Math.min(Math.abs(counter), 2) : 0
|
|
||||||
return message.split('|')[choice]
|
/** Check for 'singular|plural' or 'zero|one|multiple' pluralization */
|
||||||
|
const isSimplePluralization = parts.length === 2
|
||||||
|
let choice = isSimplePluralization ? 1 : 0
|
||||||
|
|
||||||
|
if (typeof counter === 'number') {
|
||||||
|
choice = Math.min(
|
||||||
|
Math.abs(counter) - (isSimplePluralization ? 1 : 0),
|
||||||
|
parts.length - 1,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return parts[choice]
|
||||||
},
|
},
|
||||||
])
|
])
|
||||||
},
|
},
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
import { i18n } from '../src/index'
|
import { i18n } from '../src/index'
|
||||||
import { Store } from 'svelte/store.umd'
|
import { Store } from 'svelte/store.umd'
|
||||||
import { capital, title, upper, lower, isObject, warn } from '../src/utils'
|
import { capital, title, upper, lower, isObject } from '../src/utils'
|
||||||
|
|
||||||
const store = new Store()
|
const store = new Store()
|
||||||
const locales = {
|
const locales = {
|
||||||
|
@ -11,6 +11,7 @@ const locales = {
|
||||||
phrase: 'adoro banana',
|
phrase: 'adoro banana',
|
||||||
phrases: ['Frase 1', 'Frase 2'],
|
phrases: ['Frase 1', 'Frase 2'],
|
||||||
pluralization: 'Zero | Um | Muito!',
|
pluralization: 'Zero | Um | Muito!',
|
||||||
|
simplePluralization: 'Singular | Plural',
|
||||||
interpolation: {
|
interpolation: {
|
||||||
key: 'Olá, {0}! Como está {1}?',
|
key: 'Olá, {0}! Como está {1}?',
|
||||||
named: 'Olá, {name}! Como está {time}?',
|
named: 'Olá, {name}! Como está {time}?',
|
||||||
|
@ -132,6 +133,10 @@ describe('Localization', () => {
|
||||||
store.i18n.setLocale('pt-br')
|
store.i18n.setLocale('pt-br')
|
||||||
const { _ } = store.get()
|
const { _ } = store.get()
|
||||||
|
|
||||||
|
expect(_.plural('simplePluralization')).toBe('Plural')
|
||||||
|
expect(_.plural('simplePluralization', 1)).toBe('Singular')
|
||||||
|
expect(_.plural('simplePluralization', 3)).toBe('Plural')
|
||||||
|
expect(_.plural('simplePluralization', -23)).toBe('Plural')
|
||||||
expect(_.plural('pluralization')).toBe('Zero')
|
expect(_.plural('pluralization')).toBe('Zero')
|
||||||
expect(_.plural('pluralization', 0)).toBe('Zero')
|
expect(_.plural('pluralization', 0)).toBe('Zero')
|
||||||
expect(_.plural('pluralization', 1)).toBe('Um')
|
expect(_.plural('pluralization', 1)).toBe('Um')
|
||||||
|
|
Loading…
Reference in New Issue
Block a user