|
| 1 | +# Localization (L10n) |
| 2 | + |
| 3 | +Localization is a crucial aspect of plugin development, as it allows your plugin to be used by a global audience. |
| 4 | + |
| 5 | +## Laravel basics |
| 6 | +### Setting the default locale |
| 7 | +The default locale is set in the application configuration file, which is located at `config/app.php`. The default locale is defined by the `locale` key. |
| 8 | + |
| 9 | +You can also change the default locale by setting the `APP_LOCALE` environment variable in the `.env` file. |
| 10 | + |
| 11 | +### Getting the current locale |
| 12 | +You can get the current locale using the `app()->getLocale()` method. |
| 13 | + |
| 14 | +### Changing the current locale |
| 15 | +You can change the current locale using the `app()->setLocale()` method. |
| 16 | + |
| 17 | +Read more on the Laravel documentation localization page [here](https://laravel.com/docs/localization). |
| 18 | + |
| 19 | +## Available locales |
| 20 | +The first thing to note is that the available locales are provided with the common package; therefore, you must set your locales in the `eclipse-common` config file. |
| 21 | + |
| 22 | +```php |
| 23 | + /* |
| 24 | + * Available locales for the application. |
| 25 | + * Can be a list (array) of locale codes or a closure that returns such an array. |
| 26 | + */ |
| 27 | + 'available_locales' => function () { |
| 28 | + return ['en', 'sl']; |
| 29 | + }, |
| 30 | +``` |
| 31 | +As the comment above states, you can either provide a list of locales or a closure that returns such a list if you need to dynamically set the locales. |
| 32 | + |
| 33 | +## L10nHelper |
| 34 | +The `L10nHelper` class is provided by the common package and provides a set of static methods for localization. |
| 35 | + |
| 36 | +### `getAvailableLocales()` |
| 37 | +The `getAvailableLocales()` method returns an associative array of available locales, where the element key is always the same as the value. |
| 38 | + |
| 39 | +**Example:** |
| 40 | +```php |
| 41 | +[ |
| 42 | + 'en' => 'en', |
| 43 | + 'sl' => 'sl', |
| 44 | +] |
| 45 | +``` |
| 46 | + |
| 47 | +### `getLocaleOptions()` |
| 48 | +The `getLocaleOptions()` method returns an associative array of locales, where the element key is the locale code and the element value is the language name. This is useful for building select boxes or similar UI elements. |
| 49 | + |
| 50 | +**Example:** |
| 51 | +```php |
| 52 | +[ |
| 53 | + 'en' => 'English', |
| 54 | + 'sl' => 'Slovenian', |
| 55 | +] |
| 56 | +``` |
| 57 | +::: tip IMPORTANT |
| 58 | +The language name is provided by the `intl` PHP extension; therefore, you must have it installed to use this method. |
| 59 | +::: |
| 60 | + |
| 61 | +### `getLanguageName(string $code, bool $include_code = false)` |
| 62 | +The `getLanguageName()` method returns the language name from the language code. |
| 63 | + |
| 64 | +**Examples:** |
| 65 | +```php |
| 66 | +L10nHelper::getLanguageName('en') // English |
| 67 | +L10nHelper::getLanguageName('sl') // Slovenian |
| 68 | +L10nHelper::getLanguageName('sl', true) // Slovenian (sl) |
| 69 | +L10nHelper::getLanguageName('xx') // xx |
| 70 | +``` |
| 71 | +1. If the `intl` PHP extension is installed, the method uses the `\Locale` class to get the language name. |
| 72 | +2. Otherwise, it returns the language code. |
| 73 | + |
0 commit comments