2017 © Pedro Peláez
 

library laravel-subdomain-localization

Subdomain localization support for Laravel

image

hoyvoy/laravel-subdomain-localization

Subdomain localization support for Laravel

  • Friday, April 20, 2018
  • by mario-hoyvoy
  • Repository
  • 3 Watchers
  • 6 Stars
  • 121 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 10 Forks
  • 1 Open issues
  • 15 Versions
  • 55 % Grown

The README.md

Build Status Codacy Badge StyleCI, (*1)

Laravel-Subdomain-Localization

Subdomain localization support for Laravel, (*2)

Laravel laravel-subdomain-localization Lifecycle
^5.5 ^5.5 January 24, 2017
Bug fixes until January 2019
Security fixes until June 2020
^5.6 ^5.6 February 7, 2018
6 months of bug fixes
1 year of security

Table of Contents

Installation

Install with composer, (*3)

composer require hoyvoy/laravel-subdomain-localization

From Laravel 5.5 onwards, it's possible to take advantage of auto-discovery of the service provider. For Laravel versions before 5.5, you must register the service provider in your config/app.php, (*4)

Hoyvoy\Localization\LocalizationServiceProvider::class,

Laravel-Subdomain-Localization comes with 2 facades: Localize and Router., (*5)

If you want to use them, open config/app.php and add the following lines to the aliases array:, (*6)

    ...
    'Localize'  => Hoyvoy\Localization\Facades\Localize::class,
    'Router'    => Hoyvoy\Localization\Facades\Router::class,
    ...

Laravel comes with a middleware that can be used to enforce the use of a language subdomain., (*7)

If you want to use it, open app/Http/kernel.php and register this route middleware by adding it to the routeMiddleware array:, (*8)

    ...
    'localize' => \Hoyvoy\Localization\Middleware\Localization::class,
    ...

Usage

Locale detection

Open app/Providers/RouteServiceProvider.php and add a call to detectLocale() from the boot method. For example, using the facade:, (*9)

    ...
    use Hoyvoy\Localization\Facades\Localize;
    ...
    public function boot(Router $router)
    {
        // This will guess a locale from the current HTTP request
        // and set the application locale
        Localize::detectLocale();

        parent::boot($router);
    }
    ...

Once you have done this, there is nothing more that you MUST do. Laravel application locale has been set and you can use other locale-dependant Laravel components (e.g. Translation) as you normally do., (*10)

Middleware

If you want to enforce the use of a language subdomain for some routes, you can simply assign the middleware provided, for example as follows in app/Http/routes.php:, (*11)

    // Without the localize middleware, this route can be reached with or without language subdomain
    Route::get('logout', 'AuthController@logout');

    // With the localize middleware, this route cannot be reached without language subdomain
    Route::group([ 'middleware' => [ 'localize' ]], function() {

        Route::get('welcome', 'WelcomeController@index');

    });

For more information about Middleware, please refer to Laravel docs., (*12)

Route translation

If you want to use translated routes (en.yourdomain.com/welcome, fr.yourdomain.com/bienvenue), proceed as follows:, (*13)

First, create language files for the languages that you support:, (*14)

resources/lang/en/routes.php:, (*15)

    return [

        // route name => route translation
        'welcome' => 'welcome',
        'user_profile' => 'user/{username}',

    ];

resources/lang/fr/routes.php:, (*16)

    return [

        // route name => route translation
        'welcome' => 'bienvenue',
        'user_profile' => 'utilisateur/{username}',

    ];

Then, here is how you define translated routes in app/Http/routes.php:, (*17)

    Route::group([ 'middleware' => [ 'localize' ]], function() {

        Route::get(Router::resolve('routes.welcome'), 'WelcomeController@index');

    });

You can of course name the language files as you wish, and pass the proper prefix (routes. in the example) to the resolve() method., (*18)

Configuration

Configuration file

In order to edit the default package configuration, you can run the following artisan command:, (*19)

php artisan vendor:publish --provider="Hoyvoy\Localization\LocalizationServiceProvider" --tag="config"

Once you have done that, you will find the config file at config/localization.php., (*20)

Configuration values

  • available_locales (default: ['en', 'de'])

An array of the locales accepted by the routing system., (*21)

  • cookie_localization (default: true)

Use this option to enable or disable the use of cookies during the locale detection., (*22)

  • browser_localization (default: true)

Use this option to enable or disable the use of the browser settings during the locale detection., (*23)

  • cookie_name (default: 'locale')

Here you may change the name of the cookie used to save the locale. This option is used only if localization with cookie is enabled., (*24)

  • domain (default: env('DOMAIN'))

Here you may change the name of the domain used in your application. By default, the domain is read from the .env file., (*25)

  • aliases (defaults to empty array)

