-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
276 additions
and
27 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import assert from 'node:assert' | ||
|
||
export const translateString = ( | ||
(IntlMessageFormat, dictionary, defaultLocale = 'en') => ( | ||
(msgID, params = {}, locale = defaultLocale) => { | ||
assert( | ||
msgID in dictionary, | ||
`The messsage with ID \`${msgID}\` does not exist in the dictionary.`, | ||
) | ||
|
||
return new IntlMessageFormat(dictionary[msgID], locale).format(params) | ||
} | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { describe, it } from 'node:test' | ||
import assert from 'node:assert' | ||
import { translateString } from '#src/adapter/icu' | ||
import { IntlMessageFormat } from 'intl-messageformat' | ||
|
||
describe('#src/adapter/icu', () => { | ||
describe('translateString()', () => { | ||
describe('translates an ICU-formatted string using a static dictionary', () => { | ||
const dictionary = { | ||
greetings: 'Hello, my name is {name}.', | ||
occupation: 'I’m a {job}, the best in {town}.', | ||
number_of_kids: 'I have {kidNum, plural, =0 {no kids} =1 {a single child} other {# kids}}.', | ||
bastille_day: 'France’s Bastille Day was on {bastilleDay, date, ::yyyyMMMMdd}.', | ||
account_balance: 'I have {balance, number} moneyz on my bank account.' | ||
} | ||
|
||
const _t = translateString(IntlMessageFormat, dictionary) | ||
|
||
it('supports param placeholders', () => { | ||
assert.strictEqual( | ||
_t('greetings', { name: 'John Smith' }), | ||
'Hello, my name is John Smith.', | ||
) | ||
assert.strictEqual( | ||
_t('occupation', { job: 'smith', town: 'Smithtown' }), | ||
'I’m a smith, the best in Smithtown.', | ||
) | ||
}) | ||
|
||
it('supports plural form', () => { | ||
assert.strictEqual( | ||
_t('number_of_kids', { kidNum: 0 }), | ||
'I have no kids.', | ||
) | ||
assert.strictEqual( | ||
_t('number_of_kids', { kidNum: 1 }), | ||
'I have a single child.', | ||
) | ||
assert.strictEqual( | ||
_t('number_of_kids', { kidNum: 2 }), | ||
'I have 2 kids.', | ||
) | ||
}) | ||
|
||
it('supports locale-specific date formatting', () => { | ||
const bastilleDay = new Date('1789-07-14') | ||
|
||
assert.strictEqual( | ||
_t('bastille_day', { bastilleDay }, 'en-US'), | ||
'France’s Bastille Day was on July 14, 1789.', | ||
) | ||
assert.strictEqual( | ||
_t('bastille_day', { bastilleDay }, 'fr-FR'), | ||
'France’s Bastille Day was on 14 juillet 1789.', | ||
) | ||
}) | ||
|
||
it('supports locale-specific number formatting', () => { | ||
const balance = 12345.67 | ||
|
||
assert.strictEqual( | ||
_t('account_balance', { balance }, 'en-US'), | ||
'I have 12,345.67 moneyz on my bank account.', | ||
) | ||
assert.strictEqual( | ||
_t('account_balance', { balance }, 'fr-FR'), | ||
'I have 12 345,67 moneyz on my bank account.', | ||
) | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.