Search
 
SCRIPT & CODE EXAMPLE
 

PHP

laravel localization

1. create language file in lang folder
	en.json, jpn.json, ko.json, hi.json..
    jpn.json->{"Log in": "ログイン", "Register": "登録", etc...}
    
2. create controller and middleware for localization
	-> $ php artisan make:middleware Localization
    -> $ php artisan make:controller LocalizationController

3. declare localization middleware in kernel.php
	  protected $middlewareGroups = [
        'web' => [
            AppHttpMiddlewareEncryptCookies::class,
           	...
            Localization::class,
        ],
]

4. create route for localization.
	  Route::get('/lang/{locale}', [AppHttpControllersLocalizationController::class,'lang_change'])->name('lang.change');
      
5. In LocalizationController.php
   public function lang_change($locale){
        Session::put("locale",$locale);
        return redirect()->back();
    }
    
6. In localization.php <- middleware file
->  public function handle(Request $request, Closure $next)
    {
        if(Session::has("locale"))
            App::setLocale(Session::get("locale"));
        else
            App::setLocale(config("app.locale"));
        return $next($request);    }
}


7. use in blade
	{{__('Log in')}}
    {{__('Register')}}
    
    
8. choose languages from blade
      <div class="dropdown-menu dropdown-menu-end" aria-labelledby="navbarDropdown">
        <ul class="list-group list">
          <li class="list-group-item border-0">
              <a href="{{route('lang.change','en')}}">English</a>
          </li>
          <li class="list-group-item border-0">
              <a href="{{route('lang.change','jpn')}}">Japanese</a>
          </li>
          <li class="list-group-item border-0">
              <a href="{{route('lang.change','ko')}}">Korea</a>
          </li>
           <li class="list-group-item border-0">
              <a href="{{route('lang.change','hi')}}">Hindi</a>
          </li>
        </ul>
      </div>
Comment

PREVIOUS NEXT
Code Example
Php :: php declare array 
Php :: laravel blade php variable concatenate javascript variable 
Php :: laravel notification attach file 
Php :: clear cache in laravel without artisan 
Php :: laravel modules 
Php :: rule for radio button in laravel 
Php :: get admin url wordpress 
Php :: filter wordpress 
Php :: laravel queue timeout 
Php :: php find multiple value in array 
Php :: laravel exclude field 
Php :: php regex format number with commas and decimal 
Php :: ajax search request 
Php :: how create page 419 in laravel 
Php :: update cart subtotal woocommerce 
Php :: array join pgp 
Php :: parse json phph 
Php :: remove all items of an array except the last 5 in php 
Php :: if is page woocommerce 
Php :: laravel carbon date format 
Php :: get shipping price of choosen shipping method woocommerce 
Php :: get all error message in array form laravel validation in laravel 
Php :: session value not removed php 
Php :: where like in laravel 
Php :: get array of last 3 dates with carbon 
Php :: execute script php command line 
Php :: how to use wherehas in laravel 
Php :: laravel collection except 
Php :: insert array values in database using codeigniter 
Php :: array_map in php 
ADD CONTENT
Topic
Content
Source link
Name
3+3 =