2017 © Pedro Peláez
 

package language-detector

Detect the language for your application using browser preferences, subdomains or route prefixes.

image

vluzrmos/language-detector

Detect the language for your application using browser preferences, subdomains or route prefixes.

  • Saturday, March 11, 2017
  • by vluzrmos
  • Repository
  • 11 Watchers
  • 75 Stars
  • 21,054 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 16 Forks
  • 6 Open issues
  • 31 Versions
  • 18 % Grown

The README.md

Laravel Language Detector

Join the chat at https://gitter.im/vluzrmos/laravel-language-detector, (*1)

Latest Stable Version Total Downloads License, (*2)

Build Status Scrutinizer Code Quality Code Climate Codacy Badge StyleCI, (*3)

This package provides an easy way to detect and apply the language for your application using browser preferences, subdomains or route prefixes., (*4)

Installation

Require the package using composer:, (*5)

composer require vluzrmos/language-detector, (*6)

Add the service provider as follows:, (*7)

Laravel

Edit your config/app.php:, (*8)

Insert this line of code above the listed RouteServiceProvider, ex:, (*9)

Vluzrmos\LanguageDetector\Providers\LanguageDetectorServiceProvider::class,
App\Providers\RouteServiceProvider::class, 

::class notation is optional., (*10)

Lumen

Edit the bootstrap/app.php:, (*11)

$app->register(Vluzrmos\LanguageDetector\Providers\LanguageDetectorServiceProvider::class);

::class notation is optional., (*12)

Configuration

Two options for Laravel, either publish the package configuration using:, (*13)

php artisan vendor:publish --provider="Vluzrmos\LanguageDetector\Providers\LanguageDetectorServiceProvider"

then edit the new config/lang-detector.php file or add the following lines to your .env file:, (*14)

#Indicates whether the language should be autodetected (can be removed)
LANG_DETECTOR_AUTODETECT=true

#Driver to use, the default is browser 
LANG_DETECTOR_DRIVER="browser"

#Segment to use in the uri or subdomain driver, default 0 (can be removed) 
LANG_DETECTOR_SEGMENT=0

#Name of cookie to cache the detected language (use false|null to disable) 
LANG_DETECTOR_COOKIE=locale

#Comma-separated list of languages provided on the app 
LANG_DETECTOR_LANGUAGES="en,fr,pt_BR"

#To alias the language use the notation ":", "=", ":=" or  "=>" to separate the alias and its value.
# LANG_DETECTOR_LANGUAGES="en, en-us:en, pt-br:pt_BR"

If you not want to use that, just publish the configurations of the package with php artisan vendor:publish and edit on config/lang-detector.php generated., (*15)

For Lumen, consider to copy vendor/vluzrmos/language-detector/config/lang-detector.php to your configs dir and use $app->configure('lang-detector') before register the LanguageDetectorServiceProvider., (*16)

Detector Drivers

There are a few drivers that you might to use, choose one which matches with your application design:, (*17)

Browser Preferences

The driver browser will try to detect the language of the application based on the request languages (browser preferences). This driver doesn't need any other configuration, just configure the available languages., (*18)

Subdomains

The driver subdomain will try to detect the language of the application which matches with subdomain of the hostname. eg.:, (*19)

http://fr.site.domain

The subdomain driver will detect language fr and set the application to fr if it is one of the available languages in the lang-detector config file., (*20)

Note: subdomain and uri drivers require aliases of the the language-locales in the lang-detector config file., (*21)

Route Prefixes

The driver uri will try to detect the language based on the route prefix:, (*22)

http://site.domain/en-us/home

That driver will detect en-us and set it to the application. (Note: Consider to aliase that locale), (*23)

And don't worry, if the url is like:, (*24)

http://site.domain/home

The language will not be changed, the application will use your default language configured on your config/app.php file., (*25)

With uri driver, your route group needs be like this:, (*26)

//That would be nice if you put (edit) it on your App\Providers\RouteServiceProvider.

// Laravel
Route::group(['prefix' => app('language.routePrefix'), 'namespace' => 'App\Http\Controllers'], function ($router) {
    require __DIR__.'/../Http/routes.php';
});

//For lumen, it should be on bootstrap/app.php.

