Giter Club home page Giter Club logo

laravel-gettext's Introduction

laravel-gettext

Build Status

Installation

Laravel 5.2 Installation

Add the composer repository to your composer.json file:

"require": {
    "clusteramaryllis/gettext": "1.3.x"
}

And run composer update. Once finished, register via service provider in config/app.php in the providers array:

'providers' => [

    // ...

    Clusteramaryllis\Gettext\GettextServiceProvider::class,

]

You can also provide static syntax via facade in the aliases array:

'aliases' => [

    // ...

    'Gettext' => Clusteramaryllis\Gettext\Facades\Gettext::class,

]

Publish the configuration file (optional) (will create on config/gettext.php) :

php artisan vendor:publish

Command

Available commands

gettext:create => Generate new .po file

gettext:update => Update existing .po file

Available options

Check with php artisan gettext:create --help or php artisan gettext:update --help

Example

php artisan gettext:create --sources="app/Http/Controllers, resources/views" --destination="resources/locale" --locale="en_US" 

This will generate .po files in resources/locale/en_US/LC_MESSAGES/messages.po & will scan any string that utilize php-gettext function on app/Http/Controllers & resources/views

Once done, you can easily translate your application using tools such as PoEdit.

How To

Simple usage

  1. Prepare view with strings wrapped with Gettext method or helper
<!-- resources\views\welcome.blade.php -->
{!! __('Welcome to main page') !!}
  1. Add your language preferences via config/gettext.php on languages array
languages => [

    // ...,

    'sv' => [
        'locale' => 'sv_SE',
        'encoding' => 'utf-8',
        'plural_forms' => "nplurals=2; plural=(n != 1);",
    ]      
]
  1. Run php artisan gettext:create. This will generate .po file in
resources\locale\sv_SE\LC_MESSAGES\messages.po

& ready to scan translated string in app\Http\Controllers & resources\views (Default option).

  1. Open the .po file with PoEdit or any similar editors. In PoEdit you need to click update to populate the table with the scanned strings. After that, you can start begin translating.

  2. Simple routes test

Route::get('/', function() {

    Gettext::bindTextDomain('messages', base_path('resources/locale'));
    Gettext::textDomain('messages');

    Gettext::setLocale(LC_ALL, 'sv_SE.utf-8');

    return view('welcome');
});

Available methods

Methods Helper shortcut
Gettext::setLocale _setlocale
Gettext::bindTextDomain _bindtextdomain
Gettext::bindTextDomainCodeset _bind_text_domain_codeset
Gettext::textDomain _textdomain
Gettext::getText __
Gettext::nGetText _n
Gettext::dGetText _d
Gettext::dNGetText _dn
Gettext::dCGetText _dc
Gettext::dCNGetText _dcn
Gettext::pGetText _p
Gettext::dPGetText _dp
Gettext::dCPGetText _dcp
Gettext::nPGetText _np
Gettext::dNPGetText _dnp
Gettext::dCNPGetText _dcnp

More detailed method & their parameters can be seen here.

Acknowledgements

This package is inspired by laravel-gettext by Nicolás Daniel Palumbo for .po files creation & utilize php-gettext package by Danilo Segan.

laravel-gettext's People

Contributors

clusteramaryllis avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

laravel-gettext's Issues

All API throws

ErrorException in GettextDriver.php line 264: setlocale(): Specified locale name is too long

