typicms / laravel-translatable-bootforms
赋予typicms/bootforms spatie/laravel-translatable的功能。
8.0.1
2024-05-15 12:04 UTC
Requires
- php: ^8.1
- illuminate/support: ~10.0|~11.0
- spatie/laravel-translatable: ^6.0
- typicms/bootforms: ^4.0.0
- typicms/form: ^3.0.0
Requires (Dev)
- nunomaduro/larastan: ^2.0
- orchestra/testbench: ~8.3.0
- php-coveralls/php-coveralls: ~2.5.2
- phpunit/phpunit: ^9.5.10
README
使BootForms与Laravel Translatable无缝配合!
通过导入此包,使用BootForms生成可翻译表单变得轻而易举。
安装
-
运行Composer require命令安装包,Laravel将自动发现服务提供者。
composer require typicms/laravel-translatable-bootforms
-
在您的应用配置中,将Facade添加到
$aliases
数组中'aliases' => [ ... 'TranslatableBootForm' => TypiCMS\LaravelTranslatableBootForms\Facades\TranslatableBootForm::class, ],
-
发布配置文件
php artisan vendor:publish --provider="TypiCMS\LaravelTranslatableBootForms\TranslatableBootFormsServiceProvider" --tag="config"
使用方法
只需像使用BootForm
一样使用TranslatableBootForm
Facade即可!就是这样。现在将为在Translatable的配置文件中设置的每个语言生成多个表单输入。它们将为每种语言提供相应的值,并且将保存所有翻译,无需任何代码操作。
如果您不确定如何使用它,请查阅BootForms文档。
示例
// View {!! BootForm::text('Name', 'name') ->placeholder('My placeholder') !!} // Output <div class="form-group"> <label for="name">Name</label> <input type="text" name="name" class="form-control" placeholder="My Placeholder"> </div> // Controller public function postEdit($request) { $someModel->save($request->all()); }
// View {!! TranslatableBootForm::text('Name', 'name') ->placeholder('My placeholder') !!} // Output <div class="form-group form-group-translation"> <label for="name[en]">Name (en)</label> <input type="text" name="name[en]" class="form-control" placeholder="My Placeholder" data-language="en"> </div> <div class="form-group form-group-translation"> <label for="name[nl]">Name (nl)</label> <input type="text" name="name[nl]" class="form-control" placeholder="My Placeholder" data-language="nl"> </div> // Controller public function postEdit($request) { $someModel->save($request->all()); }
在指定参数时,您可以使用%name
和%locale
占位符。占位符将被替换为相应的输入名称或区域设置。这对于双向数据绑定库(如Angular.js或Vue.js)非常有用。例如。
{!! TranslatableBootForm::text('Title', 'title') ->attribute('some-attribute', 'Name: %name') ->attribute('another-attribute', 'Locale: %locale') !!} // Output <div class="form-group form-group-translation"> <label for="title[en]">Title (en)</label> <input type="text" name="title[en]" class="form-control" some-attribute="Name: title[en]" another-attribute="Locale: en" data-language="en"> </div> <div class="form-group form-group-translation"> <label for="title[nl]">Title (nl)</label> <input type="text" name="title[nl]" class="form-control" some-attribute="Name: title[nl]" another-attribute="Locale: nl" data-language="nl"> </div>
要仅渲染某些选择的区域设置的表单元素,显式调用renderLocale()
作为最后的方法,并将区域设置或区域设置数组作为第一个参数传递
TranslatableBootForm::text('Name', 'name') ->renderLocale('en')
如果您需要仅对某些区域设置应用方法,请给方法加上后缀ForLocale
,并将区域设置或区域设置数组作为第一个参数传递
TranslatableBootForm::text('Name', 'name') ->dataForLocale('en', 'attributeName', 'attributeValue') ->addClassForLocale(['en', 'nl'], 'addedClass')
有关自定义标签中的区域指示器(以及其他设置),请参阅配置文件。