Merge pull request #4149 from Icinga/feature/load-L10n-locales

Load L10n locales
This commit is contained in:
Johannes Meyer 2020-05-12 12:32:33 +02:00 committed by GitHub
commit 89c340522a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 44 deletions

View File

@ -65,16 +65,13 @@ More details about this module can be found in [this chapter](../modules/doc/doc
## Translation <a id="about-translation"></a> ## Translation <a id="about-translation"></a>
With the translation module every piece of text in the user interface (of the framework itself and any module) can be translated to a language of your choice. Icinga Web 2 and all modules by Icinga utilize gettext to provide translations into other languages from the default
English (en_US). However, the actual language specific files (locales) are not separately included in every project.
Currently provided languages: Icinga uses a central repository to manage locales: https://github.com/Icinga/L10n
* German
* Italian
* Portuguese
More details about this module can be found in [this chapter](../modules/translation/doc/01-About.md#translation-module-about).
If you want to provide or update a translation for your own language, please head over there where you will find
[instructions](https://github.com/Icinga/L10n/blob/master/CONTRIBUTING.md) on how to contribute.
## Accessibility <a id="about-accessibility"></a> ## Accessibility <a id="about-accessibility"></a>

View File

@ -75,6 +75,13 @@ abstract class ApplicationBootstrap
*/ */
protected $configDir; protected $configDir;
/**
* Locale directory
*
* @var string
*/
protected $localeDir;
/** /**
* Common storage directory * Common storage directory
* *
@ -663,7 +670,16 @@ abstract class ApplicationBootstrap
*/ */
public function getLocaleDir() public function getLocaleDir()
{ {
return $this->getApplicationDir('locale'); if ($this->localeDir === null) {
$L10nLocales = getenv('ICINGAWEB_LOCALEDIR') ?: '/usr/share/icinga-L10n/locale';
if (file_exists($L10nLocales) && is_dir($L10nLocales)) {
$this->localeDir = $L10nLocales;
} else {
$this->localeDir = $this->getApplicationDir('locale');
}
}
return $this->localeDir;
} }
/** /**

View File

@ -11,7 +11,7 @@ part on localizing Icinga Web 2 for different languages and how to use the
## Translation for Developers <a id="module-translation-developers"></a> ## Translation for Developers <a id="module-translation-developers"></a>
To make use of the built-in translations in your applications code or views, you should use the method To make use of the built-in translations in your module's code or views, you should use the method
`$this->translate('String to be translated')`, let's have a look at an example: `$this->translate('String to be translated')`, let's have a look at an example:
```php ```php
@ -71,7 +71,7 @@ class ExampleController extends Controller
{ {
public function indexAction() public function indexAction()
{ {
$this->view->title = $this->translate('My Titile', 'mycontext'); $this->view->title = $this->translate('My Title', 'mycontext');
$this->view->message = $this->translatePlural('Service', 'Services', 3, 'mycontext'); $this->view->message = $this->translatePlural('Service', 'Services', 3, 'mycontext');
} }
} }
@ -79,6 +79,12 @@ class ExampleController extends Controller
## Translation for Translators <a id="module-translation-translators"></a> ## Translation for Translators <a id="module-translation-translators"></a>
> **Note**:
>
> If you want to translate Icinga Web 2 or any module made by Icinga, please head over to
> [Icinga/L10n](https://github.com/Icinga/L10n) instead. We won't accept any contributions
> in this regard other than those made there.
Icinga Web 2 internally uses the UNIX standard gettext tool to perform internationalization, this means translation Icinga Web 2 internally uses the UNIX standard gettext tool to perform internationalization, this means translation
files in the .po file format are supplied for text strings used in the code. files in the .po file format are supplied for text strings used in the code.
@ -108,11 +114,14 @@ When you are done, just save your new settings.
#### Editing .po files <a id="module-translation-translators-poedit-edit-po-files"></a> #### Editing .po files <a id="module-translation-translators-poedit-edit-po-files"></a>
To work with Icinga Web 2 .po files, you can open for e.g. the German icinga.po file which is located under > **Note**
`application/locale/de_DE/LC_MESSAGES/icinga.po`, as shown below, you will get then a full list of all available >
translation strings for the core application. Each module names its translation files `%module_name%.po`. For a > ll_CC stands for ll=language and CC=country code for e.g `de_DE`, `fr_FR`, `ru_RU`, `it_IT` etc.
module called `yourmodule` the .po translation file will be named `yourmodule.po`.
To work with .po files, open or create the one for your language located under
`application/locale/ll_CC/LC_MESSAGES/yourmodule.po`. As shown below, you will
get then a full list of all available translation strings for the module. Each
module names its translation files `%module_name%.po`.
![Full list of strings](img/poedit_004.png) ![Full list of strings](img/poedit_004.png)
@ -135,45 +144,20 @@ If you want to try out your translation changes in Icinga Web 2, you can make us
To get an easier development with translations, you can activate the `translation module` which provides CLI commands, To get an easier development with translations, you can activate the `translation module` which provides CLI commands,
after that you would be able to refresh and compile your .po files. after that you would be able to refresh and compile your .po files.
> **Note**
>
> The ll_CC stands for ll=language and CC=country code for e.g `de_DE`, `fr_FR`, `ru_RU`, `it_IT` etc.
### Application <a id="module-translation-tests-application"></a>
To refresh the `icinga.po` file:
```
icingacli translation refresh icinga ll_CC
```
And to compile it:
```
icingacli translation compile icinga ll_CC
```
> **Note**
>
> After a compile you need to restart the web server to get new translations available in your application.
### Modules <a id="module-translation-tests-modules"></a>
Let's assume, we want to provide German translations for our just new created module `yourmodule`. Let's assume, we want to provide German translations for our just new created module `yourmodule`.
If we haven't yet any translations strings in our .po file or even the .po file, we can use the CLI command, to do the If we haven't yet any translations strings in our .po file or even the .po file, we can use the CLI command, to do the
job for us: job for us:
``` ```
icingacli translation refresh module yourmodule ll_CC icingacli translation refresh module yourmodule de_DE
``` ```
This will go through all .php and .phtml files inside the module and a look after `$this->translate()` if there is This will go through all .php and .phtml files inside the module and a look after `$this->translate()` if there is
something to translate - if there is something and is not available in the `yourmodule.po` it will update this file something to translate - if there is something and is not available in the `yourmodule.po` it will update this file
for us with new strings. for us with new strings.
Now you can open the `yourmodule.po` and you will see something similar: Now you can open the `application/locale/de_DE/LC_MESSAGES/yourmodule.po` and you will see something similar:
``` ```
# Icinga Web 2 - Head for multiple monitoring backends. # Icinga Web 2 - Head for multiple monitoring backends.
@ -210,8 +194,12 @@ msgstr "Attrappe"
The last step is to compile the `yourmodule.po` to the `yourmodule.mo`: The last step is to compile the `yourmodule.po` to the `yourmodule.mo`:
``` ```
icingacli translation compile module yourmodule ll_CC icingacli translation compile module yourmodule de_DE
``` ```
At this moment, everywhere in the module where the `Dummy` should be translated, it would returns the translated > **Note**
>
> After compiling it you need to restart the web server to get new translations available in your module.
At this moment, everywhere in the module where the `Dummy` should be translated, it would return the translated
string `Attrappe`. string `Attrappe`.