Skip to content

Commit

Permalink
refactor: Use a dedicated namespace for template helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
fabschurt committed Apr 18, 2024
1 parent 171a95a commit 03c5f7f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export default async function main(
lang
? (
parseProjectTranslations(_parseJSONFile, withSrcDir, dotFlattenObject)(lang)
.then((dictionary) => ({ _t: translateString(IntlMessageFormat, dictionary, lang) }))
.then((dictionary) => ({ _: { t: translateString(IntlMessageFormat, dictionary, lang) } }))
)
: {}
,
Expand Down
20 changes: 10 additions & 10 deletions tests/adapter/icu.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,30 @@ describe('#src/adapter/icu', () => {
account_balance: 'I have {balance, number} moneyz on my bank account.'
}

const _t = translateString(IntlMessageFormat, dictionary)
const trans = translateString(IntlMessageFormat, dictionary)

it('supports param placeholders', () => {
assert.strictEqual(
_t('greetings', { name: 'John Smith' }),
trans('greetings', { name: 'John Smith' }),
'Hello, my name is John Smith.',
)
assert.strictEqual(
_t('occupation', { job: 'smith', town: 'Smithtown' }),
trans('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 }),
trans('number_of_kids', { kidNum: 0 }),
'I have no kids.',
)
assert.strictEqual(
_t('number_of_kids', { kidNum: 1 }),
trans('number_of_kids', { kidNum: 1 }),
'I have a single child.',
)
assert.strictEqual(
_t('number_of_kids', { kidNum: 2 }),
trans('number_of_kids', { kidNum: 2 }),
'I have 2 kids.',
)
})
Expand All @@ -46,11 +46,11 @@ describe('#src/adapter/icu', () => {
const bastilleDay = new Date('1789-07-14')

assert.strictEqual(
_t('bastille_day', { bastilleDay }, 'en-US'),
trans('bastille_day', { bastilleDay }, 'en-US'),
'France’s Bastille Day was on July 14, 1789.',
)
assert.strictEqual(
_t('bastille_day', { bastilleDay }, 'fr-FR'),
trans('bastille_day', { bastilleDay }, 'fr-FR'),
'France’s Bastille Day was on 14 juillet 1789.',
)
})
Expand All @@ -59,11 +59,11 @@ describe('#src/adapter/icu', () => {
const balance = 12345.67

assert.strictEqual(
_t('account_balance', { balance }, 'en-US'),
trans('account_balance', { balance }, 'en-US'),
'I have 12,345.67 moneyz on my bank account.',
)
assert.strictEqual(
_t('account_balance', { balance }, 'fr-FR'),
trans('account_balance', { balance }, 'fr-FR'),
'I have 12 345,67 moneyz on my bank account.',
)
})
Expand Down
4 changes: 2 additions & 2 deletions tests/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ html
head
title Some meaningless title
body
p #{_t('greetings')}! I’m #{_t('fullName', { firstName: id.firstName, lastName: id.lastName })}, I’m #{age} years old, and I live in #{location.city}.
p I work as a #{_t('occupation.dev')}, but I’ve always dreamt about being a #{_t('occupation.fireman')}.
p #{_.t('greetings')}! I’m #{_.t('fullName', { firstName: id.firstName, lastName: id.lastName })}, I’m #{age} years old, and I live in #{location.city}.
p I work as a #{_.t('occupation.dev')}, but I’ve always dreamt about being a #{_.t('occupation.fireman')}.
`)
await fs.writeFile(join(srcDirPath, mainJSFileBasePath), mainJSFileContent)
await fs.writeFile(join(srcDirPath, mainCSSFileBasePath), mainCSSFileContent)
Expand Down

0 comments on commit 03c5f7f

Please sign in to comment.