Coder Social home page Coder Social logo

polyglot's Introduction

Polyglot

Build Status Latest Stable Version Total Downloads Scrutinizer Quality Score Code Coverage

Introduction

Polyglot is a localization helper for the Laravel framework, it's an helper class to localize both your routes and your models.

To install it, do composer require anahkiasen/polyglot:dev-master, then add Polyglot\PolyglotServiceProvider to the providers array in app/config/app.php.

Publish config to your laravel app : php artisan config:publish anahkiasen/polyglot

Model localization

Setting a model as polyglot will allow you to make fields speak several languages. Polyglot requires you to separate common fields from localized ones assuming the following common pattern :

Take the example of a blog article model

TABLE articles
  id INT
  category_id INT
  created_at DATETIME
  updated_at DATETIME

TABLE article_lang
  id INT
  title VARCHAR
  content TEXT
  article_id INT
  lang ENUM

From there you can either access any language easily by doing the following : $article->fr->title. Or you can add the following parameter to your model and let Polyglot automatically translate attributes.

class Article extends Polyglot
{
  protected $polyglot = ['title', 'content'];
}

// Get an automatically localized Article
$article = Article::find(4)

echo $article->fr->title // This will print out the french title
echo $article->title // This will print out the title in the current language

Polyglot also helps you saving localized attributes :

$article->fill([
  'title'   => 'Titre',
  'content' => 'Contenu',
  'lang'    => 'fr',
])->save();

// Is the same as

$article->fr->fill([
  'title'   => 'Titre',
  'content' => 'Contenu',
])->save();

Globally speaking when Polyglot sees you're trying to save localized attribute on the parent model, it will automatically fetch the Lang model and save them on it instead. If no lang attribute is passed, Polyglot will use the current language.

Note that, as your attributes are now split into two tables, you can Polyglot eager load the correct Lang relation with the withLang method. Per example Article::withLang()->get() will return Articles with fr autoloaded if it's the current language, or en, according to app.locale.

Routes localization

To localize your routes, you need to set the locales option in your config file, per example array('fr', 'en'). Now you may define your routes as such :

Route::groupLocale(['before' => 'auth'], function() {
  Route::get('/', 'HomeController@index');
  Route::get('articles', 'ArticlesController@index');
  // etc...
});

Now you can access /fr and /fr/articles, or /en and /en/articles โ€“ Polyglot will recognize the locale in the URL and automatically set your app in that language. There is also a default option in the config file, setting that option to a locale like 'default' => 'fr' will make the root URLs point to that locale. So accessing /articles without prefixing it with a locale would render the page in french.

Locales helpers

Polyglot also provide various locale helpers hooked into the Lang and URL class you know and love :

URL::locale() // Returns the locale in the current URL

Lang::active('fr') // Check if fr is the current locale
Lang::setInternalLocale('fr') // Set both the locale with the Translator class and setlocale method
Lang::valid('fr') // Check if a locale is valid
Lang::sanitize('fr') // Returns the locale if valid, or the default locale if not

polyglot's People

Contributors

anahkiasen avatar mathieudoyon avatar gontard avatar abbasadel avatar

Watchers

 avatar James Cloos avatar

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.