// Lumen
$app->group(['prefix' => app('language.routePrefix'), 'namespace' => 'App\Http\Controllers'], function ($app) {
   require __DIR__.'/../app/Http/routes.php';
}

Issue: Lumen 5.0 doesn't support route prefix with empty strings, you should use that script:, (*27)

$prefix = app('language.routePrefix');

$options = [];

if (!empty($prefix) && $prefix!="/") {
    $options['prefix'] = $prefix;
}

// any other options here
$options['namespace'] = 'App\Http\Controllers';

$app->group($options, function () use($app) {
    // ...
});

Note: That is only for Lumen 5.0, the newest version (5.1) already fixes it., (*28)

Aliasing language locales

You might to use the style lang_LOCALE or just lang on your resources/lang dir. The language detector driver you have chosen will try to detect the language which matches with lang or lang_LOCALE available on your config/lang-detector.php., (*29)

'languages' => ['en', 'pt_BR' ...]

example:, (*30)

├── lang
│   ├── en
│   │   ├── messages.php
│   │   └── validation.php
│   └── pt_BR
│       ├── messages.php
│       └── validation.php

If you are not following that style of languages names or if you are using the subdomain or uri drivers, just configure it on config/lang-detector.php file:, (*31)

'languages' => [
    'pt_BR' => 'pt-BR', //will detect pt_BR language, and set pt-BR to the application,
    'pt' => 'pt-BR', //aliasing, will detect pt and set pt-BR to the application
    'pt-br' => "pt-BR", //aliasing, will detect pt-br and set pt-BR to the application (you will need it with subdomain driver)
    'en', //will detect 'en' language
]

or if you are using .env instead of config file:, (*32)

#Just put the languages in a comma-separated string.
#To aliase the language use the notation ":", "=", ":=" or  "=>" to separate the alias and its value.
LANG_DETECTOR_LANGUAGES="pt_BR:pt-BR, pt:pt-BR, pt-br:pt-BR, en"

Suggestions

The default Laravel language lines are available translated into 51 languages here:, (*33)

If you want to translate your models you can use this package:, (*34)

License

MIT., (*35)

The Versions

11/03 2017

1.0.x-dev

1.0.9999999.9999999-dev

Detect the language for your application using browser preferences, subdomains or route prefixes.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel language lumen locale i18n

11/03 2017

v1.0.19

1.0.19.0

Detect the language for your application using browser preferences, subdomains or route prefixes.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel language lumen locale i18n

11/03 2017

dev-master

9999999-dev

Detect the language for your application using browser preferences, subdomains or route prefixes.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel language lumen locale i18n

11/03 2017

dev-develop

dev-develop

Detect the language for your application using browser preferences, subdomains or route prefixes.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel language lumen locale i18n

08/01 2016

v1.0.18

1.0.18.0

Detect the language for your application using browser preferences, subdomains or route prefixes.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel language lumen locale i18n

08/01 2016

v1.0.17

1.0.17.0

Detect the language for your application using browser preferences, subdomains or route prefixes.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel language lumen locale i18n

30/12 2015

v1.0.16

1.0.16.0

Detect the language for your application using browser preferences, subdomains or route prefixes.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel language lumen locale i18n

30/12 2015

v1.0.15

1.0.15.0

Detect the language for your application using browser preferences, subdomains or route prefixes.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel language lumen locale i18n

30/12 2015

v1.0.14

1.0.14.0

Detect the language for your application using browser preferences, subdomains or route prefixes.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel language lumen locale i18n

30/12 2015

v1.0.13

1.0.13.0

Detect the language for your application using browser preferences, subdomains or route prefixes.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel language lumen locale i18n

30/12 2015

v1.0.12

1.0.12.0

Detect the language for your application using browser preferences, subdomains or route prefixes.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel language lumen locale i18n

30/12 2015

v1.0.11

1.0.11.0

Detect the language for your application using browser preferences, subdomains or route prefixes.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel language lumen locale i18n

30/12 2015

v1.0.10

1.0.10.0

Detect the language for your application using browser preferences, subdomains or route prefixes.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel language lumen locale i18n

22/07 2015

v1.0.9

1.0.9.0

Detect the language for your application using browser preferences, subdomains or route prefixes.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel language lumen locale i18n

22/07 2015

v1.0.8

1.0.8.0

Detect the language for your application using browser preferences, subdomains or route prefixes.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel language lumen locale i18n

21/07 2015

v1.0.7

1.0.7.0

Detect the language for your application using browser preferences, subdomains or route prefixes.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel language lumen locale i18n

19/07 2015

v1.0.6

1.0.6.0

Detect the language for your application using browser preferences, subdomains or route prefixes.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel language lumen locale i18n

17/07 2015

v1.0.5

1.0.5.0

Detect the language for your application using browser preferences, subdomains or route prefixes.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel language lumen locale i18n

16/07 2015

v1.0.4

1.0.4.0

Detect the language for your application using browser preferences, subdomains or route prefixes.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel language lumen locale i18n

16/07 2015

v1.0.3

1.0.3.0

Detect the language for your application using browser preferences, subdomains or route prefixes.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel language lumen locale i18n

16/07 2015

v1.0.2

1.0.2.0

Detect the language for your application using browser preferences, subdomains or route prefixes.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel language lumen locale i18n

16/07 2015

v1.0.1

1.0.1.0

A Laravel and Lumen Language Detector.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel language lumen locale i18n

15/07 2015

v1.0.0

1.0.0.0

A Laravel and Lumen Language Detector.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel language lumen locale i18n

14/07 2015

0.2.x-dev

0.2.9999999.9999999-dev

A Laravel and Lumen Automatic Language Detector.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel language lumen locale i18n

06/07 2015

v0.2.2

0.2.2.0

A Laravel and Lumen Automatic Language Detector.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel language lumen locale i18n

06/07 2015

v0.2.1

0.2.1.0

A Laravel and Lumen Automatic Language Detector.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel language lumen locale i18n

06/07 2015

v0.2.0

0.2.0.0

A Laravel and Lumen Automatic Language Detector.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel language lumen locale i18n

04/07 2015

v0.1.2

0.1.2.0

A Laravel and Lumen Automatic Language Detector.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel language lumen locale i18n

04/07 2015

v0.1.1

0.1.1.0

A Laravel and Lumen Automatic Language Detector.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel language lumen locale i18n

03/07 2015

v0.1.0

0.1.0.0

A Laravel and Lumen Automatic Language Detector.

  Sources   Download

MIT

The Requires

 

laravel language lumen locale i18n

30/06 2015

v0.0.1

0.0.1.0

A Laravel and Lumen Automatic Locale Detector.

  Sources   Download

MIT

The Requires

 

laravel lumen locale