Giter Club home page Giter Club logo

laravel-gettext's Introduction

laravel-gettext

Build Status

Installation

Laravel 5.1 Installation

Add the composer repository to your composer.json file:

"require": {
    "clusteramaryllis/gettext": "1.2.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.

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.