optimistdigital/nova-translatable
Composer 安装命令:
composer require optimistdigital/nova-translatable
包简介
A laravel-translatable extension for Laravel Nova.
README 文档
README
This Laravel Nova allows you to make any input field spatie/laravel-translatable compatible and localisable.
Requirements
PHP: ^8.0laravel/nova: ^4.12spatie/laravel-translatable: ^4.0 || ^5.0 || ^6.0
Features
- Supports almost all fields (including third party ones)
- Supports default validation automatically
- Simple to implement with minimal code changes (after
spatie/laravel-translatablesupport) - Locale tabs to switch between different locale values of the same field
- Double click on a tab to switch all fields to that locale
- Supports nova-settings package
Known non-working fields
ImageandFile- Workarounds:
- outl1ne/nova-media-hub
- or any library that uploads images/files using XHR
- Workarounds:
Limitations
- The following methods can not be used, as this package uses them internally:
resolveUsingfillUsing
Screenshots
Installation
Firstly, set up spatie/laravel-translatable.
Install the package in a Laravel Nova project via Composer:
# Install nova-translatable composer require outl1ne/nova-translatable # Publish configuration (optional, but useful for setting default locales) php artisan vendor:publish --tag="nova-translatable-config"
Usage
Call ->translatable() on any field, like so:
// Any Nova field Text::make('Name') ->rules('required', 'min:2') ->translatable(), // Any third-party input field Multiselect::make('Football teams') ->rules('required') ->translatable(), // Optionally pass custom locales on a per-field basis Number::make('Population') ->translatable([ 'en' => 'English', 'et' => 'Estonian', ]),
Validation
It's possible to define locale specific validation rules.
To do so, add the ->rulesFor() on your field and the HandlesTranslatable trait to your Nova resource.
->rulesFor accepts array|string|callable locales and array|callable rules.
use Outl1ne\NovaTranslatable\HandlesTranslatable; class Product extends Resource { use HandlesTranslatable; public function fields(Request $request) { return [ Text::make(__('Name'), 'name') ->sortable() ->translatable() ->rules(['max:255']) ->rulesFor('en', [ 'required', ]) ->rulesFor(['en', 'et'], function ($locale) { return ["unique:products,name->$locale{{resourceId}}"]; }), ]; } }
In this example, rules will be added to the following values
max: name.* required: name.en unique: name.en & name.et
Configuration
You can define default locales for all the translatable fields in the config file. The config file can be published using:
php artisan vendor:publish --tag="nova-translatable-config"
Fill other locales from config option
The configuration option fill_other_locales_from allows you to pre-fill other locales from just one locale. This requires the resources to also have the HandlesTranslatable trait.
One select for all fields on a page
If you don't want to display the locale select next to each field, you can set the display_type to none and add a Outl1ne\NovaTranslatable\Fields\LocaleSelect field to your Nova resource. This will render a single select for all fields.
Custom locale display
To customize the locale display you can use Nova::provideToScript to pass customLocaleDisplay as in the example below.
// in app/Providers/NovaServiceProvider.php public function boot() { Nova::serving(function () { Nova::provideToScript([ // ... 'customLocaleDisplay' => [ 'en' => <img src="/flag-en.png"/>, 'et' => <img src="/flag-et.png"/>, ] ]); }); }
Edge cases
BelongsToMany allowDuplicateRelations corner-case
When using this field inside a BelongsToMany as a pivot field with ->allowDuplicateRelations() and you want to filter out exact matches using the NotExactlyAttached rule, use the BelongsToManyTranslatable field instead of the regular BelongsToMany.
Credits
License
This project is open-sourced software licensed under the MIT license.
optimistdigital/nova-translatable 适用场景与选型建议
optimistdigital/nova-translatable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 460.71k 次下载、GitHub Stars 达 202, 最近一次更新时间为 2019 年 12 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「i18n」 「translatable」 「nova」 「laravel」 「translate」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 optimistdigital/nova-translatable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 optimistdigital/nova-translatable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 optimistdigital/nova-translatable 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Laravel Nova card that shows you your system information.
A Laravel Nova card.
Easy to use i18n translation PHP class for multi-language websites
A custom URL rule class for Yii 2 which allows to create translated URL rules
A Laravel Eloquent model trait for translatable resource
An Astrotomic Translatable extension for Laravel Nova.
统计信息
- 总下载量: 460.71k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 202
- 点击次数: 26
- 依赖项目数: 5
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-12-20