You can specify aliases to use custom subdomains instead of locale codes., (*26)

Example: ['german' => 'de'] means that german.mydomain.dev sets locale to de and if locale is set to de, routes will be prefixed with german. instead of de., (*27)

Useful functions

The package provides useful functions that you can use - for example - in your views:, (*28)

Translate current URL

    <a href="{{ Router::current('fr') }}">See the french version</a>

Use Router::current(string $locale) this to generate an alternate version of the current route. This will return an url with the proper subdomain and also translate the uri if necessary., (*29)

Get alternate versions of the current URL

    @foreach (Router::getCurrentVersions() as $locale => $url)
    <a href="{{ $url }}">{{ $locale }}</a>
    @endforeach

Use Router::getCurrentVersions(bool $excludeCurrentLocale = true) to fetch all localized versions of the current route. This will return an array of $locale => $url items that you can use to generate links to alternate versions., (*30)

You can pass false as parameter for $excludeCurrentLocale to let function also returns an item for the current locale., (*31)

Get localized version for a given route

    <a href="{{ Router::url('user_profile', [ 'username' => 'JohnDoe' ], 'fr') }}">See JohnDoe's profile</a>

Use Router::url($routeName, $routeAttributes = null, $locale = null) to generate an alternate version of the given route. This will return an url with the proper subdomain and also translate the uri if necessary., (*32)

You can pass route parameters if necessary. If you don't give a specific locale, it will use the current locale., (*33)

Changelog

To see what has changed in recent versions, see the CHANGELOG., (*34)

License

This package is licensed under the MIT license., (*35)

The Versions

20/04 2018

dev-master

9999999-dev https://github.com/hoyvoy/laravel-subdomain-localization

Subdomain localization support for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mario Aguilar Alves

laravel php localization subdomain

20/04 2018

5.5.x-dev

5.5.9999999.9999999-dev https://github.com/hoyvoy/laravel-subdomain-localization

Subdomain localization support for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mario Aguilar Alves

laravel php localization subdomain

20/04 2018

5.5.1

5.5.1.0 https://github.com/hoyvoy/laravel-subdomain-localization

Subdomain localization support for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mario Aguilar Alves

laravel php localization subdomain

20/04 2018

5.6.x-dev

5.6.9999999.9999999-dev https://github.com/hoyvoy/laravel-subdomain-localization

Subdomain localization support for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mario Aguilar Alves

laravel php localization subdomain

20/04 2018

5.6.6

5.6.6.0 https://github.com/hoyvoy/laravel-subdomain-localization

Subdomain localization support for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mario Aguilar Alves

laravel php localization subdomain

18/04 2018

5.6.5

5.6.5.0 https://github.com/hoyvoy/laravel-subdomain-localization

Subdomain localization support for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mario Aguilar Alves

laravel php localization subdomain

21/03 2018

5.6.4

5.6.4.0 https://github.com/hoyvoy/laravel-subdomain-localization

Subdomain localization support for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mario Aguilar Alves

laravel php localization subdomain

12/02 2018

5.6.2

5.6.2.0 https://github.com/hoyvoy/laravel-subdomain-localization

Subdomain localization support for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mario Aguilar Alves

laravel php localization subdomain

12/02 2018

5.6.0

5.6.0.0 https://github.com/hoyvoy/laravel-subdomain-localization

Subdomain localization support for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mario Aguilar Alves

laravel php localization subdomain

12/02 2018

dev-analysis-8K40EQ

dev-analysis-8K40EQ https://github.com/hoyvoy/laravel-subdomain-localization

Subdomain localization support for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mario Aguilar Alves

laravel php localization subdomain

12/02 2018

dev-analysis-q1DEpw

dev-analysis-q1DEpw https://github.com/hoyvoy/laravel-subdomain-localization

Subdomain localization support for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mario Aguilar Alves

laravel php localization subdomain

30/08 2017

dev-develop

dev-develop https://github.com/hoyvoy/laravel-subdomain-localization

Subdomain localization support for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mario Aguilar Alves

laravel php localization subdomain

30/08 2017

5.5.0

5.5.0.0 https://github.com/hoyvoy/laravel-subdomain-localization

Subdomain localization support for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mario Aguilar Alves

laravel php localization subdomain

27/01 2017

5.4.0

5.4.0.0 https://github.com/hoyvoy/laravel-subdomain-localization

Subdomain localization support for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mario Aguilar Alves

laravel php localization subdomain

17/08 2015

1.0.0

1.0.0.0 https://github.com/LaurentEsc/Laravel-Subdomain-Localization

Subdomain localization support for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Laurent Escalier

laravel php localization subdomain