locastic / symfony-translation-bundle
Symfony translation bundle
Installs: 4 484
Dependents: 1
Suggesters: 0
Security: 0
Stars: 3
Watchers: 7
Forks: 1
Open Issues: 1
Type:symfony-bundle
Requires
- php: >=7.4 || >=8.1
- doctrine/doctrine-bundle: ^2.5
- doctrine/doctrine-migrations-bundle: ^3.2
- pagerfanta/pagerfanta: ^2.6 || ^3.6 || ^4.0
- symfony/console: 5.4.* || 6.*
- symfony/form: 5.4.* || 6.*
- symfony/http-kernel: 5.4.* || 6.*
- symfony/property-access: 5.4.* || 6.*
- symfony/translation: 5.4.* || 6.*
- symfony/yaml: 5.4.* || 6.*
- twig/twig: 2.15.* || ^3.5
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.13
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.