Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nextcloud-libraries/nextcloud-l10n
Nextcloud L10n helpers for apps and libraries https://npmjs.org/@nextcloud/l10n
https://github.com/nextcloud-libraries/nextcloud-l10n
javascript javascript-library nextcloud nextcloud-plugin translations typescript
Last synced: 7 days ago
JSON representation
Nextcloud L10n helpers for apps and libraries https://npmjs.org/@nextcloud/l10n
- Host: GitHub
- URL: https://github.com/nextcloud-libraries/nextcloud-l10n
- Owner: nextcloud-libraries
- License: gpl-3.0
- Created: 2019-06-18T12:09:07.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2024-10-26T01:29:58.000Z (about 2 months ago)
- Last Synced: 2024-10-30T03:57:00.417Z (about 2 months ago)
- Topics: javascript, javascript-library, nextcloud, nextcloud-plugin, translations, typescript
- Language: TypeScript
- Homepage: https://nextcloud-libraries.github.io/nextcloud-l10n/
- Size: 4.89 MB
- Stars: 6
- Watchers: 5
- Forks: 4
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
- Authors: AUTHORS.md
Awesome Lists containing this project
README
# @nextcloud/l10n
[![REUSE status](https://api.reuse.software/badge/github.com/nextcloud-libraries/nextcloud-l10n)](https://api.reuse.software/info/github.com/nextcloud-libraries/nextcloud-l10n)
[![Build Status](https://img.shields.io/github/actions/workflow/status/nextcloud-libraries/nextcloud-l10n/node.yml?branch=master)](https://github.com/nextcloud-libraries/nextcloud-l10n/actions/workflows/node.yml)
[![npm](https://img.shields.io/npm/v/@nextcloud/l10n.svg)](https://www.npmjs.com/package/@nextcloud/l10n)
[![Documentation](https://img.shields.io/badge/Documentation-online-brightgreen)](https://nextcloud.github.io/nextcloud-l10n/)Nextcloud L10n helpers for apps and libraries.
## Installation
```
npm i -S @nextcloud/l10n
```## Usage
### OC.L10n abstraction
You can use helpers in this package in order generate code that also works when it's not loaded on a Nextcloud page. This is primary useful for testing. The logic will just return the original string if the global variable `OC` isn't found.
In order to not break the l10n string extraction scripts, make sure to alias the imported function to match the legacy syntax:
```js
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)
```See the [localization docs](https://docs.nextcloud.com/server/stable/developer_manual/basics/front-end/l10n.html) for more info.
### Independent translation
You can use this package to translate your app or library independent of Nextcloud. For that you need .po(t) files. These can be extracted with [gettext-extractor](https://github.com/lukasgeiter/gettext-extractor).
```js
import { getGettextBuilder } from '@nextcloud/l10n/gettext'const lang = 'sv'
const po = ... // Use https://github.com/smhg/gettext-parser to read and convert your .po(t) fileconst gt = getGettextBuilder()
.detectLocale()
.addTranslation('sv', po)
.build()
```#### Translate single string
```js
gt.gettext('my source string')
```#### Placeholders
```js
gt.gettext('this is a {placeholder}. and this is {key2}', {
placeholder: 'test',
key2: 'also a test',
})
```See [the developer docs for general guidelines](https://docs.nextcloud.com/server/stable/developer_manual/basics/front-end/l10n.html).
#### Translate plurals
```js
gt.ngettext('%n Mississippi', '%n Mississippi', 3)
```See [the developer docs for general guidelines](https://docs.nextcloud.com/server/stable/developer_manual/basics/front-end/l10n.html).