2017 © Pedro Peláez
 

library laravel-gettext

Adds localization support to laravel applications in an easy way using Poedit and GNU gettext.

image

belphemur/laravel-gettext

Adds localization support to laravel applications in an easy way using Poedit and GNU gettext.

  • Thursday, March 1, 2018
  • by Belphemur
  • Repository
  • 8 Watchers
  • 38 Stars
  • 40,338 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 61 Forks
  • 1 Open issues
  • 32 Versions
  • 22 % Grown

The README.md

Laravel Gettext

Laravel Gettext is a translation package compatible with the Laravel Framework. It provides a simple way to add localization support to Laravel applications. It is designed to work with GNU gettext and Poedit. Former versions of this package (before 4.x) works with the native php-gettext module. Current versions uses the Symfony translation package by default instead of native php extension., (*1)

Stable build Status laravel-gettext for Laravel 8, (*2)

Stable build Status laravel-gettext for Laravel 6, (*3)

Laravel Latest Long Term Support Version is Laravel 6, we do not recommend using Laravel version < 6, (*4)

1. Requirements

1.1 Optional

1.1.1 APCU

APCU extension installed - http://php.net/manual/en/book.apcu.php, (*5)

If the APCU php extension is installed, the library will use the memory to cache the loaded translation to avoid having to parse the translation file (mo/po) at each request., (*6)

The cache is automatically invalidated when there is a change in the translation file., (*7)

1.1.2 gettext

Optional requirements if you want to use the native php-gettext extension:, (*8)

  • php-gettext - http://www.php.net/manual/en/book.gettext.php
  • GNU gettext on system (and production server!) - https://www.gnu.org/software/gettext/

You will need to update the 'handler' option to 'gettext' in order to use the native php-gettext module., (*9)

2. Install

Once it's installed, Laravel will discover automatically the provider and load it. (Only for 5.5+), (*10)

    composer require zerospam/laravel-gettext

Now you need to publish the configuration file in order to set your own application values:, (*11)

    php artisan vendor:publish

This command creates the package configuration file in: config/laravel-gettext.php., (*12)

You also need to register the LaravelGettext middleware in the app/Http/Kernel.php file:, (*13)

    protected $middlewareGroups = [
        'web' => [
            // ...
            \Xinax\LaravelGettext\Middleware\GettextMiddleware::class,
        ],
        // ...
    ]

Be sure to add the line after Illuminate\Session\Middleware\StartSession, otherwise the locale won't be saved into the session., (*14)

3. Configuration

At this time your application has full gettext support. Now you need to set some configuration values in config/laravel-gettext.php., (*15)

    /**
     * Default locale: this will be the default for your application all
     * localized strings. Is to be supposed that all strings are written
     * on this language.
     */
    'locale' => 'es_ES',
    /**
     * Supported locales: An array containing all allowed languages
     */
    'supported-locales' => array(
        'es_ES',
        'en_US',
        'it_IT',
        'es_AR',
    ),
    /**
     * Default charset encoding.
     */
    'encoding' => 'UTF-8',

Ok, now it's configured. It's time to generate the directory structure and translation files for the first time., (*16)

Make sure you have write permissions on resources/ before you run this command, (*17)

    php artisan gettext:create

With this command the needed directories and files are created on resources/lang/i18n, (*18)

4. Workflow

A. Write strings :D

By default LaravelGettext looks on app/Http/Controllers and resources/views recursively searching for translations. Translations are all texts printed with the _i() function. Let's look a simple view example:, (*19)

    // an example view file
    echo 'Non translated string';
    echo _i('Translated string');
    echo _i('Another translated string');
    // with parameter
    $str = 'parameter';
    $n = 2;
    echo _i('Translated string with %s', $str);
    echo _i('%dnd translated string with %s', [$n, $str]);
    // an example view in blade
    {{ _i('Translated string') }}

Poedit doesn't "understand" blade syntax. When using blade views you must run php artisan gettext:update in order to compile all blade views to plain php before update the translations in Poedit, (*20)

B. Plural strings

The plural translations follow the same pattern above. Plural translations are all texts printed with the _n() function, and it follow the php ngettext. Let's look a simple view example:, (*21)

    // an example view file
    $n = 2;
    echo ($n > 1) ? 'Non translated plural string' : 'Non translated string';
    echo _n('Translated string', 'Translated plural string', $n);
    // with parameter
    $str = 'parameter';
    echo _n('Translated string %s', 'Translated plural string %s', 2, $str);
    // an example view in blade
    {{ _n('Translated string', 'Translated plural string', $n) }}

The Poedit keywords are defined in configuration file with this default pattern:, (*22)

    ['_n:1,2', 'ngettext:1,2']

See Plural forms used by Poedit to configure for your language., (*23)

