Form Object is reordered at translated cost

I have an entity category that has code. This code is internal, and we use it for each language. For example, imagine the following:

Categories: ---- id:1 Code: "Bread" ---- id:2 Code: "Butter" 

I have a form with a form field entity. I want to order it using the translated shortcut.

In English, for example, it will display

 Bread Butter 

But, for example, in French the order is different

 Beurre (butter) Pain (bread) 

Therefore, I cannot use the orderBy of the entity field.

I have a manual solution, very dirty . I use the select box with label translation

 $categories_translated =array(); $categories= $this->em->getRepository('MyRepo')->findAll(); foreach($categories as $category){ $categories_translated[$category->getId()]= $this->translator->trans($category); } asort($categories_translated);//sorted //then later $builder->add('category','choice',array( 'choices' => $choices_technologies) ) 

Do you have the right way to do this?

+4
source share
2 answers

Your path is the best (and, I think, the only) way to handle this problem with file translations. There are indeed more opportunities to solve problems such as database translations. For example: https://github.com/l3pp4rd/DoctrineExtensions/blob/master/doc/symfony2.md

0
source

Are the codes static? those. test baked dough (in any language) is always id = 1 in your data dictionaries, then sorting is in order. Hypothetically, if you have a table as follows:

 category - catid int auto_increment, - locale varchar(5), - itemid int, - text varchar(100) 

You can choose to access categories sorted by text .
If you just rasterize according to the corresponding languages ​​and objects, you get global identifiers for your categories.

You might want to use the best i18n engine, but that would be very simple for static categories.

0
source

Source: https://habr.com/ru/post/1498015/


All Articles