Laravel Multiple Language

Laravel Multiple Language

  1. Depend on HTML tag "language' and 'lang' directory package

  2. Use Middleware Set Session locale

  3. Generate each language standard Url

Depend on HTML tag "language' and 'lang' directory package

  1. <html lang="{{app()->getLocale()}}">

2. Use Middleware Set Session locale

  1. php artisan make:middleware Localization

       if (Session::has('locale')) {
                   //validation Regex
            switch (substr($request->path(), 0, 2)) {
                case 'en':
                    Session::put('locale', 'en');
                    \App::setlocale(Session::get('locale'));
                break;
                default:
                    Session::put('locale', 'zh-CN');
                    \App::setlocale(Session::get('locale'));
            }
       }
  1. App\Http\kernel

3. Generate each language standard Url

  1. Use Help function

function multiple_url($lang, $url, $target = "") {
    switch($lang) {
        case "en":
            return $url . '/en' . $target;
        break;
        default:
            return $url . $target;
    }
    return $url;
}

Reference

  1. Adding multi-language functionality in a website developed in laravel.

  2. Localization

标签: none

添加新评论