You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: developer_manual/basics/front-end/l10n.rst
+87-49Lines changed: 87 additions & 49 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,27 +7,19 @@ Translation
7
7
Nextcloud provides mechanisms for internationalization (make an application translatable) and localization (add translations for specific languages). This section provides detailed instructions for both aspects.
8
8
In order to make your app translatable (internationalization), you should use Nextcloud's methods for translating strings. They are available for both the server-side (PHP, Templates) as well as for the client-side (JavaScript).
9
9
10
-
PHP
11
-
---
10
+
PHP Backend
11
+
-----------
12
12
13
13
If localized strings are used in the backend code, simply inject the ``\OCP\IL10N`` class into your service via type hinting it in the constructor. You will automatically get the language object containing the translations of your app:
14
14
15
15
16
16
.. code-block:: php
17
17
18
18
<?php
19
-
namespace OCA\MyApp\Service;
20
-
21
-
use OCP\IL10N;
22
-
23
-
24
19
class AuthorService {
25
-
26
-
/** @var IL10N */
27
-
private $l;
28
-
29
-
public function __construct(IL10N $l) {
30
-
$this->l = $l;
20
+
public function __construct(
21
+
private \OCP\IL10N $l,
22
+
) {
31
23
}
32
24
33
25
…
@@ -38,13 +30,12 @@ Strings can then be translated in the following way:
38
30
.. code-block:: php
39
31
40
32
<?php
41
-
…
42
-
43
33
class AuthorService {
44
34
45
35
…
46
36
47
37
public function getLanguageCode() {
38
+
// Get the language code of the current language
48
39
return $this->l->getLanguageCode();
49
40
}
50
41
@@ -61,14 +52,17 @@ Strings can then be translated in the following way:
61
52
public function getAuthors($count, $city) {
62
53
// Translation with plural
63
54
return $this->l->n(
64
-
'%n author is currently in the city %1$s', // singular string
65
-
'%n authors are currently in the city %1$s', // plural string
55
+
'%n author is currently in the city %1$s', // singular string
56
+
'%n authors are currently in the city %1$s', // plural string
66
57
$count, // number to decide which plural to use
67
58
[$city] // further parameters are possible
68
59
);
69
60
}
70
61
}
71
62
63
+
FIXME
64
+
-----
65
+
72
66
Correct plurals
73
67
^^^^^^^^^^^^^^^
74
68
@@ -97,15 +91,10 @@ the ``force_language`` and ``default_language`` configuration options to conside
97
91
.. code-block:: php
98
92
99
93
<?php
100
-
101
-
use OCP\L10N\IFactory;
102
94
class SendEmail {
103
-
104
-
/** @var IFactory */
105
-
private $l10nFactory;
106
-
107
-
public function __construct(IFactory $l10nFactory) {
108
-
$this->l10nFactory = $l10nFactory;
95
+
public function __construct(
96
+
private \OCP\L10N\IFactory $l10nFactory,
97
+
) {
109
98
}
110
99
111
100
public function send(IUser $user): void {
@@ -116,25 +105,28 @@ the ``force_language`` and ``default_language`` configuration options to conside
116
105
}
117
106
118
107
119
-
Templates
120
-
---------
108
+
PHP Templates
109
+
-------------
121
110
122
-
In every template the global variable **$l** can be used to translate the strings using its methods **t()** and **n()**:
111
+
In every template the global variable ``$l`` can be used to translate the strings using its methods ``t()`` and ``n()``:
For the right date format use ``<?php p($l->l('date', time()));?>``.
118
+
// Text with a placeholder
119
+
<div><?php p($l->t('Show files of %1$s', [$user])); ?></div>
131
120
121
+
// Date string
122
+
<em><?php p($l->l('date', time())); ?></em>
132
123
124
+
JavaScript / Typescript / Vue
125
+
-----------------------------
133
126
134
-
JavaScript
135
-
----------
136
-
137
-
There are global functions **t()** and **n()** available for translating strings in javascript code. They differ a bit in terms of usage compared to php:
127
+
There are global functions ``t()`` and ``n()`` available for translating strings in javascript code.
128
+
If your app is build, you can import the translation functions from the `@nextcloud/l10n package <https://github.com/nextcloud-libraries/nextcloud-l10n>`_.
129
+
They differ a bit in terms of usage compared to php:
138
130
139
131
* First argument is the appId e.g. ``'myapp'``
140
132
* Placeholders (apart from the count in plurals) use single-mustache brackets with meaning-full descriptors.
@@ -147,11 +139,57 @@ There are global functions **t()** and **n()** available for translating strings
147
139
n('myapp', 'Import %n calendar into {collection}', 'Import %n calendars into {collection}', selectionLength, {collection:'Nextcloud'});
148
140
149
141
142
+
Guidelines
143
+
----------
150
144
151
-
Important notes
152
-
---------------
153
-
154
-
Please also look through the following steps to improve your strings and make them better translatable by others
145
+
Please also look through the following hints to improve your strings and make them better translatable by the community
146
+
and therefore improving the experience for non-english users.
147
+
148
+
.. list-table::
149
+
:header-rows: 1
150
+
151
+
* - Bad
152
+
- Good
153
+
- Description
154
+
* - ``´`` or ``’``
155
+
- ``'``
156
+
- Use ascii single quote
157
+
* - ``Loading...``
158
+
- ``Loading …``
159
+
- | Use unicode triple-dot character.
160
+
|Add a space before the triple-dot when trimming a sentence instead of a word.
161
+
* - Don't
162
+
- Do not
163
+
- Using the spelled out version is easier to understand and makes translating easier.
164
+
* - Won't
165
+
- Will not
166
+
- Using the spelled out version is easier to understand and makes translating easier.
167
+
* - Can not
168
+
- Cannot
169
+
- Using the combined version is easier to understand and makes translating easier.
170
+
* - id
171
+
- ID
172
+
- Full uppercase for shortcutting "identifier"
173
+
* - Users
174
+
- Accounts / People
175
+
- Use **accounts** when you refer to a profile/entity. Use **people** when referring to humans.
176
+
* - Admin / Administrator
177
+
- Administration
178
+
- | Refer to administration as a non-human organizational entity
179
+
|instead of a single or multiple persons.
180
+
* - Headline
181
+
- Headline:
182
+
- Include colons ``:`` in the translations as some languages add a space before the colon.
183
+
* - | " Leading space"
184
+
|"Trailing space "
185
+
- | "No leading space"
186
+
|"No trailing space"
187
+
- | Leading or trailing spaces mostly indicate that strings are concatenated.
188
+
|For translators it is often helpful to have all the content in a single translation,
189
+
|as order and references between words and sentences might get lost otherwise.
190
+
* - "Error:" $error
191
+
- "Error: %s"
192
+
- Instead of concatenating errors or part messages, make them a proper placeholder
155
193
156
194
Improving your translations
157
195
^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -166,10 +204,10 @@ Bad example:
166
204
167
205
Translators will translate:
168
206
169
-
* Select file from
170
-
* local filesystem
171
-
* ' or '
172
-
* cloud
207
+
* ``Select file from``
208
+
* ``local filesystem``
209
+
* ``or`` (with leading and trailing whitespace)
210
+
* ``cloud``
173
211
174
212
Translating these individual strings results in ``local filesystem`` and ``cloud`` losing case. The two white spaces surrounding ``or`` will get lost while translating as well. For languages that have a different grammatical order it prevents the translators from reordering the sentence components.
175
213
@@ -196,7 +234,7 @@ This allows translators to have the cloudlink before the browselink in case the
196
234
.. _Hints:
197
235
198
236
Provide context hints for translators
199
-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
237
+
-------------------------------------
200
238
201
239
In case some translation strings may be translated wrongly because they have multiple meanings, you can add hints which will be shown in the Transifex web-interface:
202
240
@@ -213,7 +251,7 @@ In case some translation strings may be translated wrongly because they have mul
213
251
</li>
214
252
</ul>
215
253
216
-
**Javascript**
254
+
**Javascript / Typescript**
217
255
218
256
.. code-block:: javascript
219
257
@@ -261,8 +299,8 @@ Nextcloud's translation system is powered by `Transifex <https://explore.transif
261
299
Translation tool
262
300
^^^^^^^^^^^^^^^^
263
301
264
-
The translation tool scrapes the source code for method calls to **t()**
265
-
or **n()** to extract the strings that should be translated. If you check
302
+
The translation tool scrapes the source code for method calls to ``t()``
303
+
or ``n()`` to extract the strings that should be translated. If you check
266
304
in minified JS code for example then those method names are also quite
267
305
common and could cause wrong extractions. For this reason we allow to
268
306
specify a list of files that the translation tool will not scrape for
@@ -278,7 +316,7 @@ Setup of the transifex sync
278
316
^^^^^^^^^^^^^^^^^^^^^^^^^^^
279
317
280
318
To setup the transifex sync within the Nextcloud community you need to add first the
281
-
transifex config to your app folder at :file:`.tx/config` (please replace **MYAPP** with your apps id):
319
+
transifex config to your app folder at :file:`.tx/config` (please replace ``MYAPP`` with your apps id):
282
320
283
321
.. code-block:: ini
284
322
@@ -349,7 +387,7 @@ in the app folder::
349
387
cd /srv/http/nextcloud/apps/myapp
350
388
translationtool.phar create-pot-files
351
389
352
-
The translation tool requires **gettext**, installable via::
390
+
The translation tool requires ``gettext``, installable via::
0 commit comments