How to use
At the top level, a Manager
is responsible for keeping track of your exporters.
Obtain Manager
The translation exporter Manger
can be obtained via the TranslationsExporterManagerTrait
.
use Aedart\Translation\Traits\TranslationsExporterManagerTrait;
class MyController
{
use TranslationsExporterManagerTrait;
public function index()
{
$manager = $this->getTranslationsExporterManager();
// ..remaining not shown...
}
}
Once you have your manager, you can request an Exporter
instance, which will perform the actual exporting of translations.
$exporter = $manager->profile(); // Default profile
To obtain a specific Exporter
, specify the profile name as argument.
$exporter = $manager->profile('lang_js');
Export Translations
To export the application's translations, invoke the export()
method. It accepts the following arguments:
string|array $locales
(optional) Locales to export. Wildcard (*) = all locales.string|array $groups
(optional) Groups to export. Wildcard (*) = all groups, incl. namespaced groups, e.g.'courier::messages'
.
// All available translations, for all locales...
$all = $exporter->export();
// English translations only...
$englishOnly = $exporter->export('en');
// En-uk, of the auth and validation groups
$uk = $exporter->export('en-uk', [ 'auth', 'validation' ]);
// Namespaced group (from a package)...
$acme = $exporter->export('be', 'acme::users');
The output of the export()
method depends on the chosen profile's driver.
Paths and Translations
Available locales and groups are automatically detected, based on the paths
option (in your config/translations-exporter.php
), as well as registered namespaced translations and paths to JSON files, in Laravel's translation loader.
If you wish to export translations from 3rd party packages, then you have the following options:
- Register 3rd party service provider to load translations.
- Publish packages' resource (if translations are published)
- Or, manual registration of translations (see below)
To use translations from packages, without having to register their service providers, you can register them in the configuration:
<?php
return [
// ...previous not shown...
/*
|--------------------------------------------------------------------------
| Namespaces and Json
|--------------------------------------------------------------------------
|
| Register namespaced translations and paths to JSON translations. Use this
| when you want to use 3rd part translations without having to register
| their service providers.
*/
'namespaces' => [
'acme' => realpath(__DIR__ . '/../vendor/acme/package/lang'),
],
'json' => [
realpath(__DIR__ . '/../vendor/acme/package/lang')
],
// ...remaining not shown ...
];
Consequently, when you manually register namespaced translations and paths to JSON translations, then these will be made available in your application!