Skip to content

Commit e5bb623

Browse files
authored
Merge pull request #853 from nextcloud-libraries/chore/clarify-readme
doc: Clarify readme about Nextcloud provided and custom translations
2 parents 3bef6da + f31c187 commit e5bb623

File tree

1 file changed

+76
-10
lines changed

1 file changed

+76
-10
lines changed

README.md

Lines changed: 76 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,43 +19,110 @@ npm i -S @nextcloud/l10n
1919

2020
## Usage
2121

22-
### OC.L10n abstraction
22+
### With Nextcloud based translations
2323

24-
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.
24+
Apps normally use the Nextcloud provided translation mechanism which allows to translate backend (PHP) strings together with the frontend.
25+
This can either be done using the Nextcloud Transifex automatization or translations can be done manually.
26+
See the [localization docs](https://docs.nextcloud.com/server/stable/developer_manual/basics/front-end/l10n.html) for how to setup.
2527

26-
In order to not break the l10n string extraction scripts, make sure to alias the imported function to match the legacy syntax:
28+
When using the Nextcloud translation system (manually or with automated Transifex) Nextcloud will automatically
29+
register the translations on the frontend.
30+
In this case all you have to do is to import the translation functions from this package like shown below:
2731

2832
```js
2933
import { t, n } from '@nextcloud/l10n'
3034
// Or
3135
import { translate as t, translatePlural as n } from '@nextcloud/l10n'
36+
```
37+
38+
> [!NOTE]
39+
> In order to not break the l10n string extraction scripts, make sure to use the aliases `t` and `n`.
3240
41+
#### Translate singular string
42+
43+
```js
3344
t('myapp', 'Hello!')
45+
```
46+
47+
#### Translate singular string with placeholders
48+
49+
```js
50+
t('myapp', 'Hello {name}!', { name: 'Jane' })
51+
```
52+
53+
By default placeholders are sanitized and escaped to prevent XSS.
54+
But you can disable this behavior if you trust the user input:
55+
56+
```js
57+
t(
58+
'myapp',
59+
'See also the {linkStart}documentation{linkEnd}.',
60+
{
61+
linkStart: '<a href="http://example.com">'
62+
linkEnd: '</a>',
63+
},
64+
undefined, // this would be a number replacement
65+
{ escape: false },
66+
)
67+
```
68+
69+
#### Translate plural string
70+
71+
```js
3472
n('myapp', '%n cloud', '%n clouds', 100)
3573
```
3674

37-
See the [localization docs](https://docs.nextcloud.com/server/stable/developer_manual/basics/front-end/l10n.html) for more info.
75+
Of course also placeholders are possible:
76+
77+
```js
78+
n('myapp', '{name} owns %n file', '{name} owns %n files', 100, { name: 'Jane' })
79+
```
3880

3981
### Independent translation
4082

41-
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).
83+
It is also possible to use this package for Nextcloud independent translations.
84+
This is mostly useful for libraries that provide translated strings.
85+
86+
Independent means you can use this without Nextcloud registered translations buy just providing your `.po` files.
87+
If you decide to use this way you have to extract the translation strings manually, for example using the [gettext-extractor](https://github.com/lukasgeiter/gettext-extractor).
4288

4389
```js
4490
import { getGettextBuilder } from '@nextcloud/l10n/gettext'
4591

46-
const lang = 'sv'
4792
const po = ... // Use https://github.com/smhg/gettext-parser to read and convert your .po(t) file
4893

94+
// When using this for a Nextcloud app you can auto-detect the language
4995
const gt = getGettextBuilder()
5096
.detectLocale()
5197
.addTranslation('sv', po)
5298
.build()
99+
100+
// But it is also possible to force a language
101+
const gt = getGettextBuilder()
102+
.setLocale('sv')
103+
.addTranslation('sv', po)
104+
.build()
105+
```
106+
107+
To make things easier you could also create aliases for this in a module and reuse it in your code:
108+
109+
```ts
110+
// When using JavaScript
111+
export const t = (...args) => gt.gettext(...args)
112+
export const n = (...args) => gt.ngettext(...args)
113+
114+
// When using Typescript
115+
export const t = (...args: Parameters<typeof gt.gettext>) => gt.gettext(...args)
116+
export const n = (...args: Parameters<typeof gt.ngettext>) => gt.ngettext(...args)
53117
```
54118

55119
#### Translate single string
56120

57121
```js
58122
gt.gettext('my source string')
123+
124+
// or if you are using the aliases mentioned above:
125+
t('my source string')
59126
```
60127

61128
#### Placeholders
@@ -67,12 +134,11 @@ gt.gettext('this is a {placeholder}. and this is {key2}', {
67134
})
68135
```
69136

70-
See [the developer docs for general guidelines](https://docs.nextcloud.com/server/stable/developer_manual/basics/front-end/l10n.html).
71-
72137
#### Translate plurals
73138

74139
```js
75140
gt.ngettext('%n Mississippi', '%n Mississippi', 3)
76-
```
77141

78-
See [the developer docs for general guidelines](https://docs.nextcloud.com/server/stable/developer_manual/basics/front-end/l10n.html).
142+
// or if you are using the aliases mentioned above:
143+
n('%n Mississippi', '%n Mississippi', 3)
144+
```

0 commit comments

Comments
 (0)