|
@@ -1,7 +1,7 @@
|
|
|
import { describe, expect, it } from 'vitest'
|
|
|
-import Translator from './Translator.js'
|
|
|
+import Translator, { type Locale } from './Translator.ts'
|
|
|
|
|
|
-const english = {
|
|
|
+const english: Locale<0 | 1> = {
|
|
|
strings: {
|
|
|
chooseFile: 'Choose a file',
|
|
|
youHaveChosen: 'You have chosen: %{fileName}',
|
|
@@ -9,12 +9,12 @@ const english = {
|
|
|
0: '%{smart_count} file selected',
|
|
|
1: '%{smart_count} files selected',
|
|
|
},
|
|
|
- pluralize (n) {
|
|
|
- if (n === 1) {
|
|
|
- return 0
|
|
|
- }
|
|
|
- return 1
|
|
|
- },
|
|
|
+ },
|
|
|
+ pluralize(n) {
|
|
|
+ if (n === 1) {
|
|
|
+ return 0
|
|
|
+ }
|
|
|
+ return 1
|
|
|
},
|
|
|
}
|
|
|
|
|
@@ -28,7 +28,7 @@ const russian = {
|
|
|
2: 'Выбрано %{smart_count} файлов',
|
|
|
},
|
|
|
},
|
|
|
- pluralize (n) {
|
|
|
+ pluralize(n: number) {
|
|
|
if (n % 10 === 1 && n % 100 !== 11) {
|
|
|
return 0
|
|
|
}
|
|
@@ -50,41 +50,58 @@ describe('Translator', () => {
|
|
|
|
|
|
it('should translate a string with non-string elements', () => {
|
|
|
const translator = new Translator({
|
|
|
+ pluralize: english.pluralize,
|
|
|
strings: {
|
|
|
test: 'Hello %{who}!',
|
|
|
test2: 'Hello %{who}',
|
|
|
},
|
|
|
})
|
|
|
|
|
|
- const who = Symbol('who')
|
|
|
- expect(translator.translateArray('test', { who })).toEqual(['Hello ', who, '!'])
|
|
|
+ const who = Symbol('who') as any as string
|
|
|
+ expect(translator.translateArray('test', { who })).toEqual([
|
|
|
+ 'Hello ',
|
|
|
+ who,
|
|
|
+ '!',
|
|
|
+ ])
|
|
|
// No empty string at the end.
|
|
|
- expect(translator.translateArray('test2', { who })).toEqual(['Hello ', who])
|
|
|
+ expect(translator.translateArray('test2', { who })).toEqual([
|
|
|
+ 'Hello ',
|
|
|
+ who,
|
|
|
+ ])
|
|
|
})
|
|
|
})
|
|
|
|
|
|
describe('translation strings inheritance / overriding', () => {
|
|
|
const launguagePackLoadedInCore = english
|
|
|
const defaultStrings = {
|
|
|
+ pluralize: english.pluralize,
|
|
|
strings: {
|
|
|
youHaveChosen: 'You have chosen 123: %{fileName}',
|
|
|
},
|
|
|
}
|
|
|
const userSuppliedStrings = {
|
|
|
+ pluralize: english.pluralize,
|
|
|
strings: {
|
|
|
youHaveChosen: 'Beep boop: %{fileName}',
|
|
|
},
|
|
|
}
|
|
|
|
|
|
it('should prioritize language pack strings from Core over default', () => {
|
|
|
- const translator = new Translator([defaultStrings, launguagePackLoadedInCore])
|
|
|
+ const translator = new Translator([
|
|
|
+ defaultStrings,
|
|
|
+ launguagePackLoadedInCore,
|
|
|
+ ])
|
|
|
expect(
|
|
|
translator.translate('youHaveChosen', { fileName: 'img.jpg' }),
|
|
|
).toEqual('You have chosen: img.jpg')
|
|
|
})
|
|
|
|
|
|
it('should prioritize user-supplied strings over language pack from Core', () => {
|
|
|
- const translator = new Translator([defaultStrings, launguagePackLoadedInCore, userSuppliedStrings])
|
|
|
+ const translator = new Translator([
|
|
|
+ defaultStrings,
|
|
|
+ launguagePackLoadedInCore,
|
|
|
+ userSuppliedStrings,
|
|
|
+ ])
|
|
|
expect(
|
|
|
translator.translate('youHaveChosen', { fileName: 'img.jpg' }),
|
|
|
).toEqual('Beep boop: img.jpg')
|
|
@@ -103,17 +120,17 @@ describe('Translator', () => {
|
|
|
describe('pluralization', () => {
|
|
|
it('should translate a string', () => {
|
|
|
const translator = new Translator(russian)
|
|
|
- expect(
|
|
|
- translator.translate('filesChosen', { smart_count: 18 }),
|
|
|
- ).toEqual('Выбрано 18 файлов')
|
|
|
+ expect(translator.translate('filesChosen', { smart_count: 18 })).toEqual(
|
|
|
+ 'Выбрано 18 файлов',
|
|
|
+ )
|
|
|
|
|
|
- expect(
|
|
|
- translator.translate('filesChosen', { smart_count: 1 }),
|
|
|
- ).toEqual('Выбран 1 файл')
|
|
|
+ expect(translator.translate('filesChosen', { smart_count: 1 })).toEqual(
|
|
|
+ 'Выбран 1 файл',
|
|
|
+ )
|
|
|
|
|
|
- expect(
|
|
|
- translator.translate('filesChosen', { smart_count: 0 }),
|
|
|
- ).toEqual('Выбрано 0 файлов')
|
|
|
+ expect(translator.translate('filesChosen', { smart_count: 0 })).toEqual(
|
|
|
+ 'Выбрано 0 файлов',
|
|
|
+ )
|
|
|
})
|
|
|
|
|
|
it('should support strings without plural forms', () => {
|
|
@@ -124,12 +141,12 @@ describe('Translator', () => {
|
|
|
pluralize: () => 0,
|
|
|
})
|
|
|
|
|
|
- expect(
|
|
|
- translator.translate('theAmount', { smart_count: 0 }),
|
|
|
- ).toEqual('het aantal is 0')
|
|
|
- expect(
|
|
|
- translator.translate('theAmount', { smart_count: 1 }),
|
|
|
- ).toEqual('het aantal is 1')
|
|
|
+ expect(translator.translate('theAmount', { smart_count: 0 })).toEqual(
|
|
|
+ 'het aantal is 0',
|
|
|
+ )
|
|
|
+ expect(translator.translate('theAmount', { smart_count: 1 })).toEqual(
|
|
|
+ 'het aantal is 1',
|
|
|
+ )
|
|
|
expect(
|
|
|
translator.translate('theAmount', { smart_count: 1202530 }),
|
|
|
).toEqual('het aantal is 1202530')
|
|
@@ -143,11 +160,14 @@ describe('Translator', () => {
|
|
|
1: '%{smart_count} tests',
|
|
|
},
|
|
|
},
|
|
|
+ pluralize: () => 1,
|
|
|
})
|
|
|
|
|
|
expect(() => {
|
|
|
translator.translate('test')
|
|
|
- }).toThrow('Attempted to use a string with plural forms, but no value was given for %{smart_count}')
|
|
|
+ }).toThrow(
|
|
|
+ 'Attempted to use a string with plural forms, but no value was given for %{smart_count}',
|
|
|
+ )
|
|
|
})
|
|
|
})
|
|
|
})
|