Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Translator] Handle W3C locale format on document element #2390

Merged
merged 1 commit into from
Nov 20, 2024

Conversation

aleho
Copy link
Contributor

@aleho aleho commented Nov 19, 2024

Q A
Bug fix? yes
New feature? no
Issues Fix #2378
License MIT

According to W3C (and RFC1766) the valid format for language codes is "primary-code"-"subcode", but Symfony expects underscores as separator, resulting in broken translations.

This changes language codes specified in the correct format for HTML to the Symfony format when reading the "lang" attribute.

@aleho aleho requested a review from Kocal as a code owner November 19, 2024 13:55
@carsonbot carsonbot added Status: Needs Review Needs to be reviewed Bug Bug Fix labels Nov 19, 2024
Copy link

github-actions bot commented Nov 19, 2024

📊 Packages dist files size difference

Thanks for the PR! Here is the difference in size of the packages dist files between the base branch and the PR.
Please review the changes and make sure they are expected.

FileBefore (Size / Gzip)After (Size / Gzip)
Translator
translator_controller.js 9.17 kB / 2.27 kB 9.23 kB+1% 📈 / 2.29 kB+1% 📈

Copy link
Member

@Kocal Kocal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @aleho, and thank you for taking the subject!

This is a direct BC break for people using <html lang="{{ app.request.locale }}"> with a _ inside the locale, but the component is still experimental, good news! It means that we can break things :)

However, it miss the CHANGELOG update, and we can improve the documentation a little bit.

Also, can you update the test getLocale / default locale to ensure that expect(getLocale()).toEqual('fr_FR') when document.documentElement.lang = 'fr-FR';?

Thanks!

src/Translator/doc/index.rst Outdated Show resolved Hide resolved
@carsonbot carsonbot added Status: Needs Work Additional work is needed and removed Status: Needs Review Needs to be reviewed labels Nov 19, 2024
@carsonbot carsonbot added Status: Needs Review Needs to be reviewed and removed Status: Needs Work Additional work is needed labels Nov 20, 2024
@aleho
Copy link
Contributor Author

aleho commented Nov 20, 2024

This is a direct BC break for people using <html lang="{{ app.request.locale }}"> with a _ inside the locale

Is it? Did I miss something? In my tests it didn't matter whether I used de, de_DE or de-DE, all work with this PR. (See new tests.)

What doesn't work is using en(-|_)US if there's no en_US configured in Symfony. Neither does de(-|_)AT if only de and de_DE are configured. (But that's unrelated to my PR, it wouldn't fallback to the available primary code before either.)

However, it miss the CHANGELOG update

If this isn't a BC break, does it still need a changelog entry? Or can this be a bug fix?

Now with tests. Sorry about that, completely forgot to commit them.

Comment on lines 94 to 95
#. Or with ``<html data-symfony-ux-translator-locale="{{ app.request.locale }}">`` attribute (e.g., `de_AT` or `de` for Symfony locale format)
#. Or with ``<html lang="{{ app.request.locale|replace({ '_': '-' }) }}">`` attribute (e.g., `de-AT` or `de` for W3C spec compliant format)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#. Or with ``<html data-symfony-ux-translator-locale="{{ app.request.locale }}">`` attribute (e.g., `de_AT` or `de` for Symfony locale format)
#. Or with ``<html lang="{{ app.request.locale|replace({ '_': '-' }) }}">`` attribute (e.g., `de-AT` or `de` for W3C spec compliant format)
#. Or with ``<html data-symfony-ux-translator-locale="{{ app.request.locale }}">`` attribute (e.g., ``de_AT`` or ``de`` using Symfony locale format)
#. Or with ``<html lang="{{ app.request.locale|replace({'_': '-'}) }}">`` attribute (e.g., ``de`` or ``de-AT`` or following HTML specifications)

wdyt ?

@smnandre
Copy link
Member

If this isn't a BC break, does it still need a changelog entry? Or can this be a bug fix?

Almost a feature here :)

# CHANGELOG

+ ## 2.22.0
+ 
+ -  Support both "fr-FR" and "fr_FR" format in html lang attribute

## 2.20.0

Something like this maybe ? (wording to illustrate only 😅 )

Copy link
Member

@Kocal Kocal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to me after applying @smnandre suggestion

@carsonbot carsonbot added Status: Reviewed Has been reviewed by a maintainer and removed Status: Needs Review Needs to be reviewed labels Nov 20, 2024
@aleho aleho changed the title [Translator] Handle W3 locale format on document element [Translator] Handle W3C locale format on document element Nov 20, 2024
According to W3C[1] (and RFC1766) the valid format for language codes is
"primary-code"-"subcode", but Symfony expects underscores as separator,
resulting in broken translations.

This changes language codes specified in the correct format for HTML to
the Symfony format when reading the "lang" attribute.

Fixes symfony#2378

[1] https://www.w3.org/TR/html401/struct/dirlang.html#h-8.1.1
@aleho
Copy link
Contributor Author

aleho commented Nov 20, 2024

@smnandre @Kocal Thanks!

@Kocal
Copy link
Member

Kocal commented Nov 20, 2024

Thank you @aleho.

@Kocal Kocal merged commit 83ae9e8 into symfony:2.x Nov 20, 2024
61 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Bug Fix DX Status: Reviewed Has been reviewed by a maintainer Translator
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Translator] No translations if detected via HTML lang attribute
4 participants