Configuration
Publish
Optionally, you can publish the config file with:
php artisan vendor:publish --tag="localization"
This is the contents of the published config/localization.php
file:
return [
/*
* Determines what type of files to use when updating language files.
*
* @see https://laravel-lang.com/configuration.html#inline
*
* By default, `false`.
*/
'inline' => (bool) env('LOCALIZATION_INLINE', env('LANG_PUBLISHER_INLINE')),
/*
* Do arrays need to be aligned by keys before processing arrays?
*
* @see https://laravel-lang.com/configuration.html#alignment
*
* By default, true
*/
'align' => (bool) env('LOCALIZATION_ALIGN', env('LANG_PUBLISHER_ALIGN', true)),
/*
* The language codes chosen for the files in this repository may not
* match the preferences for your project.
*
* Specify here mappings of localizations with your project.
*
* @see https://laravel-lang.com/configuration.html#aliases
*/
'aliases' => [
// \LaravelLang\LocaleList\Locale::German->value => 'de-DE',
// \LaravelLang\LocaleList\Locale::GermanSwitzerland->value => 'de-CH',
],
/*
* This option determines the mechanism for converting translation
* keys into a typographic version.
*
* @see https://laravel-lang.com/configuration.html#smart_punctuation
*
* By default, false
*/
'smart_punctuation' => [
'enable' => (bool) env('LOCALIZATION_SMART_ENABLED', false),
'common' => [
'double_quote_opener' => '“',
'double_quote_closer' => '”',
'single_quote_opener' => '‘',
'single_quote_closer' => '’',
],
'locales' => [
Locale::French->value => [
'double_quote_opener' => '« ',
'double_quote_closer' => ' »',
'single_quote_opener' => '‘',
'single_quote_closer' => '’',
],
Locale::Russian->value => [
'double_quote_opener' => '«',
'double_quote_closer' => '»',
'single_quote_opener' => '‘',
'single_quote_closer' => '’',
],
Locale::Ukrainian->value => [
'double_quote_opener' => '«',
'double_quote_closer' => '»',
'single_quote_opener' => '‘',
'single_quote_closer' => '’',
],
Locale::Belarusian->value => [
'double_quote_opener' => '«',
'double_quote_closer' => '»',
'single_quote_opener' => '‘',
'single_quote_closer' => '’',
],
],
],
/*
* This option defines the application's route settings.
*
* @see https://laravel-lang.com/configuration.html#routes
*/
'routes' => [
/*
* This option defines the settings for the key names used when working with application routing.
*
* Default values:
*
* parameter - locale
* header - Accept-Language
* cookie - Accept-Language
* session - Accept-Language
* column - column
*/
'names' => [
'parameter' => RouteName::Parameter,
'header' => RouteName::Header,
'cookie' => RouteName::Cookie,
'session' => RouteName::Session,
'column' => RouteName::Column,
],
/*
* This option specifies the prefix of route group names.
*
* By default, `localized.`
*/
'name_prefix' => env('LOCALIZATION_NAME_PREFIX', 'localized.'),
/*
* This option specifies the request redirection option when trying to open the default localization.
*
* Applies when using the `LaravelLang\Routes\Facades\LocalizationRoute` facade.
*/
'redirect_default' => (bool) env('LOCALIZATION_REDIRECT_DEFAULT', false),
],
/*
* This option defines settings for working with model translations.
*
* @see https://laravel-lang.com/configuration.html#models
*/
'models' => [
/*
* This option specifies a suffix for models containing translations.
*
* For example,
* main model is `App\Models\Page`
* translation model is `App\Models\PageTranslation`
*
* By default, `Translation`
*/
'suffix' => 'Translation',
/*
* This option determines the need to filter localizations loaded
* in the relay when using eager loading.
*
* By default, true.
*/
'filter' => [
'enabled' => (bool) env('LOCALIZATION_FILTER_ENABLED', true),
],
/*
* This option specifies a folder to store helper files for the IDE.
*
* By default, `vendor/_laravel_lang`
*/
'helpers' => env('VENDOR_PATH', base_path('vendor/_laravel_lang')),
],
/*
* This option contains a list of translators that the Laravel Lang Translator project works with.
*
* Google Translate is enabled by default.
*
* @see https://laravel-lang.com/configuration.html#translators
*/
'translators' => [
/*
* List of channels used for translations.
*
* By default,
*
* Google is enabled
* Deepl is disabled
* Yandex is disabled
*/
'channels' => [
'google' => [
'translator' => '\LaravelLang\Translator\Integrations\Google',
'enabled' => (bool) env('TRANSLATION_GOOGLE_ENABLED', true),
'priority' => (int) env('TRANSLATION_GOOGLE_PRIORITY', 1),
],
'deepl' => [
'translator' => '\LaravelLang\Translator\Integrations\Deepl',
'enabled' => (bool) env('TRANSLATION_DEEPL_ENABLED', false),
'priority' => (int) env('TRANSLATION_DEEPL_PRIORITY', 2),
'credentials' => [
'key' => (string) env('TRANSLATION_DEEPL_KEY'),
],
],
'yandex' => [
'translator' => '\LaravelLang\Translator\Integrations\Yandex',
'enabled' => (bool) env('TRANSLATION_YANDEX_ENABLED', false),
'priority' => (int) env('TRANSLATION_YANDEX_PRIORITY', 3),
'credentials' => [
'key' => (string) env('TRANSLATION_YANDEX_KEY'),
'folder' => (string) env('TRANSLATION_YANDEX_FOLDER_ID'),
],
],
],
'options' => [
/*
* Set a custom pattern for extracting replaceable keywords from the string,
* default to extracting words prefixed with a colon.
*
* Available options:
*
* `true` is a `/:(\w+)/`
* `false` will disable regular expression processing
* `/any regex/` - any regular expression you specify
*
* By default, `true`
*
* @example (e.g. "Hello :name" will extract "name")
*/
'preserve_parameters' => true,
],
],
];
Inline
The inline
option determines what type of files to use when updating language files.
For example, the difference between them can be seen here:
The :attribute must be accepted. // default
This field must be accepted. // inline
This option is available in the config/localization.php
file:
return [
'inline' => (bool) env('LOCALIZATION_INLINE', env('LANG_PUBLISHER_INLINE')),
];
Alignment
The align
option defines the alignment of values relative to each other when processing localization files.
When updating files, all comments from the final files are automatically deleted. Unfortunately, var_export does not know how to work with comments.
Your file example:
return [
/*
|--------------------------------------------------------------------------
| Authentication Language Lines
|--------------------------------------------------------------------------
|
| The following language lines are used during authentication for various
| messages that we need to display to the user. You are free to modify
| these language lines according to your application's requirements.
|
*/
'failed' => 'These credentials do not match our records 123456.',
'throttle' => 'Too many login attempts. Please try again in :seconds seconds.',
'foo' => 'bar',
];
An updated file like this:
return [
'failed' => 'These credentials do not match our records 123456.',
'throttle' => 'Too many login attempts. Please try again in :seconds seconds.',
'foo' => 'bar',
];
and example of validation.php
file:
return [
/*
|--------------------------------------------------------------------------
| Validation Language Lines
|--------------------------------------------------------------------------
|
| The following language lines contain the default error messages used by
| the validator class. Some of these rules have multiple versions such
| as the size rules. Feel free to tweak each of these messages here.
|
*/
'accepted' => 'The :attribute must be accepted.',
'active_url' => 'The :attribute is not a valid URL.',
// many rules
'uuid' => 'The :attribute must be a valid UUID.',
/*
|--------------------------------------------------------------------------
| Custom Validation Language Lines
|--------------------------------------------------------------------------
|
| Here you may specify custom validation messages for attributes using the
| convention "attribute.rule" to name the lines. This makes it quick to
| specify a specific custom language line for a given attribute rule.
|
*/
'custom' => [
'name' => [
'required' => 'Custom message 1',
'string' => 'Custom message 2',
],
],
/*
|--------------------------------------------------------------------------
| Custom Validation Attributes
|--------------------------------------------------------------------------
|
| The following language lines are used to swap our attribute placeholder
| with something more reader friendly such as "E-Mail Address" instead
| of "email". This simply helps us make our message more expressive.
|
*/
'attributes' => [
'name' => 'Foo',
'bar' => 'Bar',
'baz' => 'Baz',
],
];
This is what it will look like after the update:
return [
'accepted' => 'The :attribute must be accepted.',
'active_url' => 'The :attribute is not a valid URL.',
'uuid' => 'The :attribute must be a valid UUID.',
'custom' => [
'name' => [
'required' => 'Custom message 1',
'string' => 'Custom message 2',
],
],
'attributes' => [
'name' => 'Foo',
'bar' => 'Bar',
'baz' => 'Baz',
],
];
This option is available in the config/localization.php
file:
return [
'align' => (bool) env('LOCALIZATION_ALIGN', env('LANG_PUBLISHER_ALIGN', true)),
];
Aliases
If you want to name codes differently, such as de-DE
instead of de
or de-CH
instead of de_CH
, you can define aliases
in the configuration file.
After that, all adding, updating, and deleting actions will automatically use the specified aliases in the config/localization.php
config file:
use LaravelLang\LocaleList\Locale;
return [
'aliases' => [
Locale::German->value => 'de-DE',
Locale::GermanSwitzerland->value => 'de-CH',
],
];
If the config/localization.php
file already exists, then you can add the aliases key in it and specify the desired aliases
using a data array.
After this, you can, for example, add new localizations by specifying both the main code and its alias:
php artisan lang:add de de_CH
Installed locales will use the specified aliases as folder names for PHP translations and file names for JSON translations:
lang
de-CH
de-DE
This option is available in the config/localization.php
file:
return [
'aliases' => [
// \LaravelLang\LocaleList\Locale::German->value => 'de-DE',
// \LaravelLang\LocaleList\Locale::GermanSwitzerland->value => 'de-CH',
],
];
Smart Punctuation
When updating translation keys, you can also enable intelligent converts ASCII quotes, dashes, and ellipses to their Unicode.
For example:
{
"Some": "\"It's super-configurable... you can even use additional extensions to expand its capabilities -- just like this one!\""
}
Will result in files:
{
"Some": "“It’s super-configurable… you can even use additional extensions to expand its capabilities – just like this one!”"
}
This option is available in the config/localization.php
file:
use LaravelLang\LocaleList\Locale;
return [
'smart_punctuation' => [
'enable' => (bool) env('LOCALIZATION_SMART_ENABLED', false),
'common' => [
'double_quote_opener' => '“',
'double_quote_closer' => '”',
'single_quote_opener' => '‘',
'single_quote_closer' => '’',
],
'locales' => [
Locale::French->value => [
'double_quote_opener' => '« ',
'double_quote_closer' => ' »',
'single_quote_opener' => '‘',
'single_quote_closer' => '’',
],
Locale::Russian->value => [
'double_quote_opener' => '«',
'double_quote_closer' => '»',
'single_quote_opener' => '‘',
'single_quote_closer' => '’',
],
Locale::Ukrainian->value => [
'double_quote_opener' => '«',
'double_quote_closer' => '»',
'single_quote_opener' => '‘',
'single_quote_closer' => '’',
],
Locale::Belarusian->value => [
'double_quote_opener' => '«',
'double_quote_closer' => '»',
'single_quote_opener' => '‘',
'single_quote_closer' => '’',
],
],
],
];
You can also set different rules for any localization.
By default, conversion is disabled.
Routes
When using the package to work with routing, you can override the default values of key names.
This option is enabled in the config/localization.php
file:
use LaravelLang\Config\Constants\RouteName;
return [
'routes' => [
'names' => [
'parameter' => RouteName::Parameter,
'header' => RouteName::Header,
'cookie' => RouteName::Cookie,
'session' => RouteName::Session,
'column' => RouteName::Column,
],
'name_prefix' => env('LOCALIZATION_NAME_PREFIX', 'localized.'),
'redirect_default' => (bool) env('LOCALIZATION_REDIRECT_DEFAULT', false),
],
];
Models
You can define default parameter values when using the Models package.
This setting available in the config/localization.php
file:
return [
/*
* This option defines settings for working with model translations.
*
* @see https://laravel-lang.com/configuration.html#models
*/
'models' => [
/*
* This option specifies a suffix for models containing translations.
*
* For example,
* main model is `App\Models\Page`
* translation model is `App\Models\PageTranslation`
*
* By default, `Translation`
*/
'suffix' => 'Translation',
/*
* This option determines the need to filter localizations loaded
* in the relay when using eager loading.
*
* By default, true.
*/
'filter' => [
'enabled' => (bool) env('LOCALIZATION_FILTER_ENABLED', true),
],
/*
* This option specifies a folder to store helper files for the IDE.
*
* By default, `vendor/_laravel_lang`
*/
'helpers' => env('VENDOR_PATH', base_path('vendor/_laravel_lang')),
],
];
The suffix
parameter is used to specify the suffix of the models to which the translation repository belongs.
For example:
App\Models\Post::class
App\Models\PostTranslation::class
Translator
Here you can not only activate default translators, but also create your own. You can read more about this on the Translator package information page.
This setting available in the config/localization.php
file:
return [
/*
* This option contains a list of translators that the Laravel Lang Translator project works with.
*
* Google Translate is enabled by default.
*
* @see https://laravel-lang.com/configuration.html#translators
*/
'translators' => [
/*
* List of channels used for translations.
*
* By default,
*
* Google is enabled
* Deepl is disabled
* Yandex is disabled
*/
'channels' => [
'google' => [
'translator' => '\LaravelLang\Translator\Integrations\Google',
'enabled' => (bool) env('TRANSLATION_GOOGLE_ENABLED', true),
'priority' => (int) env('TRANSLATION_GOOGLE_PRIORITY', 1),
],
'deepl' => [
'translator' => '\LaravelLang\Translator\Integrations\Deepl',
'enabled' => (bool) env('TRANSLATION_DEEPL_ENABLED', false),
'priority' => (int) env('TRANSLATION_DEEPL_PRIORITY', 2),
'credentials' => [
'key' => (string) env('TRANSLATION_DEEPL_KEY'),
],
],
'yandex' => [
'translator' => '\LaravelLang\Translator\Integrations\Yandex',
'enabled' => (bool) env('TRANSLATION_YANDEX_ENABLED', false),
'priority' => (int) env('TRANSLATION_YANDEX_PRIORITY', 3),
'credentials' => [
'key' => (string) env('TRANSLATION_YANDEX_KEY'),
'folder' => (string) env('TRANSLATION_YANDEX_FOLDER_ID'),
],
],
],
'options' => [
/*
* Set a custom pattern for extracting replaceable keywords from the string,
* default to extracting words prefixed with a colon.
*
* Available options:
*
* `true` is a `/:(\w+)/`
* `false` will disable regular expression processing
* `/any regex/` - any regular expression you specify
*
* By default, `true`
*
* @example (e.g. "Hello :name" will extract "name")
*/
'preserve_parameters' => true,
],
],
];
Last modified: 31 August 2024