[Bug]: Invalid translations for numeric translation keys
Pimcore version
11.2.2
Steps to reproduce
Go to Admin Translation and add a translation with key=123 and text='any text'. Open the admin panel. Then, open the debug console and call the JS function t(123). You will get the result "123" instead of "any text". However, calling t(0) returns "any text". This is just one of many ways to reproduce the issue, as the problem is more general.
In the jsonTranslationsSystemAction method of the Pimcore\Bundle\AdminBundle\Controller\Admin\MiscController, there is a line:
$translations = array_merge($translations, $translator->getCatalogue($language)->all($domain));
This line is the root cause of the issue. The problem arises because array_merge behaves differently for indexed arrays (where keys are numbers, e.g. [1 => 'value1', 2 => 'value2', 3 => 'value3']) and associative arrays (e.g. ['a' => 'value1', 'b' => 'value2', 'c' => 'value3']).
For indexed arrays, numeric keys are reassigned to maintain a continuous sequence (0, 1, 2, 3...). This results in unexpected behavior when merging arrays with numeric keys.
For example, the following code:
array_merge(['key1' => 'value1', 42 => '42'], ['key2' => 'value2', 51 => '51']);
Produces:
['key1' => 'value1', 0 => '42', 'key2' => 'value2', 1 => '51']
Instead of the expected:
['key1' => 'value1', 42 => '42', 'key2' => 'value2', 51 => '51']
Actual Behavior
Missing or invalid translation is provided.
Expected Behavior
Valid translation should be provided.
To get ['key1' => 'value1', 42 => '42', 'key2' => 'value2', 51 => '51'] you would need to use array_replace() instead of aray_merge().
They behave the same for string keys, but aray_merge() resets and keeps all items with numeric keys, while array_replace() keeps the numeric keys and the values from the second argument.
Thanks a lot for reporting the issue. We did not consider the issue as "Pimcore:Priority", "Pimcore:ToDo" or "Pimcore:Backlog", so we're not going to work on that anytime soon. Please create a pull request to fix the issue if this is a bug report. We'll then review it as quickly as possible. If you're interested in contributing a feature, please contact us first here before creating a pull request. We'll then decide whether we'd accept it or not. Thanks for your understanding.
PR provided with: https://github.com/pimcore/admin-ui-classic-bundle/issues/1002
fixed via https://github.com/pimcore/admin-ui-classic-bundle/pull/968