Git Product home page Git Product logo

laravel-localization's Introduction

Laravel localization

Very easy and light package for loclization. Routing based on available locales and easy to get rid of default language prefix from route

Installation

Composer

Add Laravel Localization to your composer.json file

"rohit/laravel-localization": "^1.0"

Run composer install to get the latest version of package

Or you can directly run the composer require command

composer require rohit/laravel-localization

Configuration

After the package install is completed you need to configure config/app.php and add Providers and Aliases

    'providers` => [
        .......
        .......
        Rohit\LaravelLocalization\LaravelLocalizationServiceProvider::class
    ]
    'aliases' => [
        ......
        ......
        'Localization' => Rohit\LaravelLocalization\Facades\LaravelLocalization::class
    ]

Vendor Publish

After the above steps, you need to publish vendor for this packge. It will create laravel-localization.php file under config folder. This folder contains the configuration for your locales.

php artisan vendor:publish --provider="Rohit\LaravelLocalization\LaravelLocalizationServiceProvider"

The file laravel-localization.php will contain the following structure

    return [
        // Add any language you want to support and comes as prefix in the url
        'all_locales' => [
            'en',
            'th'
        ],
        'default_locale' => 'th', // Default locale will not be shown in the url
    ];

Here you can add as many locales available in your project and set the default_locale to the value for which you want to exclude the prefix

NOTE: You can keep this field blank if you want all prefix in th url

For Example:

If your project has en and th as the available locales and if you set th as your default locale. Then the url will look like,

For English: http://your_domain.com/en/your_page

For Thai http://your_domain.com/your_page

It will skip default locale th from the url

Middleware

After this, you need to update the app\Http\Kernel.php file and add the following line under routeMiddleware

    protected $routeMiddleware = [
        ........
        ........
        'localization' => \Rohit\LaravelLocalization\Middleware\LanguageHandler::class,
    ]

Routing

Finally you can manage all the routes with this configuration and middleware for smooth operation

Update app\Http\routes.php file and add all your routes under this group

    Route::group([
        'prefix' => Localization::setLocale(),
        'middleware' => ['localization']
    ], function() {
        // All your routes here
    });

Helpers

There is a helper function to get the url for language switcher on the current page. You simply need to call a function with the locale

Localization::getLocaleUrl('en')

laravel-localization's People

Contributors

rohitsubedi avatar

Stargazers

Tez avatar Weerapat avatar

laravel-localization's Issues

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.