With Symfony

If you're using Symfony as your translation backend, you have access to their plurals syntax with the _s method. In Poedit it will be considered as a single line instead of a plural., (*24)

    // an example view file
    $n = 2;
    echo ($n > 1) ? 'Non translated plural string' : 'Non translated string';
    echo _s('Translated string|Translated plural string', $n);
    // with parameter
    $str = 'parameter';
    echo _n('Translated string %s|Translated plural string %s', 2, $str);

With symfony complex syntax:, (*25)

    echo _s('{0} There are no apples|{1} There is one apple|]1,Inf[ There are %count% apples', $n);
    // with parameter
    $str = 'red';
    echo _s('{0} There are no %s apples|{1} There is one %s apple|]1,Inf[ There are %count% %s apples', 2, $str);
C. Translate with Poedit

Open the PO file for the language that you want to translate with Poedit. The PO files are located by default in resources/lang/i18n/[locale]/LC_MESSAGES/[domain].po. If you have multiple gettext domains, one file is generated by each domain., (*26)

, (*27)

Once Poedit is loaded press the Update button to load all localized strings. You can repeat this step anytime you add a new localized string., (*28)

Fill translation fields in Poedit and save the file. The first time that you do this the MO files will be generated for each locale., (*29)

C. Runtime methods

To change configuration on runtime you have these methods:, (*30)

    /**
     * Sets the Current locale.
     * Example param value: 'es_ES'
     *
     * @param mixed $locale the locale
     * @return LaravelGettext
     */
    LaravelGettext::setLocale($locale);
    /**
     * Gets the Current locale.
     * Example returned value: 'es_ES'
     *
     * @return String
     */
     LaravelGettext::getLocale();
    /**
     * Gets the language portion of the locale.
     * Eg from en_GB, returns en
     *
     * @return mixed
     */
    LaravelGettext::getLocaleLanguage()
    /**
     * Sets the Current encoding.
     * Example param value: 'UTF-8'
     *
     * @param mixed $encoding the encoding
     * @return LaravelGettext
     */
     LaravelGettext::setEncoding($encoding);
    /**
     * Gets the Current encoding.
     * Example returned value: 'UTF-8'
     *
     * @return String
     */
     LaravelGettext::getEncoding();
    /**
     * Sets the current domain
     *
     * @param String $domain
     */
    LaravelGettext::setDomain($domain);
    /**
     * Returns the current domain
     *
     * @return String
     */
    LaravelGettext::getDomain();
    /**
     * Returns the language selector object
     *
     * @param  Array $labels
     * @return LanguageSelector
     */
    LaravelGettext::getSelector($labels = []);

5. Features and examples:

A. Route and controller implementation example:

app/Http/routes.php, (*31)

    Route::get('/lang/{locale?}', [
        'as'=>'lang',
        'uses'=>'HomeController@changeLang'
    ]);

app/Http/Controllers/HomeController.php, (*32)

    /**
     * Changes the current language and returns to previous page
     * @return Redirect
     */
    public function changeLang($locale=null)
    {
        LaravelGettext::setLocale($locale);
        return Redirect::to(URL::previous());
    }

B. A basic language selector example:

  <ul>
      @foreach(Config::get('laravel-gettext.supported-locales') as $locale)
            <li><a href="/lang/{{$locale}}">{{$locale}}</a></li>
      @endforeach
  </ul>

C. Built-in language selector:

You can use the built-in language selector in your views:, (*33)

    // Plain php:
    LaravelGettext::getSelector()->render();

    // Blade views:
    {!! LaravelGettext::getSelector()->render() !!}

It also supports custom labels:, (*34)

    LaravelGettext::getSelector([
        'en_US' => 'English',
        'es_ES' => 'Spanish',
        'de_DE' => 'Deutsch',
    ])->render();

D. Adding source directories and domains

You can achieve this editing the source-paths configuration array. By default resources/views and app/Http/Controllers are set., (*35)

    /**
     * Paths where Poedit will search recursively for strings to translate.
     * All paths are relative to app/ (don't use trailing slash).
     *
     * Remember to call artisan gettext:update after change this.
     */
    'source-paths' => array(
        'Http/Controllers',
        '../resources/views',
        'foo/bar',              // app/foo/bar
    ),

You may want your translations in different files. Translations in GNUGettext are separated by domains, domains are simply context names., (*36)

Laravel-Gettext set always a default domain that contains all paths that doesn't belong to any domain, its name is established by the 'domain' configuration option., (*37)

To add a new domain just wrap your paths in the desired domain name, like this example:, (*38)

    'source-paths' => array(
        'frontend' => array(
            'Http/Controllers',
            '../resources/views/frontend',
        ),
        'backend' => array(
            '../resources/views/backend',
        ),
        '../resources/views/misc',
    ),

