From cdf2ac8291d37bbf42118909f449b658e074bb9d Mon Sep 17 00:00:00 2001 From: Fabien Schurter <8007715+fabschurt@users.noreply.github.com> Date: Fri, 19 Apr 2024 01:06:09 +0200 Subject: [PATCH] refactor: Use a dedicated namespace for template helper functions (#52) --- src/main.js | 2 +- tests/adapter/icu.test.js | 20 ++++++++++---------- tests/main.test.js | 4 ++-- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/main.js b/src/main.js index d1f5152..c7419e4 100644 --- a/src/main.js +++ b/src/main.js @@ -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) } })) ) : {} , diff --git a/tests/adapter/icu.test.js b/tests/adapter/icu.test.js index 3e67e29..602ff8e 100644 --- a/tests/adapter/icu.test.js +++ b/tests/adapter/icu.test.js @@ -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.', ) }) @@ -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.', ) }) @@ -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.', ) }) diff --git a/tests/main.test.js b/tests/main.test.js index 803b3e6..784dd1c 100644 --- a/tests/main.test.js +++ b/tests/main.test.js @@ -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)