laravel / lumen-framework

The Laravel Lumen Framework.
https://lumen.laravel.com
MIT License
1.48k stars 416 forks source link

Translate breaks with arrays as using strtr instead of str_replace #1207

Closed Gummby closed 2 years ago

Gummby commented 2 years ago

Description:

Hi there, I am not sure if this a bug or is not intended functionality. But in illuminate/translation/Translator.php translate's makeReplacements method is now using strtr. If we use a nested array strtr will fail on the type check. It previously used str_replace which would allow $line to be an array or string. I'm unsure if this was ever meant to be used like this. How ever if it was it would be great if this could be supported still.

Steps To Reproduce:

  1. make a translation file with nested values.
<?php

// resources/lang/en/messages.php

return [
    'welcome' => 'Welcome to our application!',
    'buttons' => [
          'close' => ':reject',
          'submit' => ':confirm',
    ],
];
  1. call translate with changes

trans('messages', ['confirm' => 'save'])

change occurred here:

https://github.com/illuminate/translation/commit/019c83d17b6311be5e52ab99487804d210110d9d

driesvints commented 2 years ago

I think you're missing the key to the actual line here?

trans('messages.buttons.submit', ['confirm' => 'save'])

The usage you're showing has never been supported I believe.

Gummby commented 2 years ago

Hey, sorry that was just an example. We have trans files full with multiple key values. see: https://laravel.com/docs/8.x/localization#using-short-keys

so doing trans('messages', ['reject' => 'nah', 'confirm' => 'yeah']));

on

<?php

// messages

return [
    'welcome' => 'Welcome to our application! :reject',
    'buttons' => [
        'close' => ':reject',
        'submit' => ':confirm',
    ],
];

will return

Array
(
    [welcome] => Welcome to our application! nah
    [buttons] => Array
        (
            [close] => nah
            [submit] => yeah
        )

)
driesvints commented 2 years ago

I'm sorry but I believe it was never intended to be used that way. You'd always specify the full path to the translation key.

Gummby commented 2 years ago

I'm sorry but I believe it was never intended to be used that way. You'd always specify the full path to the translation key.

all good, thought that might be the case. Thank you