Skip to content

Commit

Permalink
Remove data for plural rules and digit transforms as well
Browse files Browse the repository at this point in the history
These are also made injectable by the client script, as done with fallback languages earlier.
  • Loading branch information
siddharthvp committed May 22, 2021
1 parent c9df2e1 commit 10e9768
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 12 deletions.
File renamed without changes.
File renamed without changes.
9 changes: 9 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import BananaParser from './parser'
import BananaMessageStore from './messagestore'
import BananaEmitter from './emitter'
import BananaLanguage from "./languages/language";

export default class Banana {
/**
Expand Down Expand Up @@ -50,6 +51,14 @@ export default class Banana {
this.fallbackLocales = locales
}

setPluralRules(locale, rule) {
BananaLanguage.pluralRules[locale] = rule
}

setDigitTransforms(locale, transforms) {
BananaLanguage.digitTransforms[locale] = transforms
}

getMessage (messageKey) {
let locale = this.locale
let fallbackIndex = 0
Expand Down
12 changes: 6 additions & 6 deletions src/languages/language.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import PLURALRULES from './pluralrules.json'
import DIGITTRANSFORMTABLE from './digit-transform.json'

export default class BananaLanguage {
static pluralRules = {}
static digitTransforms = {}

constructor (locale) {
this.locale = locale
}
Expand Down Expand Up @@ -36,7 +36,7 @@ export default class BananaLanguage {

forms = forms.filter((form) => !!form)

let pluralRules = PLURALRULES[ this.locale ]
let pluralRules = BananaLanguage.pluralRules[ this.locale ]

if (!pluralRules) {
// default fallback.
Expand Down Expand Up @@ -173,10 +173,10 @@ export default class BananaLanguage {
* representation, or boolean false if there is no information.
*/
digitTransformTable (language) {
if (!DIGITTRANSFORMTABLE[ language ]) {
if (!BananaLanguage.digitTransforms[ language ]) {
return false
}

return DIGITTRANSFORMTABLE[ language ].split('')
return BananaLanguage.digitTransforms[ language ].split('')
}
}
23 changes: 17 additions & 6 deletions test/banana.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -576,21 +576,23 @@ describe('Banana', function () {
})

it('should parse formatnum', () => {
const bananaHi = new Banana('hi')
const banana = new Banana('hi')
banana.setDigitTransforms('hi', '०१२३४५६७८९')
banana.setDigitTransforms('fa', '۰۱۲۳۴۵۶۷۸۹')
assert.strictEqual(
bananaHi.i18n('{{formatnum:34242}}'),
banana.i18n('{{formatnum:34242}}'),
'३४२४२',
'Gives Devanagiri numerals with locale=hi'
)
const bananaEn = new Banana('en')
banana.setLocale('en')
assert.strictEqual(
bananaEn.i18n('{{formatnum:34242}}'),
banana.i18n('{{formatnum:34242}}'),
'34242',
'Gives Hindu–Arabic numerals with locale=en'
)
const bananaFa = new Banana('fa')
banana.setLocale('fa')
assert.strictEqual(
bananaFa.i18n('{{formatnum:42}}'),
banana.i18n('{{formatnum:42}}'),
'۴۲',
'Gives Persian numerals with locale=fa'
)
Expand All @@ -599,6 +601,14 @@ describe('Banana', function () {
it('should parse the Arabic message', () => {
const locale = 'ar'
const banana = new Banana(locale)
banana.setPluralRules('ar', {
"zero": "n = 0",
"one": "n = 1",
"two": "n = 2",
"few": "n % 100 = 3..10",
"many": "n % 100 = 11..99"
})
banana.setDigitTransforms('ar', '٠١٢٣٤٥٦٧٨٩')
assert.strictEqual(banana.locale, 'ar', 'Locale is Arabic')
assert.strictEqual(banana.i18n('{{plural:$1|zero|one|two|few|many|other}}', 1), 'one',
'Arabic plural test for one')
Expand Down Expand Up @@ -657,6 +667,7 @@ describe('Banana', function () {
it('should use digit transform table and localize digits', function () {
const langCode = 'fa'
const banana = new Banana(langCode)
banana.setDigitTransforms('fa', '۰۱۲۳۴۵۶۷۸۹')
assert.strictEqual(banana.parser.emitter.locale, langCode, 'Locale is ' + langCode)
assert.strictEqual(banana.parser.emitter.language.convertNumber('8'), '۸',
'Persian transform of 8')
Expand Down

0 comments on commit 10e9768

Please sign in to comment.