This configuration generates three translation files by each language: messages.po, frontend.po and backend.po, (*39)

To change the current domain in runtime (a route-middleware would be a nice place for do this):, (*40)

    LaravelGettext::setDomain("backend");

Remember: update your gettext files every time you change the 'source-paths' option, otherwise is not necessary., (*41)

    php artisan gettext:update

This command will update your PO files and will keep the current translations intact. After this you can open Poedit and click on update button to add the new text strings in the new paths., (*42)

You can update only the files of a single domain with the same command:, (*43)

    php artisan gettext:update --domain backend

E. About gettext cache (only applies to php-gettext native module)

Sometimes when you edit/add translations on PO files the changes does not appear instantly. This is because the gettext cache system holds content. The most quick fix is restart your web server., (*44)

6. Contributing

If you want to help with the development of this package, you can:, (*45)

  • Warn about errors that you find, in issues section
  • Send me a pull request with your patch
  • Fix my disastrous English in the documentation/comments ;-)
  • Make a fork and create your own version of laravel-gettext
  • Give a star!

7. Upgrade from 4.*

If you're upgrading from the 4.*, the one for Laravel 5.3.*, you need to refactor your usage of the __ method., (*46)

Laravel now use this method for their own translation. You now need to use _i instead and add this keyword in the configuration file of Laravel-Gettext:, (*47)

Also, if you're using Symfony as your backend, you can add the _s method. It's made to use the full feature set of Symfony plurals syntax., (*48)

  /**
     * The keywords list used by poedit to search the strings to be translated
     *
     * The "_", "__" and "gettext" are singular translation functions
     * The "_n" and "ngettext" are plural translation functions
     * The "dgettext" function allows a translation domain to be explicitly specified
     *
     * "__" and "_n" and "_i" and "_s" are helpers functions @see \Xinax\LaravelGettext\Support\helpers.php
     */
    'keywords-list' => ['_', '__', '_i', '_s', 'gettext', '_n:1,2', 'ngettext:1,2', 'dgettext:2'],;

The Versions

01/03 2018

dev-master

9999999-dev https://github.com/Belphemur/laravel-gettext

Adds localization support to laravel applications in an easy way using Poedit and GNU gettext.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel translation localization gettext poedit laravel-gettext

01/03 2018

7.1.0

7.1.0.0 https://github.com/Belphemur/laravel-gettext

Adds localization support to laravel applications in an easy way using Poedit and GNU gettext.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel translation localization gettext poedit laravel-gettext

23/02 2018

7.0.0

7.0.0.0 https://github.com/Belphemur/laravel-gettext

Adds localization support to laravel applications in an easy way using Poedit and GNU gettext.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel translation localization gettext poedit laravel-gettext

23/02 2018

6.1.0

6.1.0.0 https://github.com/Belphemur/laravel-gettext

Adds localization support to laravel applications in an easy way using Poedit and GNU gettext.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel translation localization gettext poedit laravel-gettext

23/02 2018

dev-optimization

dev-optimization https://github.com/xinax/laravel-gettext

Adds localization support to laravel applications in an easy way using Poedit and GNU gettext.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel translation localization gettext poedit laravel-gettext

05/09 2017

dev-laravel-5.5

dev-laravel-5.5 https://github.com/Belphemur/laravel-gettext

Adds localization support to laravel applications in an easy way using Poedit and GNU gettext.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel translation localization gettext poedit laravel-gettext

05/09 2017

6.0.0

6.0.0.0 https://github.com/Belphemur/laravel-gettext

Adds localization support to laravel applications in an easy way using Poedit and GNU gettext.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel translation localization gettext poedit laravel-gettext

05/04 2017

5.0.2

5.0.2.0 https://github.com/Belphemur/laravel-gettext

Adds localization support to laravel applications in an easy way using Poedit and GNU gettext.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel translation localization gettext poedit laravel-gettext

01/03 2017

5.0.1

5.0.1.0 https://github.com/Belphemur/laravel-gettext

Adds localization support to laravel applications in an easy way using Poedit and GNU gettext.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel translation localization gettext poedit laravel-gettext

20/02 2017

5.0.0

5.0.0.0 https://github.com/Belphemur/laravel-gettext

Adds localization support to laravel applications in an easy way using Poedit and GNU gettext.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel translation localization gettext poedit laravel-gettext

14/11 2016

4.0.4

4.0.4.0 https://github.com/xinax/laravel-gettext

Adds localization support to laravel applications in an easy way using Poedit and GNU gettext.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel translation localization gettext poedit laravel-gettext

14/11 2016

4.x-dev

4.9999999.9999999.9999999-dev https://github.com/xinax/laravel-gettext

