locastic/symfony-translation-bundle
最新稳定版本:1.0.0
Composer 安装命令:
composer require locastic/symfony-translation-bundle
包简介
Symfony translation bundle
README 文档
README
Locastic Translation Bundle
This bundle gives the basis to allow translating your messages from any admin panel. You will now be able to create translation migrations, the same way you create your database migrations. It also provides a few services to help you expose and manage your translations.
Installation
composer require locastic/symfony-translation-bundle
Configuration
imports: - { resource: "@LocasticSyliusTranslationPlugin/Resources/config/config.yaml" } locastic_sylius_translation: default_locale: en # The default locale to use locales: # The list locales supported by your application - en
Note: This bundle supports locales the same way as Symfony, meaning that en will be the fallback for en_US and en_GB.
Usage
Creating a translation migration
<?php declare(strict_types=1); namespace App\TranslationMigrations; use Locastic\SymfonyTranslationBundle\Provider\ThemesProviderInterface; use Locastic\SymfonyTranslationBundle\TranslationMigration\AbstractTranslationMigration; final class Version20230201074700 extends AbstractTranslationMigration { public function up(): void { $this->addTranslation('test.translation', 'messages', 'en', 'This is a test translation', ThemesProviderInterface::NAME_DEFAULT); } }
Then you can run the migration with the following command:
bin/console locastic:symfony-translation:migration:migrate
Note: down for migrations is not supported yet.
Exposing translations
You will need to create a controller to expose your translations. Here is an example:
<?php declare(strict_types=1); namespace App\Controller; use Locastic\SymfonyTranslationBundle\Form\Type\SearchTranslationType; use Locastic\SymfonyTranslationBundle\Model\SearchTranslation; use Locastic\SymfonyTranslationBundle\Utils\SearchTranslationsUtilsInterface; use Symfony\Component\Form\FormFactoryInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Twig\Environment; final class IndexTranslationAction { public function __construct( private SearchTranslationsUtilsInterface $searchTranslationsUtils, private Environment $twig, private FormFactoryInterface $formFactory, ) { } public function __invoke(Request $request): Response { $search = new SearchTranslation(); $searchForm = $this->formFactory->create(SearchTranslationType::class, $search); $searchForm->handleRequest($request); $pagerFanta = $this->searchTranslationsUtils->searchTranslationsFromRequest($request, $search, $searchForm); return new Response($this->twig->render('{yourTemplate}', [ 'translations' => $pagerFanta, 'resources' => $pagerFanta, 'searchForm' => $searchForm->createView(), ])); } }
And then create a template displaying your translations.
Recommended way is to save translations via an AJAX and use the SaveTranslationsAction class to save them.
统计信息
- 总下载量: 5.7k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 0
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-01-30