jrmajor/laravel-fluent
jrmajor/laravel-fluent
Unleash the expressive power of the natural language in your Laravel application with Project Fluent, a localization system designed by Mozilla.
Read the Fluent Syntax Guide or try it out in the Fluent Playground to learn more about the syntax.
shared-photos =
{ $userName } { $photoCount ->
[one] added a new photo
*[other] added { $photoCount } new photos
} to { $userGender ->
[male] his stream
[female] her stream
*[other] their stream
}.
__('stream.shared-photos', [
'userName' => 'jrmajor',
'photoCount' => 2,
'userGender' => 'male',
]); // jrmajor added 2 new photos to his stream.
This package is a Laravel wrapper around jrmajor/fluent-php.
You may install it via Composer: composer require jrmajor/laravel-fluent. The package will automatically register itself.
Usage
This package replaces default Laravel translator with Major\Fluent\Laravel\FluentTranslator.
app('translator') instanceof Major\Fluent\Laravel\FluentTranslator; // true
Fluent translations are stored in .ftl files. Place them among your .php translation files in your Laravel app:
/resources
/lang
/en
menu.ftl
validation.php
/pl
menu.ftl
validation.php
If there is no Fluent message for a given key, translator will fall back to a .php file, which allows you to introduce Fluent translation format progressively.
Laravel validator uses custom logic for replacing the :attribute variable and requires deeply nested keys, which are not supported in Fluent, so you should leave validation.php file in the default Laravel format.
The trans_choice() helper always falls back to the default translator, as the Fluent format eliminates the need for this function.
You may use the FluentTranslator::addFunction() method to register Fluent functions.
Configuration
Optionally, you can publish the configuration file with this command:
php artisan vendor:publish --tag fluent-config
This will publish the following file in config/fluent.php:
return [
/*
* In strict mode, exceptions will be thrown for syntax errors
* in .ftl files, unknown variables in messages etc.
* It's recommended to enable this setting in development
* to make it easy to spot mistakes.
*/
'strict' => env('APP_ENV', 'production') !== 'production',
/*
* Determines if it should use Unicode isolation marks (FSI, PDI)
* for bidirectional interpolations. You may want to enable this
* behaviour if your application uses right-to-left script.
*/
'use_isolating' => false,
];
Testing
vendor/bin/phpunit # Tests
vendor/bin/phpstan analyze # Static analysis
vendor/bin/php-cs-fixer fix # Formatting
Related Packages
Runtime pseudolocalization for Laravel. Flip a flag, see every i18n bug: transla...
Languara localization plugin for Laravel 5.x. Easily upload, translate, and sync...
Languara localization plugin for Laravel 4.x. Easily upload, translate, and sync...
Package for convenient work with Laravel's localization features
Version History
| Version | Released | PHP | Laravel | License |
|---|---|---|---|---|
| v1.4.0 | 8.4 - 8.5 | ^12.0 || | MIT | |
| v1.3.0 | 8.3 - 8.5 | ^11.0 || | MIT | |
| v1.2.0 | 8.3 - 8.4 | ^11.0 || | MIT | |
| v1.1.1 | 8.2 - 8.4 | ^11.0 | MIT | |
| v1.1.0 | 8.2 - 8.3 | ^10.0 || | MIT | |
| v1.0.1 | 8.1 - 8.3 | ^10.0 | MIT | |
| v1.0.0 | 8.1 - 8.2 | ^10.0 | MIT | |
| v0.2.1 | 8.1 - 8.2 | ^9.0 || | MIT | |
| v0.2.0 | 8.1 - 8.2 | ^9.0 | MIT | |
| v0.1.5 | ~8.0.0 || | ^8.0 || | MIT | |
| v0.1.4 | ~8.0.0 || | ^8.0 || | MIT | |
| v0.1.3 | ~8.0.0 || | ^8.0 || | MIT | |
| v0.1.2 | ^8.0 | ^8.0 | MIT | |
| v0.1.1 | ^8.0 | ^8.0 | MIT | |
| v0.1.0 | ^8.0 | ^8.0 | MIT |