symfony / ux

Symfony UX initiative: a JavaScript ecosystem for Symfony
https://ux.symfony.com/
MIT License
858 stars 315 forks source link

[Translator] No translations if detected via HTML lang attribute #2378

Closed aleho closed 5 days ago

aleho commented 1 week ago

Symfony locales are specified with an underscore to split country code from language code, e.g. de_AT.

When using UX Translator it correctly detects the current locale if set via the HTML attribute:

<html lang="{{ app.request.locale }}">
{# <html lang="de_AT"> #}

Unfortunately, according to W3, this format is invalid, as the "subcode" has to be separated by a hyphen.

Using

<html lang="{{ app.request.locale | replace({'_': '-'}) }}">
{# <html lang="de-AT"> #}

doesn't work, because this format is not detected by Translator, resulting in no translations at all (only the string constants are returned).

smnandre commented 1 week ago

You can use the data attribute as shown in the section of the documentation you link ?

aleho commented 1 week ago

Yes, there are other ways of configuring the locale. I'm just saying that Translator has a feature to read html lang="" but that feature requires an invalid value for lang to be fully compatible with Symfony i18n.

In my opinion either the feature should be removed or fixed. As the fix would be trivial (as far as I can see right now), I'd rather see a fix. As it stands now it doesn't break with en but would break with en_US, which was unexpected, at least for me.

smnandre commented 1 week ago

If you want to use the lang attribute of the html tag, and if your locale region and langage code differ, you're right it would currently requires a value not following the w3 spec.

What i understand here is the feature is working for users:

So i doubt it will be "removed"

But it does not "convert" en-US to en_US indeed. So.. would you have some time to open a PR? 😃

(Only constraint: any change should keep previous behaviour working, to not break existing apps)

aleho commented 6 days ago

@smnandre Done!

smnandre commented 5 days ago

Thank you very much @aleho :)