PHPackages                             devuniverse/laravel-gettext - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Localization &amp; i18n](/categories/localization)
4. /
5. devuniverse/laravel-gettext

ActiveLibrary[Localization &amp; i18n](/categories/localization)

devuniverse/laravel-gettext
===========================

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

v1.0.1(1y ago)05.2k—3.2%MITPHPPHP &gt;=8.0

Since Mar 19Pushed 1y ago1 watchersCompare

[ Source](https://github.com/devuniverse/laravel-gettext)[ Packagist](https://packagist.org/packages/devuniverse/laravel-gettext)[ Docs](https://github.com/devuniverse/laravel-gettext)[ RSS](/packages/devuniverse-laravel-gettext/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)Dependencies (7)Versions (4)Used By (0)

Laravel Gettext - Laravel 11+
=============================

[](#laravel-gettext---laravel-11)

*Laravel Gettext* is a package compatible with the great Laravel PHP 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.

[![Stable build Status](https://camo.githubusercontent.com/8ffbb0aa33ce7b65caf719b85b252669aaf403d855df148769132ed1a7638d1b/68747470733a2f2f7472617669732d63692e6f72672f7a65726f7370616d2f6c61726176656c2d676574746578742e706e673f6272616e63683d372e322e30)](https://travis-ci.org/zerospam/laravel-gettext) [Latest Laravel 6 stable release (7.2.0)](https://github.com/zerospam/laravel-gettext/tree/7.2.0)

> Note: This documentation applies to laravel 5.5.x and up. For older versions of laravel check the following links:

### Older versions

[](#older-versions)

[![Stable build Status](https://camo.githubusercontent.com/04508ecf51cbd7ddf992c423a516e6ef5cb47ae73368757e805e2eb3150833df/68747470733a2f2f7472617669732d63692e6f72672f7a65726f7370616d2f6c61726176656c2d676574746578742e706e673f6272616e63683d372e312e30)](https://travis-ci.org/zerospam/laravel-gettext) [Latest Laravel 5.6.x &amp; 5.7.x stable release (7.1.0)](https://github.com/zerospam/laravel-gettext/tree/7.0.0)

[![Stable build Status](https://camo.githubusercontent.com/19d227f618a9f9043c80d6825689f9100265c8ade2ff7489d8d0b43530302517/68747470733a2f2f7472617669732d63692e6f72672f7a65726f7370616d2f6c61726176656c2d676574746578742e706e673f6272616e63683d362e312e30)](https://travis-ci.org/zerospam/laravel-gettext) [Latest Laravel 5.5.x stable release (6.1.0)](https://github.com/zerospam/laravel-gettext/tree/6.1.0)

[![Stable build Status](https://camo.githubusercontent.com/e3dba27edc75f27a68992182fef52363c1f950fe87f1d210e38ddd0f1510b624/68747470733a2f2f7472617669732d63692e6f72672f7a65726f7370616d2f6c61726176656c2d676574746578742e706e673f6272616e63683d352e302e32)](https://travis-ci.org/zerospam/laravel-gettext) [Latest Laravel 5.4.x stable release (5.0.2)](https://github.com/zerospam/laravel-gettext/tree/5.0.2)

[![Stable build Status](https://camo.githubusercontent.com/1b971d782d130996ba544f07de19b8d6a030ab565a4348f2c4e6e9401b111794/68747470733a2f2f7472617669732d63692e6f72672f7a65726f7370616d2f6c61726176656c2d676574746578742e706e673f6272616e63683d342e302e34)](https://travis-ci.org/zerospam/laravel-gettext) [Latest Laravel 5.3.x stable release (4.0.4)](https://github.com/zerospam/laravel-gettext/tree/4.0.4)

[![Stable build Status](https://camo.githubusercontent.com/847493625128333023b4698b1b0fa7816768c8788fb62656a91a7b48e826e3c5/68747470733a2f2f7472617669732d63692e6f72672f7a65726f7370616d2f6c61726176656c2d676574746578742e706e673f6272616e63683d332e312e30)](https://travis-ci.org/zerospam/laravel-gettext) [Latest Laravel 5.2.x stable release (3.1.0)](https://github.com/zerospam/laravel-gettext/tree/3.1.0)

[![Stable build Status](https://camo.githubusercontent.com/58f7c49f632cfed4c1e580fc3427ef27946331fdb98a2e845fb70d68b55ae18f/68747470733a2f2f7472617669732d63692e6f72672f7a65726f7370616d2f6c61726176656c2d676574746578742e706e673f6272616e63683d332e302e33)](https://travis-ci.org/zerospam/laravel-gettext) [Latest Laravel 5.1.x stable release (3.0.3)](https://github.com/zerospam/laravel-gettext/tree/3.0.3)

[![Stable build Status](https://camo.githubusercontent.com/43a832444713dc7ed8b273379b8d25b27d2c48e06e377b3a5d157b4ed546e7c7/68747470733a2f2f7472617669732d63692e6f72672f7a65726f7370616d2f6c61726176656c2d676574746578742e706e673f6272616e63683d322e302e33)](https://travis-ci.org/zerospam/laravel-gettext) [Latest Laravel 5.0 stable release (2.0.3)](https://github.com/zerospam/laravel-gettext/tree/2.0.3)

[![Stable build Status](https://camo.githubusercontent.com/5fc8ac1dcb063fdc77d5ff802ed6b2b125ed7316c871a4d8904b177f08a5a08b/68747470733a2f2f7472617669732d63692e6f72672f7a65726f7370616d2f6c61726176656c2d676574746578742e706e673f6272616e63683d312e302e33)](https://travis-ci.org/zerospam/laravel-gettext) [Latest Laravel 4.x stable release (1.0.3)](https://github.com/zerospam/laravel-gettext/tree/1.0.3)

[![Dev build Status](https://camo.githubusercontent.com/e269771af5ddbda8c19fa13820cdc9619532cb4634e7716e9058ae367e8b7a37/68747470733a2f2f7472617669732d63692e6f72672f7a65726f7370616d2f6c61726176656c2d676574746578742e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/zerospam/laravel-gettext) [Development master](https://github.com/zerospam/laravel-gettext/tree/master) Unstable, only for development (dev-master)

### 1. Requirements

[](#1-requirements)

- Composer -
- Laravel 5.5.\* -
- Poedit -

#### 1.1 Optional

[](#11-optional)

##### 1.1.1 APCU

[](#111-apcu)

APCU extension installed -

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.

The cache is automatically invalidated when there is a change in the translation file.

##### 1.1.2 gettext

[](#112-gettext)

Optional requirements if you want to use the native php-gettext extension:

- php-gettext -
- GNU gettext on system (and production server!) -

> You will need to update the 'handler' option to 'gettext' in order to use the native php-gettext module.

### 2. Install

[](#2-install)

Add the composer repository to your *composer.json* file:

```
    composer require devuniverse/laravel-gettext
```

And run composer update. Once it's installed, laravel will discover automatically the provider and load it. (Only for 5.5)

Now you need to publish the configuration file in order to set your own application values:

```
    php artisan vendor:publish
```

This command creates the package configuration file in: `config/laravel-gettext.php`.

You also need to register the LaravelGettext middleware in the `app/Http/Kernel.php` file:

```
    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.

### 3. Configuration

[](#3-configuration)

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

```
    /**
     * 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.

> Make sure you have write permissions on `resources/` before you run this command

```
    php artisan gettext:create
```

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

### 4. Workflow

[](#4-workflow)

##### A. Write strings :D

[](#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:

```
    // 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

##### B. Plural strings

[](#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](http://php.net/manual/en/function.ngettext.php). Let's look a simple view example:

```
    // 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:

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

See [Plural forms](http://docs.translatehouse.org/projects/localization-guide/en/latest/l10n/pluralforms.html?id=l10n/pluralforms) used by Poedit to configure for your language.

###### With Symfony

[](#with-symfony)

If you're using [Symfony](http://symfony.com/doc/current/translation.html) 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.

```
    // 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:

```
    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

[](#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.

[![](https://camo.githubusercontent.com/029feeb5f3859935205c5c3441638b176fac45d888cc5cffcadb66d002cd9530/68747470733a2f2f7261772e6769746875622e636f6d2f7a65726f7370616d2f6c61726176656c2d676574746578742f6d61737465722f646f632f706f656469742e706e67)](https://camo.githubusercontent.com/029feeb5f3859935205c5c3441638b176fac45d888cc5cffcadb66d002cd9530/68747470733a2f2f7261772e6769746875622e636f6d2f7a65726f7370616d2f6c61726176656c2d676574746578742f6d61737465722f646f632f706f656469742e706e67)

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.

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.

##### C. Runtime methods

[](#c-runtime-methods)

To change configuration on runtime you have these methods:

```
    /**
     * 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:

[](#5-features-and-examples)

#### A. Route and controller implementation example:

[](#a-route-and-controller-implementation-example)

app/Http/routes.php

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

app/Http/Controllers/HomeController.php

```
    /**
     * 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:

[](#b-a-basic-language-selector-example)

```

      @foreach(Config::get('laravel-gettext.supported-locales') as $locale)
            {{$locale}}
      @endforeach

```

#### C. Built-in language selector:

[](#c-built-in-language-selector)

You can use the built-in language selector in your views:

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

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

It also supports custom labels:

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

#### D. Adding source directories and domains

[](#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.

```
    /**
     * 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.

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.

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

```
    '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**

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

```
    LaravelGettext::setDomain("backend");
```

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

```
    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.

You can update only the files of a single domain with the same command:

```
    php artisan gettext:update --domain backend
```

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

[](#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.

### 6. Contributing

[](#6-contributing)

If you want to help with the development of this package, you can:

- 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.\*

[](#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.

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:

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.

```
  /**
     * 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'],;
```

###  Health Score

33

—

LowBetter than 74% of packages

Maintenance42

Moderate activity, may be stable

Popularity21

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 60% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~274 days

Total

2

Last Release

507d ago

PHP version history (2 changes)v1.0.0PHP &gt;=8.2

v1.0.1PHP &gt;=8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/e649a7023045f5f55de905991070b6d041cfa56fb44eacc80c58ac97d3e7c0b1?d=identicon)[devuniverse](/maintainers/devuniverse)

---

Top Contributors

[![hawkverse](https://avatars.githubusercontent.com/u/113536456?v=4)](https://github.com/hawkverse "hawkverse (6 commits)")[![devuniverse](https://avatars.githubusercontent.com/u/45764367?v=4)](https://github.com/devuniverse "devuniverse (4 commits)")

---

Tags

laravellocalizationtranslationgettextpoEditlaravel-gettext

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/devuniverse-laravel-gettext/health.svg)

```
[![Health](https://phpackages.com/badges/devuniverse-laravel-gettext/health.svg)](https://phpackages.com/packages/devuniverse-laravel-gettext)
```

###  Alternatives

[zerospam/laravel-gettext

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

102207.4k](/packages/zerospam-laravel-gettext)[tio/laravel

Add this package to localize your Laravel application (PHP, JSON or GetText).

170318.5k](/packages/tio-laravel)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
