Problem/Motivation
The select element type's #options field does not play well with translations when they contain optgroups and subsequently silently fails to translate some of the options.
Steps to reproduce
Using the following YAML for the webform source :
optgroup_select:
'#type': select
'#title': 'Example select with optgroup'
'#options':
Fruits:
- Banana
- Strawberry
- Mango
Vegetables:
3: Turnip
4: Eggplant
5: Carrot
And translating it to this (in french in my case), either on the translation UI or in a config directory:
Fruits:
- Banane
- Fraise
- Mangue
Légumes:
3: Navet
4: Aubergine
5: Carotte
Results in the (master) english version:

And the following french version:

Note that the fruits are successfully translated, but not the vegetables.
I think it may be linked to how fruits is an array while vegetables is an object, because changing the fruits IDs to a text value or other numbers makes them untranslated as well.
Additionally, the translation UI shows the same thing as the above screenshot while the config files contain the correct data.

I have attached the configuration files below.
Proposed resolution
We should be able to edit the select's #options fields reliably, even if it contain optgroups.
Comments
Comment #2
maxime-adagpComment #3
avpadernoComment #4
maxime-adagpAfter some investigation, optgroups are not properly taken into account in
src/Utility/WebformElementHelper.php, in eithermerge()orapplyTranslation().In this case, we are iterating over the elements inside
#options, and we get the following arrays:FruitsandVegetablesin the "master" versionFruitsandLégumesin the "translated" versionThis leads the loop to skip the vegetables because the "master" version does not contain the
Légumesarray.To solve this, a solution would be to delete the master
#optionsarrays, and simply replace them with the translated#optionsarrays.I haven't spent time hacking on this but this is the best solution I can think of. Any thoughts?
Comment #5
maxime-adagpHere's a first stab, it does fix the issue on this simple form but it doesn't on a bigger form, I'll explore a bit more.
Comment #6
maxime-adagpComment #7
maxime-adagpThis should fix the regression
Comment #8
maxime-adagpComment #9
maxime-adagpHere is a cleaner patch.
Comment #10
maxime-adagpThis seems to fit for my use case. I can't run the tests on my own so I hope they will run now.
The patch does not include tests as I haven't wrapped my head around how this is done (I'm relatively new to Drupal).
Suggestions are welcome!
Comment #11
maxime-adagpI just noticed I mislabeled this issue, sorry for the inconvenience.
Comment #12
jrockowitz commentedI was able to replicate the issue, and the patch fixes the problem.
I might try to clean up the duplicate merge logic.
Comment #13
jrockowitz commentedComment #14
jrockowitz commented