PHP code example of pmochine / laravel-tongue

1. Go to this page and download the library: Download pmochine/laravel-tongue library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

pmochine / laravel-tongue example snippets


  ...
  'speaks-tongue' => \Pmochine\LaravelTongue\Middleware\TongueSpeaksLocale::class,
  ...

>    Pmochine\LaravelTongue\ServiceProvider::class,
>

  public function boot()
  {
      // This will guess a locale from the current HTTP request
      // and set the application locale.
      tongue()->detect();
      
      //If you use Carbon you can set the Locale right here.
      \Carbon\Carbon::setLocale(tongue()->current()); 
      
      parent::boot();
  }
  ...

  // 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' => [ 'speaks-tongue' ]], function() {
  
      Route::get('welcome', 'WelcomeController@index');
  
  });

  <!doctype html>
  <html lang="{{tongue()->current()}}" dir="{{tongue()->leftOrRight()}}">

    <head>
      @

  return [
    
    // route name => route translation
    'welcome' => 'welcome',
    'user_profile' => 'user/{username}',
  
  ];

  return [
    
    // route name => route translation
    'welcome' => 'bienvenue',
    'user_profile' => 'utilisateur/{username}',
    
  ];

  Route::group([ 'middleware' => [ 'speaks-tongue' ]], function() {
    
      Route::get(dialect()->interpret('routes.welcome'), 'WelcomeController@index');
    
  });

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

  @foreach (dialect()->translateAll(true) as $locale => $url)
      <a href="{{ $url }}">{{ $locale }}</a>
  @endforeach

  <a href="{{ dialect()->translate('routes.user_profile', [ 'username' => 'JohnDoe' ], 'fr') }}">See JohnDoe's profile</a>
  // Result: https://fr.example.com/utilisateur/JohnDoe 

  <a href="{{ dialect()->redirectUrl(route('home'), 'fr') }}">See Homepage in French</a>
  // Result: https://fr.example.com 

  $collection = tongue()->speaking(); //returns collection

  $keys = tongue()->speaking()->keys()->all(); //['en','de',..]
  $sorted = tongue()->speaking()->sort()->all(); //['de','en',..]

  tongue()->speaking('BCP47', 'en'); // en-GB
  tongue()->speaking('subdomains'); // ['admin']
  tongue()->speaking('subdomains', 'admin'); // true
  tongue()->speaking('aliases'); // ['gewinnen' => 'de', 'gagner' => 'fr]
  tongue()->speaking('aliases', 'gewinnen'); //' de'

  $locale = tongue()->current(); //de

  $name = tongue()->current('name'); //German
  $script = tongue()->current('script'); //Latn
  $native = tongue()->current('native'); //Deutsch
  $regional = tongue()->current('regional'); //de_DE

  <ul>
      @foreach(tongue()->speaking()->all() as $localeCode => $properties)
          <li>
              <a rel="alternate" hreflang="{{ $localeCode }}" href="{{ dialect()->current($localeCode) }}">
                  {{ $properties['native'] }}
              </a>
          </li>
      @endforeach
  </ul>

  /**
   * Sets the locale in the app
   * @return redirect to previous url
   */
  public function store()
  {
    $locale = request()->validate([
      'locale' => '
bash
  php artisan vendor:publish --provider="Pmochine\LaravelTongue\ServiceProvider" --tag="config"