Skip to content

Commit

Permalink
Adds information about registering additional directories (#633)
Browse files Browse the repository at this point in the history
* Adds information about registering additional directories

closes #617

* fix
  • Loading branch information
butschster authored Nov 6, 2023
1 parent 59000cf commit 61b45d9
Showing 1 changed file with 69 additions and 11 deletions.
80 changes: 69 additions & 11 deletions docs/en/advanced/i18n.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Register the bootloader `I18nBootloader` in your application.
```php app/src/Application/Kernel.php
protected const LOAD = [
// ...
\SpiralBootloader\I18nBootloader::class,
\Spiral\Bootloader\I18nBootloader::class,
// ...
];
```
Expand Down Expand Up @@ -50,39 +50,96 @@ To change the locale, use the `setLocale` method.
## Configuration

The framework supplies a default configuration automatically, inside `I18nBootloader`. To alter the configuration,
create the `app/config/translator.php` file:
The framework supplies a default configuration automatically, inside `Spiral\Bootloader\I18nBootloader`. If you need
custom settings, you can define them in `app/config/translator.php`:

### Configuration File

Here's what a typical configuration might look like:

```php app/config/translator.php
use Symfony\Component\Translation\Dumper;
use Symfony\Component\Translation\Loader;

return [
'locale' => env('LOCALE', 'en'),
'locale' => env('LOCALE', 'en'),
'fallbackLocale' => env('LOCALE', 'en'),
'directory' => directory('locale'),
'autoRegister' => env('DEBUG', true),
'directory' => directory('locale'),
'autoRegister' => env('DEBUG', true),
// available locale loaders (the key is extension)
'loaders' => [
'loaders' => [
'php' => Loader\PhpFileLoader::class,
'po' => Loader\PoFileLoader::class,
'csv' => Loader\CsvFileLoader::class,
'json' => Loader\JsonFileLoader::class
],
// export methods
'dumpers' => [
'dumpers' => [
'php' => Dumper\PhpFileDumper::class,
'po' => Dumper\PoFileDumper::class,
'csv' => Dumper\CsvFileDumper::class,
'json' => Dumper\JsonFileDumper::class,
],
'domains' => [
'domains' => [
// by default we can store all messages in one domain
'messages' => ['*']
]
];
```

**Key Points:**

1. **Locale Settings:** The `locale` is your primary language, while `fallbackLocale` is the default language the app
will use if the specified one isn't available.
2. **Directory Setting:** This is where the language files reside. By default, it's set to the `app/locale` directory.
3. **Loaders and Dumpers:** These are classes that handle reading from and writing to translation files.

### Changing the Directory for Locale Files

If you want to use a different directory for your language files, modify the `directory` option:

```php app/config/translator.php
return [
'directory' => directory('locale'),
];
```

Sometimes, you might need translations from more than one location. For example, when you're using external packages
that bring their own translations.

To specify multiple directories use `directories`:

```php app/config/translator.php
return [
'directory' => directory('locale'),
// ...
'directories' => [
directory('vendor/package/name/locale'),
...
],
// ...
];
```

You can also add new directories using the `Spiral\Bootloader\I18nBootloader` bootloader:

```php
use Spiral\Boot\Bootloader\Bootloader;
use Spiral\Bootloader\I18nBootloader;

final class AppBootloader extends Bootloader
{
public function init(I18nBootloader $i18n): void
{
$i18n->addDirectory('some/directory');
}
}
```

> **Note**
> If there's a conflict between translation files, the ones in the main application will take priority over additional
> directories.
## Import and Export

It is possible to export application locale bundles into the following formats:
Expand Down Expand Up @@ -127,7 +184,7 @@ or `JSON` formats.

## View Localization

Spiral includes a view process available for `Twig` and `Stempler` engines to translate view source code. The
Spiral includes a view process available for `Twig` and `Stempler` engines to translate view source code. The
translated view will be stored in a separate view cache and it provides the ability to translate views without any
performance penalty.

Expand Down Expand Up @@ -184,7 +241,8 @@ class HomeController
### Class messages

In cases where a message is defined by logic and can not be indexed, use constants or properties to declare class messages, every string wrapped with `[[]]` will be automatically indexed.
In cases where a message is defined by logic and can not be indexed, use constants or properties to declare class
messages, every string wrapped with `[[]]` will be automatically indexed.

```php
class HomeController
Expand Down

0 comments on commit 61b45d9

Please sign in to comment.