I'm building Symfony 2 app with two languages i have implement the Translatable from StofDoctrineExtensionsBundle. The Translatable make other table ext_translations and store the translations.
Here is the table which i will use to translate (Objects table):
Here is the table ext_translations (ext_translations table):
When i run the code to save the translation for some reason in the ext_translations table.
$entity = $em->getRepository('ObjectsBundle:Object')->find(1);
$entity->setTitle('my title in ru');
$entity->setTranslatableLocale('ru'); // change locale
$em->persist($entity);
$em->flush();
On creation it write the title in the object table but later on update it is updating only the ext_translations table.
How is handle the update and creation in symfony 2 with two languages?
Do I need to make a copy of those inputs and store them in one time submit. For example in the Object table i have title do I need to have :
<input type="text" name="title_bg">
<input type="text" name="title_en">
And when they are submitted change the parameter local:
$entity->setTranslatableLocale('ru'); -- or what lang parameter we have
And then submit whit the upper code. What is the best solution for this kind of applications?
The "non translation table" texts (the ones in the proper entity) are the ones from your default locale. So usually it is "en" (or more properly is should be en_US or en_GB).
if you want to have your translation to be russian by default ("ru" or better "ru_RU"), you should set your default locale as that.
The translations (and entries to your ext_translations table) are the entries for the non-default-locale translations.
To set your default locale, go to the parameters.yml.dist and set
parameters:
[...]
locale: de
Run php composer.phar install
to transfer it into your parameters.yml. You can of course edit it directly in the parameters.yml but then it might be overwritten again after a composer update/install
edit (for the comment): If you have problems with CRUD (or most likely the U), you always have to make sure that you have the proper entity loaded. The translatable extension always relies on the locale you set inside the entity. To load the proper entity with the text you want to edit, you have to do the following on load
$entity = $em->getRepository('ObjectsBundle:Object')->find(1);
$entity->setTranslatableLocale('ru'); // change locale
$em->refresh($entity);
This you would do if you wanted to edit your non-default-language-texts. While saving you still have to make sure to properly set the locale in your entity as well (as mentioned by you above).
edit #2: https://gist.github.com/Koalabaerchen/8ed60894cdbbdcd42f3c This is an example of a _locale parameter set inside the session of a user (if not existant). Tested with Symfony 2.7.7 and below
If you want to switch the _locale of the user session (to get other translations) you simply have to create a controller action that sets the _locale session attribute so the other language gets loaded "magically" by the translatable bundle.