Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: export aliases t and n for translate and translatePlural #727

Merged
merged 1 commit into from
Feb 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions lib/translation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
text: string,
vars?: Record<string, string | number>,
number?: number,
options?: TranslationOptions

Check warning on line 37 in lib/translation.ts

View workflow job for this annotation

GitHub Actions / eslint

Missing trailing comma
): string {
const defaultOptions = {
escape: true,
Expand Down Expand Up @@ -76,7 +76,7 @@
return optSanitize(_build(
translation,
vars,
number

Check warning on line 79 in lib/translation.ts

View workflow job for this annotation

GitHub Actions / eslint

Missing trailing comma
))
} else {
return optSanitize(translation)
Expand All @@ -99,7 +99,7 @@
textPlural: string,
number: number,
vars?: Record<string, string | number>,
options?: TranslationOptions

Check warning on line 102 in lib/translation.ts

View workflow job for this annotation

GitHub Actions / eslint

Missing trailing comma
): string {
const identifier = '_' + textSingular + '_::_' + textPlural + '_'
const bundle = getAppTranslations(app)
Expand Down Expand Up @@ -175,7 +175,7 @@
* Register an app's translation bundle.
*
* @param {string} appName name of the app
* @param {Object<string, string>} bundle translation bundle

Check warning on line 178 in lib/translation.ts

View workflow job for this annotation

GitHub Actions / eslint

Use object shorthand or index signatures instead of `Object`, e.g., `{[key: string]: string}`
*/
export function register(appName: string, bundle: Translations) {
registerAppTranslations(appName, bundle, getPlural)
Expand Down Expand Up @@ -399,3 +399,10 @@
return 0
}
}

// Export short-hand

export {
translate as t,
translatePlural as n,
}
10 changes: 10 additions & 0 deletions tests/translation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
translate,
translatePlural,
unregister,
t,
n,
} from '../lib/translation'

declare const window: NextcloudWindowWithRegistry
Expand Down Expand Up @@ -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', () => {
Expand Down
Loading