doubu7425 2015-11-24 15:50
浏览 11
已采纳

StofDoctrineExtensionsBundle可翻译

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):

enter image description here

Here is the table ext_translations (ext_translations table):

enter image description here

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?

  • 写回答

1条回答 默认 最新

  • douyao4632 2015-11-24 16:25
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思