Skip to content

Commit 00447a4

Browse files
Merge pull request #9309 from nextcloud/feature/noid/document-IFactory-getUserLanguage
Document `IFactory::getUserLanguage()`
2 parents 5d5bdd9 + 17cb5ca commit 00447a4

File tree

1 file changed

+44
-18
lines changed
  • developer_manual/basics/front-end

1 file changed

+44
-18
lines changed

developer_manual/basics/front-end/l10n.rst

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,10 @@ Translation
55
.. sectionauthor:: Bernhard Posselt <dev@bernhard-posselt.com>, Kristof Hamann
66

77
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-
9-
10-
Make text translatable
11-
----------------------
12-
138
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).
149

15-
1610
PHP
17-
^^^
11+
---
1812

1913
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:
2014

@@ -24,7 +18,7 @@ If localized strings are used in the backend code, simply inject the ``\OCP\IL10
2418
<?php
2519
namespace OCA\MyApp\Service;
2620
27-
use \OCP\IL10N;
21+
use OCP\IL10N;
2822
2923
3024
class AuthorService {
@@ -76,24 +70,54 @@ Strings can then be translated in the following way:
7670
}
7771
7872
Correct plurals
79-
"""""""""""""""
73+
^^^^^^^^^^^^^^^
8074

8175
If you use a plural, you **must** also use the ``%n`` placeholder. The placeholder defines the plural and the word without the number preceding is wrong. If you don't know/have a number for your translation, e.g. because you don't know how many items are going to be selected, just use an undefined plural. They exist in every language and have one form. They do not follow the normal plural pattern.
8276

8377
Example:
8478

8579
.. code-block:: php
8680
81+
<?php
82+
8783
// BAD: Plural without count
8884
$title = $l->n('Import calendar', 'Import calendars', $selectionLength)
8985
// BETTER: Plural has count, but disrupting to read and unnecessary information
9086
$title = $l->n('Import %n calendar', 'Import %n calendars', $selectionLength)
9187
// BEST: Simple string with undefined plural
9288
$title = $l->t('Import calendars')
9389
90+
Language of other users
91+
^^^^^^^^^^^^^^^^^^^^^^^
92+
93+
If you need to get the language of another user, e.g. to send them an email or inside a background job, there are also
94+
the ``force_language`` and ``default_language`` configuration options to consider. To make this easier, the
95+
``OCP\L10N\IFactory`` class comes with a ``getUserLanguage`` method:
96+
97+
.. code-block:: php
98+
99+
<?php
100+
101+
use OCP\L10N\IFactory;
102+
class SendEmail {
103+
104+
/** @var IFactory */
105+
private $l10nFactory;
106+
107+
public function __construct(IFactory $l10nFactory) {
108+
$this->l10nFactory = $l10nFactory;
109+
}
110+
111+
public function send(IUser $user): void {
112+
$lang = $this->l10nFactory->getUserLanguage($user);
113+
$l = $this->l10nFactory->get('myapp', $lang);
114+
115+
// …
116+
}
117+
94118
95119
Templates
96-
^^^^^^^^^
120+
---------
97121

98122
In every template the global variable **$l** can be used to translate the strings using its methods **t()** and **n()**:
99123

@@ -108,7 +132,7 @@ For the right date format use ``<?php p($l->l('date', time()));?>``.
108132

109133

110134
JavaScript
111-
^^^^^^^^^^
135+
----------
112136

113137
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:
114138

@@ -125,12 +149,12 @@ There are global functions **t()** and **n()** available for translating strings
125149
126150
127151
Important notes
128-
^^^^^^^^^^^^^^^
152+
---------------
129153

130154
Please also look through the following steps to improve your strings and make them better translatable by others
131155

132156
Improving your translations
133-
"""""""""""""""""""""""""""
157+
^^^^^^^^^^^^^^^^^^^^^^^^^^^
134158

135159
You shall **never split** sentences and **never concatenate** two translations (e.g. "Enable" and "dark mode" can not be combined to "Enable dark mode", because languages might have to use different cases)! Translators lose the context and they have no chance to possibly re-arrange words/parts as needed.
136160

@@ -169,8 +193,10 @@ But there is one last problem with this. In case the language has to turn things
169193
170194
This allows translators to have the cloudlink before the browselink in case the language is e.g. right-to-left.
171195

172-
Hints
173-
"""""
196+
.. _Hints:
197+
198+
Provide context hints for translators
199+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
174200

175201
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:
176202

@@ -226,8 +252,6 @@ Adding translations
226252

227253
Nextcloud's translation system is powered by `Transifex <https://www.transifex.com/nextcloud/>`_. To start translating sign up and enter a group. If your community app should be translated by the `Nextcloud community on Transifex <https://www.transifex.com/nextcloud/nextcloud/dashboard/>`_ just follow the setup section below.
228254

229-
230-
231255
Translation tool
232256
^^^^^^^^^^^^^^^^
233257

@@ -248,7 +272,9 @@ Setup of the transifex sync
248272
^^^^^^^^^^^^^^^^^^^^^^^^^^^
249273

250274
To setup the transifex sync within the Nextcloud community you need to add first the
251-
transifex config to your app folder at :file:`.tx/config` (please replace **MYAPP** with your apps id)::
275+
transifex config to your app folder at :file:`.tx/config` (please replace **MYAPP** with your apps id):
276+
277+
.. code-block:: ini
252278
253279
[main]
254280
host = https://www.transifex.com

0 commit comments

Comments
 (0)