in GettextDriver.php line 264
at HandleExceptions->handleError('2', 'setlocale(): Specified locale name is too long', '/srv/http/vhosts/[redacted]/website/vendor/clusteramaryllis/gettext/src/Driver/GettextDriver.php', '264', array('category' => '6', 'argsCount' => '2', 'strCategory' => 'LC_ALL', 'preLocales' => array('LC_CTYPE=pl_PL.UTF-8;LC_NUMERIC=pl_PL.UTF-8;LC_TIME=pl_PL.UTF-8;LC_COLLATE=C;LC_MONETARY=pl_PL.UTF-8;LC_MESSAGES=pl_PL.UTF-8;LC_PAPER=pl_PL.UTF-8;LC_NAME=pl_PL.UTF-8;LC_ADDRESS=pl_PL.UTF-8;LC_TELEPHONE=pl_PL.UTF-8;LC_MEASUREMENT=pl_PL.UTF-8;LC_IDENTIFICATION=pl_PL.UTF-8'), 'locales' => array('LC_CTYPE=pl_PL.UTF-8;LC_NUMERIC=pl_PL.UTF-8;LC_TIME=pl_PL.UTF-8;LC_COLLATE=C;LC_MONETARY=pl_PL.UTF-8;LC_MESSAGES=pl_PL.UTF-8;LC_PAPER=pl_PL.UTF-8;LC_NAME=pl_PL.UTF-8;LC_ADDRESS=pl_PL.UTF-8;LC_TELEPHONE=pl_PL.UTF-8;LC_MEASUREMENT=pl_PL.UTF-8;LC_IDENTIFICATION=pl_PL.UTF-8')))
at setlocale('6', array('LC_CTYPE=pl_PL.UTF-8;LC_NUMERIC=pl_PL.UTF-8;LC_TIME=pl_PL.UTF-8;LC_COLLATE=C;LC_MONETARY=pl_PL.UTF-8;LC_MESSAGES=pl_PL.UTF-8;LC_PAPER=pl_PL.UTF-8;LC_NAME=pl_PL.UTF-8;LC_ADDRESS=pl_PL.UTF-8;LC_TELEPHONE=pl_PL.UTF-8;LC_MEASUREMENT=pl_PL.UTF-8;LC_IDENTIFICATION=pl_PL.UTF-8')) in GettextDriver.php line 264
at GettextDriver->setLocale('6', 'LC_CTYPE=pl_PL.UTF-8;LC_NUMERIC=pl_PL.UTF-8;LC_TIME=pl_PL.UTF-8;LC_COLLATE=C;LC_MONETARY=pl_PL.UTF-8;LC_MESSAGES=pl_PL.UTF-8;LC_PAPER=pl_PL.UTF-8;LC_NAME=pl_PL.UTF-8;LC_ADDRESS=pl_PL.UTF-8;LC_TELEPHONE=pl_PL.UTF-8;LC_MEASUREMENT=pl_PL.UTF-8;LC_IDENTIFICATION=pl_PL.UTF-8')
at call_user_func_array(array(object(GettextDriver), 'setLocale'), array('6', 'LC_CTYPE=pl_PL.UTF-8;LC_NUMERIC=pl_PL.UTF-8;LC_TIME=pl_PL.UTF-8;LC_COLLATE=C;LC_MONETARY=pl_PL.UTF-8;LC_MESSAGES=pl_PL.UTF-8;LC_PAPER=pl_PL.UTF-8;LC_NAME=pl_PL.UTF-8;LC_ADDRESS=pl_PL.UTF-8;LC_TELEPHONE=pl_PL.UTF-8;LC_MEASUREMENT=pl_PL.UTF-8;LC_IDENTIFICATION=pl_PL.UTF-8')) in Gettext.php line 47
at Gettext->setLocale('6', 'LC_CTYPE=pl_PL.UTF-8;LC_NUMERIC=pl_PL.UTF-8;LC_TIME=pl_PL.UTF-8;LC_COLLATE=C;LC_MONETARY=pl_PL.UTF-8;LC_MESSAGES=pl_PL.UTF-8;LC_PAPER=pl_PL.UTF-8;LC_NAME=pl_PL.UTF-8;LC_ADDRESS=pl_PL.UTF-8;LC_TELEPHONE=pl_PL.UTF-8;LC_MEASUREMENT=pl_PL.UTF-8;LC_IDENTIFICATION=pl_PL.UTF-8') in Gettext.php line 26
at Gettext->__construct(object(GettextDriver)) in GettextServiceProvider.php line 104
at GettextServiceProvider->Clusteramaryllis\Gettext\{closure}(object(Application), array()) in Container.php line 734
at Container->build(object(Closure), array()) in Container.php line 627
at Container->make('gettext', array()) in Application.php line 674
at Application->make('gettext') in Container.php line 1159
at Container->offsetGet('gettext') in Facade.php line 149
at Facade::resolveFacadeInstance('gettext') in Facade.php line 118
at Facade::getFacadeRoot() in Facade.php line 203
at Facade::__callStatic('bindTextDomain', array('messages', '/srv/http/vhosts/[redacted]/website/resources/locale')) in Front.php line 20
at Gettext::bindTextDomain('messages', '/srv/http/vhosts/[redacted]/website/resources/locale') in Front.php line 20
at Front->index()

This happens during the second invocation of GettextDriver::setLocale() that is executed sometime during whatever is done when trying to call any API through the facade. On the first invocation of the setLocale() method the result of native setlocale() is stored in $this->currentLocale, and on the second invocation it is passed back to setLocale(). The long string you see in the backtrace is what native setlocale() returns the first time. I don't think this is what you expect.

As you can see near the bottom of the stack dump, all this happens during on-demand construction of the Gettext instance as a result of using the facade for the first time. The specific API attempted (bindTextDomain) doesn't even involve setting the locale.

messages.po with no messages

I'm on Laravel 5.1, Windows, and I tested with both version 1.1.1 and the just added 2.0.0 with no luck.

Running php artisan gettext:create, generates all the messages.po files inside resources/locale/[lang]/LC_MESSAGES/messages.po but it only contains the header.

I have blade files under resources/views with {{ _('Welcome to application') }} and also calling _ function for some messages in controllers.

I also tried to manually put a message on messages.po but it doesn't get translated.

Am I missing anything?

I don't see that the syntax given in readme actually works in templates?

The readme says to use the following syntax in templates:

{!! __('Translated string') !!}

These strings are not detected when I update the catalogs from sources in Poedit.

A different syntax seems to work for string extraction:

{{ _('Translated string') }}

At the bottom of readme there is a table with gettext function aliases (including '__'), but the default configuration in gettext.php after vendor:publish doesn't seem to configure these as keywords.

How do I change languages?

Hello!

I'm sorry for probably being stupid, but how do I change the language?

I have set the language to 'sv' in app.php, but it's still in English.
Trying to do a set_locale results in undefined function.

Thank you in advance!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.