PHP code example of day4 / switch-locale

1. Go to this page and download the library: Download day4/switch-locale 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/ */

    

day4 / switch-locale example snippets


composer isan vendor:publish --tag=public


public function tools()
{
    return [
        new SwitchLocale([
            "locales" => [
                "en" => "English",
                "de" => "German"
            ],
            "useFallback" => false,
            "customDetailToolbar" => false //optional
        ])
    ];
}

// User Model app/User.php

protected $casts = [
    'email_verified_at' => 'datetime',
    'locale' => 'array'
];

public function allowedLocale() {
    return $this->allowedAllLocale() || $this->locale[app()->getLocale()];
}

public function allowedAllLocale() {
    return $this->isAdmin(); // As an example, admin is allowed all locale
}

// PostPolicy



namespace App\Policies;

use App\Post;
use App\User;
use Illuminate\Auth\Access\HandlesAuthorization;

class PostPolicy
{
    use HandlesAuthorization;

    /**
     * Determine whether the user can view any posts.
     */
    public function viewAny(User $user)
    {
        return true;
    }

    /**
     * Determine whether the user can view the post.
     */
    public function view(User $user, Post $post)
    {
        return $post->status == 'published' || ($user && $user->id == $post->author_id);
    }

    /**
     * Determine whether the user can create posts.
     */
    public function create(User $user)
    {
        return $user && $user->allowedLocale();
    }

    /**
     * Determine whether the user can update the post.
     */
    public function update(User $user, Post $post)
    {
        return $user && $user->allowedLocale();
    }

    /**
     * Determine whether the user can update the post.
     */
    public function delete(User $user, Post $post)
    {
        return $user && $user->allowedLocale();
    }
}