Translation guidance

We are using Pimcore v5.4

Our use case is that we have a ~500 documents that will need to be translated in 15+ languages. The content is stored in editable input fields and Wysiwyg fields. Instead of creating duplicate documents, and ending up with 15x500 languages, I would like the translations to be based on user locale using the same document. The Pimcore suggestion to replicate a document per locale will result in redundancies i’d like to avoid.

So… I tried to follow Symfony translation tutorial to get the Translator sample working, but not very successful. I am open to suggestions on a better way to do this, but my thought are as follows:

  • Get JSON view of the document to extract all the keys (or use the Translator Bundle if possible)
  • Get the missing keys to a translation thirdparty, our content is heavy legal/compliance type content, and we would like to setup an export/import process as opposed to directly editing/entering translations inside pimcore documents
  • Use Symfony Translator component to render the appropriate translation at run-time based on locale for the same document

Configuration

#/app/config/config.yml
framework:
translator:
    paths:
        - "%kernel.root_dir%/Resources/translations"
    fallback: [en]

#----
#/app/Resources/translations/messages.es.yml (also have messages.es.xlf, just in case)

'Symfony is great': 'Symfony es mui bueno'

Attempt 1

Controller-

$request->setLocale('es');
$this->view->translated = $this->get('translator')->trans('Symfony is great');
$this->view->locale = $request->getLocale();

View - [Cache cleeared]: $locale is 'es' yet the $translated value is still in English.

Attempt 2

Here is the sample script i tried to run after configuration, it just emits “Done”, no errors…

> php bin/console pimcore:run-script /tmp/translation.php
> Running script /tmp/translation.php

// -- script /tmp/translation.php -- 
$translator = new \Symfony\Component\Translation\Translator('en_GB');
$translator->addLoader('array', new \Symfony\Component\Translation\Loader\ArrayLoader());
$translator->addResource('array', [
    'sample' => 'The English translation goes here...',
], 'en_GB');
echo $translator->trans('sample');
> <blank line>
> Done

The idea and benefit of using documents is to give the users full flexibility of the layout of the content in different languages. So the content could look completely different in one language version than in another one. If you would like to use a fixed, predefined layout you could consider using data objects.

Also I I would like to point you to following features:

https://pimcore.com/docs/6.x/Development_Documentation/Multi_Language_i18n/Admin_Translations.html

https://pimcore.com/docs/6.x/Development_Documentation/Multi_Language_i18n/Shared_Translations.html

@ckemptner - thank you for your guidance… definitely something we will look into, but for now, any thoughts on my original challenge on getting a basic example of Translator working in Pimcore?

Found the problem, in controller i should have done:
$this->get('translator')->setLocale('es');

using $request->setLocale('es') was not letting the document know which language to pick.

You need to remember that the translation term in Symfony is merely a placeholder for all the translations you need to display on different locales.

So, in your example:

$this->view->translated = $this->get('translator')->trans('Symfony is great');

the sentence ‘Symfony is great’ should be replaced with a generic placeholder. Basically, something like this:

$this->view->translated = $this->get('pimcore.translator')->trans('symfony.greatness.placeholder');

Then, try to input the actual sentence into the Shared Translations of your Pimcore installation, and try to display in the locale you input the sentence for.

This way you’ll get a much better understanding of the Symfony translation engine.

You can also add variables to the translations, for example, for symfony.greatness.placeholder, the translation could be:

%framework% is great!

And the actual translation call would look like:

$this->view->translated = $this->get('translator')->trans('symfony.greatness.placeholder', ['%framework%' => 'Symfony']);

Hope this helps out a bit.