From 9a355a48332d0207ef18d10b9a9beb33d471a6d4 Mon Sep 17 00:00:00 2001 From: "Grigorii K. Shartsev" Date: Sat, 10 Feb 2024 14:34:53 +0100 Subject: [PATCH] feat: export aliases `t` and `n` for `translate` and `translatePlural` Signed-off-by: Grigorii K. Shartsev --- README.md | 4 +++- lib/translation.ts | 7 +++++++ tests/translation.test.ts | 10 ++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2528fc11..ce306821 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,9 @@ You can use helpers in this package in order generate code that also works when In order to not break the l10n string extraction scripts, make sure to alias the imported function to match the legacy syntax: ```js -import {translate as t, translatePlural as n} from '@nextcloud/l10n' +import { t, n } from '@nextcloud/l10n' +// Or +import { translate as t, translatePlural as n } from '@nextcloud/l10n' t('myapp', 'Hello!') n('myapp', '%n cloud', '%n clouds', 100) diff --git a/lib/translation.ts b/lib/translation.ts index 42b099d5..3c2776f9 100644 --- a/lib/translation.ts +++ b/lib/translation.ts @@ -399,3 +399,10 @@ export function getPlural(number: number) { return 0 } } + +// Export short-hand + +export { + translate as t, + translatePlural as n, +} diff --git a/tests/translation.test.ts b/tests/translation.test.ts index 404d266d..38265d0e 100644 --- a/tests/translation.test.ts +++ b/tests/translation.test.ts @@ -5,6 +5,8 @@ import { translate, translatePlural, unregister, + t, + n, } from '../lib/translation' declare const window: NextcloudWindowWithRegistry @@ -139,6 +141,14 @@ describe('translate', () => { expect(translatePlural('core', ...text, 1)).toBe('Lade 1 Datei von {url} herunter') expect(translatePlural('core', ...text, 2)).toBe('Lade 2 Dateien von {url} herunter') }) + + it('has "t" alias for "translate"', () => { + expect(t).toBe(translate) + }) + + it('has "n" alias for "translatePlural"', () => { + expect(n).toBe(translatePlural) + }) }) describe('register', () => {