Skip to content

jrmajor/laravel-fluent

Repository files navigation

jrmajor/laravel-fluent

Latest Stable Version Required PHP Version

Unleash the expressive power of the natural language in your Laravel application. Read the Fluent Syntax Guide in order to learn more about syntax.

This package is a Laravel wrapper around jrmajor/fluent-php.

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.

You may install it via Composer: composer require jrmajor/laravel-fluent. It supports Laravel 8.0 and 9.0.

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 given key, translator will fall back to .php file, which allows you to introduce Fluent translation format progressively.

Laravel validator uses custom logic for replacing :attribute variable and requires deeply nested keys, which are not supported in Fluent, so you should leave validation.php file in default Laravel format.

trans_choice() helper always falls back to default translator, as Fluent format eliminates the need for another function.

Installation

You may install the package via Composer:

composer require jrmajor/laravel-fluent

The package will automatically register itself.

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' => ! app()->isProduction(),

    /*
     * 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 --testdox  # Tests
vendor/bin/phpstan analyse    # Static analysis
vendor/bin/php-cs-fixer fix   # Formatting

About

Fluent translations for Laravel

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •