day4/switch-locale
Composer 安装命令:
composer require day4/switch-locale
包简介
A Laravel Nova tool to switch the locale
README 文档
README
For Astrotomic's Laravel Translatable
This tool provides a nice header dropdown to quickly switch between locales. Locale is cached per user and sets the main app locale.
It also provides a Language field which gives you the status of the translation on a specific resource.
NOTE If you use Spatie's Laravel Translatable then use Laravel Nova Locale Anywhere Tool
Installation
composer require day4/switch-locale
php artisan vendor:publish --tag=public
Usage
To begin using this, you must first register the tool in NovaServiceProvider under tools. The tool utilises Astrotomic's Laravel Translatable package.
public function tools()
{
return [
new SwitchLocale([
"locales" => [
"en" => "English",
"de" => "German"
],
"useFallback" => false,
"customDetailToolbar" => false //optional
])
];
}
Dropdown
The package provides a switch for the languages that you have to insert into Nova's layout file. You can do that by overwriting the layout.blade.php file and insert it after the user dropdown.
…
<dropdown class="ml-auto h-9 flex items-center dropdown-right">
@include('nova::partials.user')
</dropdown>
<switch-locale-dropdown class="ml-6"></switch-locale-dropdown>
…
Resource status field
You can then define the field in the resource.
use Day4\SwitchLocale\Language;
Language::make(__('Translation'))
Delete translation toolbar button
The package provides a CustomDetailToolbar component that you can toggle via configuration. Optionally, you can also only grab the <delete-toolbar-button> from the package and paste it into your own custom detail toolbar.
Language Permissions
If you want to allow users access to only certain languages this tool can also display which langauges they have access to.
Setup
In order for this to work the user needs to have a locale property that returns an object of locale with boolean values for which the user has access to.
// User table migration
$table->string('locale')->default('{}');
// 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
}
// User Nova Resource app/Nova/User.php
use Laravel\Nova\Fields\BooleanGroup;
use Day4\SwitchLocale\SwitchLocale;
…
BooleanGroup::make(__('Allowed Locale'), 'locale')->options(SwitchLocale::getLocales()),
Example Usage
// PostPolicy
<?php
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();
}
}
Note You should probably have some other permissions and rules in place to limit users from giving them selves access to languages etc.
Credits
Originally forked from slovenianGooner
day4/switch-locale 适用场景与选型建议
day4/switch-locale 是一款 基于 Vue 开发的 Composer 扩展包,目前已累计 30.42k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2020 年 03 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「nova」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 day4/switch-locale 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 day4/switch-locale 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 day4/switch-locale 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Laravel Nova card that shows you your system information.
A Laravel Nova card.
A Laravel Nova package for publishable fields
A Laravel Nova package for translatable fields
A Laravel Nova Image field. you can paste or drag or chose an image
A Laravel Nova Mail testing tool.
统计信息
- 总下载量: 30.42k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 16
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-03-18





