Overview
PartitechSonataExtraBundle provides a convenient way to manage translations in Sonata Admin. This bundle includes two main commands: sonata:extra:translation-create-template and sonata:extra:translation-import-template. These commands facilitate the creation and import of translations, optimizing the process and integrating with an external translation service.
Orders
Order sonata:extra:translation-create-template
Description
This command prepares your content for translation. It creates a directory structure with files containing the text to be translated.
Use
bin/console sonata:extra:translation-create-template --site=[SITE_ID] --reference-site=[REFERENCE_SITE_ID] --service="[ADMIN_SERVICE]"
--site: The site ID for which the translations must be created.--reference-site: The identifier of the reference site.--service: The administration service for which translations are needed.
exit
The command generates a set of directories and files under var/cache/translation/YourService/For each item identifier, you will find files such as:
payloadArray.txt: Contains sentences to translate.payloadArray_translated.json: To store the translated content.- Other files to manage structured content like HTML.
Order sonata:extra:translation-import-template
Description
This command imports the translated content into your database, rebuilding the HTML structure.
Usage
After translating the content, run:
bin/console sonata:extra:translation-import-template --service="[ADMIN_SERVICE]"
Overview of the process
- Disable automatic translation: First of all, make sure that automatic translation is turned off.
- Create a translation template: use the command sonata:extra:translation-create-template to generate translation files.
- Translate the content: Submit the content of
payloadArray.txtto the selected translation service or to a professional translator. Fill in the translated content in payloadArray_translated.json. - Manage structured content: for HTML fields, use field_content_payload.txt for the translation and update field_content_translation.txt accordingly.
- Import translations: Run the sonata:extra:translation-import-template command to update your content with the new translations.
Example
# Creating translation templates
bin/console sonata:extra:translation-create-template --site=5 --reference-site=2 --service="Partitech\SonataExtra\Admin\ArticleAdmin"
# Importing translated content
bin/console sonata:extra:translation-import-template --service="Partitech\SonataExtra\Admin\ArticleAdmin"
Manage structured content like HTML
When dealing with translations for text fields containing structured content (for example, HTML), the PartitechSonataExtraBundle provides a sophisticated method to ensure that structure and formatting are preserved. This is particularly important for content including HTML tags, code snippets, images, and other embedded media.
Understanding the structure of files
The command sonata:extra:translation-create-template generates several files to help in this process:
field_content_code_blocks.json: This file contains "code blocks" or sections of HTML content that should not be translated. This may include actual code snippets, embedded media, or other non-text elements in your HTML content.field_content_payload.txt: This file includes the text of your HTML content that requires translation. Before creating this file, the bundle extracts the non-translatable elements (such as those in field_content_code_blocks.json) and replaces them with placeholders. This extraction ensures that only the necessary text is translated, optimizing both the quality and the cost of the translation.field_content_translation.txt: After having translated the content intofield_content_payload.txt, the translated text must be placed in this file.
The translation process
Here is a detailed overview of how the translation process works for structured content:
- Extract non-translatable elements: The bundle identifies and extracts parts of the HTML content that do not need to be translated (for example, code blocks, images) and stores them in field_content_code_blocks.json. These elements are replaced by placeholders in the HTML text.
- The text content to be translated, now free of non-textual elements, is placed in field_content_payload.txt. This text must be submitted for translation.
- Reinsert the translated text: Once translated, the new text is inserted into field_content_translation.txt.
- Rebuilding the HTML content: During the import process (sonata:extra:translation-import-template), the bundle reassembles the HTML content. It combines the translated text from field_content_translation.txt with the non-translatable elements from field_content_code_blocks.json, replacing the placeholders with their original content.
Conclusion
This feature simplifies the translation process in Sonata Admin, integrating seamlessly with external translation tools and ensuring efficient content management.
Adapt the example to the installed version
The Symfony and Sonata examples remain tied to the versions indicated in the article. Symfony 4.x and Symfony 6.2 are no longer maintained: for a current project, use a maintained branch and the documentation that exactly matches your dependencies.
php bin/console about
composer show symfony/framework-bundle sonata-project/admin-bundle
Before resuming the code, check the signatures, services, routes, and templates concerned. Then add a functional test covering permissions, HTTP code, and the visible result.
Reference: maintained Symfony versions.