Skip to content

Commit 3e4e76b

Browse files
committed
[Translator] Handle W3 locale format on document element
According to W3[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 #2378 [1] https://www.w3.org/TR/html401/struct/dirlang.html#h-8.1.1
1 parent eb8dd83 commit 3e4e76b

File tree

4 files changed

+17
-5
lines changed

4 files changed

+17
-5
lines changed

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 W3
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ Configuring the default locale
9191
By default, the default locale is ``en`` (English) that you can configure through many ways (in order of priority):
9292

9393
#. 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
94+
#. Or with ``<html data-symfony-ux-translator-locale="your-locale">`` attribute (e.g., `en_US`)
95+
#. Or with ``<html lang="your-locale">`` attribute (e.g., `en-US`)
9696

9797
Detecting missing translations
9898
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)