Skip to content

Commit 83ae9e8

Browse files
committed
bug #2390 [Translator] Handle W3C locale format on document element (aleho)
This PR was merged into the 2.x branch. Discussion ---------- [Translator] Handle W3C locale format on document element | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | Issues | Fix #2378 | License | MIT According to [W3C](https://www.w3.org/TR/html401/struct/dirlang.html#h-8.1.1 ) (and [RFC1766](https://www.ietf.org/rfc/rfc1766.txt)) 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. Commits ------- 8eab198 [Translator] Handle W3C locale format on document element
2 parents ed31702 + 8eab198 commit 83ae9e8

File tree

5 files changed

+23
-6
lines changed

5 files changed

+23
-6
lines changed

src/Translator/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## 2.22.0
4+
5+
- Support both the Symfony format (`fr_FR`) and W3C specification (`fr-FR`) for locale subcodes.
6+
37
## 2.20.0
48

59
- Add `throwWhenNotFound` function to configure the behavior when a translation is not found.

src/Translator/assets/dist/translator_controller.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ function setLocale(locale) {
227227
function getLocale() {
228228
return (_locale ||
229229
document.documentElement.getAttribute('data-symfony-ux-translator-locale') ||
230-
document.documentElement.lang ||
230+
(document.documentElement.lang ? document.documentElement.lang.replace('-', '_') : null) ||
231231
'en');
232232
}
233233
function throwWhenNotFound(enabled) {

src/Translator/assets/src/translator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ export function setLocale(locale: LocaleType | null) {
4747
export function getLocale(): LocaleType {
4848
return (
4949
_locale ||
50-
document.documentElement.getAttribute('data-symfony-ux-translator-locale') || // <html data-symfony-ux-translator-locale="en">
51-
document.documentElement.lang || // <html lang="en">
50+
document.documentElement.getAttribute('data-symfony-ux-translator-locale') || // <html data-symfony-ux-translator-locale="en_US">
51+
(document.documentElement.lang ? document.documentElement.lang.replace('-', '_') : null) || // <html lang="en-US">
5252
'en'
5353
);
5454
}

src/Translator/assets/test/translator.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@ describe('Translator', () => {
3535
});
3636
});
3737

38+
describe('getLocale', () => {
39+
test('with subcode', () => {
40+
// allow format according to W3C
41+
document.documentElement.lang = 'de-AT';
42+
expect(getLocale()).toEqual('de_AT');
43+
44+
// or "incorrect" Symfony locale format
45+
document.documentElement.lang = 'de_AT';
46+
expect(getLocale()).toEqual('de_AT');
47+
});
48+
});
49+
3850
describe('setLocale', () => {
3951
test('custom locale', () => {
4052
setLocale('fr');

src/Translator/doc/index.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ Configuring the default locale
9090

9191
By default, the default locale is ``en`` (English) that you can configure through many ways (in order of priority):
9292

93-
#. With ``setLocale('your-locale')`` from ``@symfony/ux-translator`` package
94-
#. Or with ``<html data-symfony-ux-translator-locale="your-locale">`` attribute
95-
#. Or with ``<html lang="your-locale">`` attribute
93+
#. With ``setLocale('de')`` or ``setLocale('de_AT')`` from ``@symfony/ux-translator`` package
94+
#. Or with ``<html data-symfony-ux-translator-locale="{{ app.request.locale }}">`` attribute (e.g., ``de_AT`` or ``de`` using Symfony locale format)
95+
#. Or with ``<html lang="{{ app.request.locale|replace({ '_': '-' }) }}">`` attribute (e.g., ``de-AT`` or ``de`` following the `W3C specification on language codes`_)
9696

9797
Detecting missing translations
9898
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -191,3 +191,4 @@ https://symfony.com/doc/current/contributing/code/bc.html
191191
.. _`the Symfony UX initiative`: https://ux.symfony.com/
192192
.. _StimulusBundle configured in your app: https://symfony.com/bundles/StimulusBundle/current/index.html
193193
.. _`ICU Message Format`: https://symfony.com/doc/current/reference/formats/message_format.html
194+
.. _`W3C specification on language codes`: https://www.w3.org/TR/html401/struct/dirlang.html#h-8.1.1

0 commit comments

Comments
 (0)