• This repository has been archived on 06/Jul/2019
  • Stars
    star
    131
  • Rank 275,867 (Top 6 %)
  • Language
    PHP
  • Created almost 12 years ago
  • Updated over 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Laravel localization and translation helper

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 your routes, models and views.

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

Views localization

Views localization work by setting up gettext for you and providing two commands to extract translations from your views to PO files and compile those to MO files.

This is currently only possible for Twig but will soon for Blade and classic PHP files, and will require the twig/extensions package which adds gettext support for Twig.

To use simply configure your domain in the configuration, then run php artisan lang:extract.

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

More Repositories

1

underscore-php

A redacted PHP port of Underscore.js with additional functions and goodies – Available for Composer and Laravel
PHP
1,121
star
2

flatten

A package to flatten any website to plain HTML
PHP
336
star
3

html-object

A set of classes to create and manipulate HTML objects abstractions
PHP
132
star
4

flickering

A modern PHP interface for the Flickr API
PHP
50
star
5

cookie-monster

A rehosted, cleaned up and updated version of the CookieMonster plugin.
JavaScript
31
star
6

fakable

Allows the creation and seeding of fake Eloquent models
PHP
24
star
7

arrounded

A set of self-aware Laravel abstracts that know their way around
PHP
23
star
8

janitor

Dust off your Laravel applications
PHP
21
star
9

illuminage

Wrapper for the Imagine library that manages caching of images
PHP
18
star
10

registry

The Laravel Packages Registry
PHP
15
star
11

dotfiles

Personnal dotfiles
Ruby
9
star
12

seonnet

SEO handler for Laravel 4, with customization of slugs, meta data and more
PHP
8
star
13

phpstorm-twig-live-templates

Twig Live Templates for PHPStorm
7
star
14

versioner

Small CLI utility to easily create new versions of Composer packages
PHP
4
star
15

isaac-mod-manager

IMM is a cross-plateform CLI-tool to manage your Workshop mods for Binding of Isaac.
PHP
2
star
16

psr-container-helpers

PHP
2
star
17

bs-moment-duration-format

This package allows you to use moment-duration-format with bs-moment
C++
2
star
18

autopergamene

Personnal portfolio
PHP
2
star
19

sadness-deployer

PHP
1
star
20

php-configuration

PHP configuration reference dumper/loader for symfony/config
PHP
1
star
21

slides-agnostic

JavaScript
1
star
22

furnace

An app to rate and curate Rocksmith CDLCs
PHP
1
star