Adds localization support to laravel applications in an easy way using Poedit and GNU gettext.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel translation localization gettext poedit laravel-gettext

14/11 2016

4.0.3

4.0.3.0 https://github.com/xinax/laravel-gettext

Adds localization support to laravel applications in an easy way using Poedit and GNU gettext.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel translation localization gettext poedit laravel-gettext

17/10 2016

4.0.2

4.0.2.0 https://github.com/xinax/laravel-gettext

Adds localization support to laravel applications in an easy way using Poedit and GNU gettext.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel translation localization gettext poedit laravel-gettext

17/10 2016

4.0.1

4.0.1.0 https://github.com/xinax/laravel-gettext

Adds localization support to laravel applications in an easy way using Poedit and GNU gettext.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel translation localization gettext poedit laravel-gettext

17/10 2016

4.0.0

4.0.0.0 https://github.com/xinax/laravel-gettext

Adds localization support to laravel applications in an easy way using PoEdit and GNU-Gettext.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel translation localization gettext poedit laravel-gettext

10/03 2016

3.1.1

3.1.1.0 https://github.com/xinax/laravel-gettext

Adds localization support to laravel applications in an easy way using PoEdit and GNU-Gettext.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel translation localization gettext poedit laravel-gettext

10/03 2016

2.x-dev

2.9999999.9999999.9999999-dev https://github.com/xinax/laravel-gettext

Adds localization support to laravel applications in an easy way using PoEdit and GNU-Gettext.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel translation localization gettext poedit laravel-gettext

09/01 2016

3.1.0

3.1.0.0 https://github.com/xinax/laravel-gettext

Adds localization support to laravel applications in an easy way using PoEdit and GNU-Gettext.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel translation localization gettext poedit laravel-gettext

08/08 2015

3.0.3

3.0.3.0 https://github.com/xinax/laravel-gettext

Adds localization support to laravel applications in an easy way using PoEdit and GNU-Gettext.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel translation localization gettext poedit laravel-gettext

08/08 2015

3.x-dev

3.9999999.9999999.9999999-dev https://github.com/xinax/laravel-gettext

Adds localization support to laravel applications in an easy way using PoEdit and GNU-Gettext.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel translation localization gettext poedit laravel-gettext

28/07 2015

3.0.2

3.0.2.0 https://github.com/xinax/laravel-gettext

Adds localization support to laravel applications in an easy way using PoEdit and GNU-Gettext.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel translation localization gettext poedit laravel-gettext

17/07 2015

3.0.1

3.0.1.0 https://github.com/xinax/laravel-gettext

Adds localization support to laravel applications in an easy way using PoEdit and GNU-Gettext.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel translation localization gettext poedit laravel-gettext

17/07 2015

3.0.0

3.0.0.0 https://github.com/xinax/laravel-gettext

Adds localization support to laravel applications in an easy way using PoEdit and GNU-Gettext.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel translation localization gettext poedit laravel-gettext

30/04 2015

2.0.3

2.0.3.0 https://github.com/xinax/laravel-gettext

Adds localization support to laravel applications in an easy way using PoEdit and GNU-Gettext.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel translation localization gettext poedit laravel-gettext

23/02 2015

2.0.2

2.0.2.0 https://github.com/xinax/laravel-gettext

Adds localization support to laravel applications in an easy way using PoEdit and GNU-Gettext.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel translation localization gettext poedit laravel-gettext

23/02 2015

2.0.1

2.0.1.0 https://github.com/xinax/laravel-gettext

Adds localization support to laravel applications in an easy way using PoEdit and GNU-Gettext.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel translation localization gettext poedit laravel-gettext

17/02 2015

2.0.0

2.0.0.0 https://github.com/xinax/laravel-gettext

Adds localization support to laravel applications in an easy way using PoEdit and GNU-Gettext.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel translation localization gettext poedit laravel-gettext

13/11 2014

1.x-dev

1.9999999.9999999.9999999-dev https://github.com/xinax/laravel-gettext

Adds localization support to laravel applications in an easy way using PoEdit and GNU-Gettext.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel translation localization gettext poedit laravel-gettext

13/11 2014

1.0.3

1.0.3.0 https://github.com/xinax/laravel-gettext

Adds localization support to laravel applications in an easy way using PoEdit and GNU-Gettext.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel translation localization gettext poedit laravel-gettext

14/10 2014

1.0.2

1.0.2.0 https://github.com/xinax/laravel-gettext

Adds localization support to laravel applications in an easy way using PoEdit and GNU-Gettext.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel translation localization gettext poedit laravel-gettext

09/10 2014

1.0.1

1.0.1.0 https://github.com/xinax/laravel-gettext

Adds localization support to laravel applications in an easy way using PoEdit and GNU-Gettext.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel translation localization gettext poedit laravel-gettext