我在 Symfony 5 專案中使用Translatable和 EasyAdmin,并且配置了 2 種語言。
問題是我需要能夠在 EasyAdmin 中編輯不同語言的記錄,我檢查了 Translatable、EasyAdmin 和 Symfony 的檔案,關于如何將資料庫翻譯集成到 EasyAdmin 的資訊很少。
因此,我在代碼方面有點卡住了,我嘗試setTranslationParameters()在物體 CRUD 控制器內部進行配置并更改了一些配置,DashboardController但是,我認為這不是正確的方法。
有關如何解決此問題的任何建議?感謝您的努力和時間。
uj5u.com熱心網友回復:
在撰寫本文時,EasyAdmin 中不存在此功能,請參閱 Github 上問題答案的鏈接。
https://github.com/EasyCorp/EasyAdminBundle/issues/4982
但是,可以使用不同的包來解決:
- 洗掉
doctrine-extensions/DoctrineExtensions然后安裝KnpLabs/DoctrineBehaviors - 安裝a2lix/translation-form-b??undle
- 創建翻譯欄位:
<?php
declare(strict_types=1);
namespace App\Controller\Admin\Field;
use A2lix\TranslationFormBundle\Form\Type\TranslationsType;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Field\FieldInterface;
use EasyCorp\Bundle\EasyAdminBundle\Field\FieldTrait;
final class TranslationField implements FieldInterface
{
use FieldTrait;
public static function new(string $propertyName, ?string $label = null, array $fieldsConfig = []): self
{
return (new self())
->setProperty($propertyName)
->setLabel($label)
->setFormType(TranslationsType::class)
->setFormTypeOptions([
'default_locale' => 'cz',
'fields' => $fieldsConfig,
]);
}
}
- 使用
TranslationField您的管理 CRUD 控制器內部:
public function configureFields(string $pageName): iterable
{
return [
TextField::new('title', 'title')->hideOnForm(),
TranslationField::new('translations', 'translations', [
'title' => [
'field_type' => TextType::class,
'required' => true,
]
// add more translatable properties into the array
])->setRequired(true)
->hideOnIndex()
];
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/422406.html
標籤:
