Skip to content

[Translator] Fall back to the application default locale - #4089

Merged
Kocal merged 1 commit into
symfony:3.xfrom
feymo:feat/translator-default-locale
Oct 18, 2026
Merged

Kocal merged 1 commit into
symfony:3.xfrom
feymo:feat/translator-default-locale

Conversation

@feymo

@feymo feymo commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor
Q A
Bug fix? yes
New feature? yes
Deprecations? no
Documentation? yes
Issues
License MIT

Follow-up of #4076 , which did the same for UX Router.

The translator reads its locale from <html data-symfony-ux-translator-locale> or <html lang>. When both are missing, it falls back to a hard-coded en. The bundle does not dump the application default locale, so the JavaScript side cannot know it. With framework.default_locale: fr and a layout without lang, messages are translated in English.

The dumped index.js file now exports the kernel.default_locale parameter as defaultLocale, and index.d.ts declares it when TypeScript types are dumped. createTranslator() accepts it as a new defaultLocale option:

import { createTranslator } from '@symfony/ux-translator';
import { messages, localeFallbacks, defaultLocale } from '../var/translations/index.js';

export const { trans } = createTranslator({ messages, localeFallbacks, defaultLocale });

The locale is resolved in this order: the locale option, <html data-symfony-ux-translator-locale>, <html lang>, the defaultLocale option, then en. A translator.js file without the option keeps its current behavior, and getDefaultLocale() is unchanged.

default_locale: fr, no lang attribute Before After
getLocale() in the browser en fr
getLocale() without document (SSR) ReferenceError: document is not defined fr

This PR also fixes two bugs on the way:

  • Without document (server-side rendering, tests in Node), createTranslator() threw a ReferenceError. It now skips the page attributes, which is also what makes the new option useful there.
  • Locales with a script and a region were only partly converted, because replace() only replaces the first match. <html lang="zh-Hant-TW"> gave zh_Hant-TW, which matches no catalogue, and an ICU message in zh_Hant_TW threw RangeError: Incorrect locale information provided. Both conversions now use replaceAll().

The Flex recipe needs a new 3.7 version that passes defaultLocale. I will open it on symfony/recipes once this is merged.

@carsonbot carsonbot added Bug Bug Fix Documentation Improvements or additions to documentation Feature New Feature Status: Needs Review Needs to be reviewed labels Oct 13, 2026
@feymo
feymo requested a review from Kocal as a code owner October 13, 2026 08:39

@Kocal Kocal left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

For 3.7

@Kocal Kocal removed the Bug Bug Fix label Oct 13, 2026
@feymo
feymo force-pushed the feat/translator-default-locale branch from 0802560 to 370fad2 Compare October 15, 2026 17:59
@github-actions

Copy link
Copy Markdown
Contributor

📊 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.d.ts 1.87 kB / 669 B 1.91 kB+2% 📈 / 678 B+1% 📈
translator_controller.js 7.39 kB / 2.29 kB 7.54 kB+2% 📈 / 2.33 kB+2% 📈

@carsonbot carsonbot added Status: Reviewed Has been reviewed by a maintainer and removed Status: Needs Review Needs to be reviewed labels Oct 18, 2026
@Kocal

Kocal commented Oct 18, 2026

Copy link
Copy Markdown
Member

Thank you @feymo.

@Kocal
Kocal merged commit a544239 into symfony:3.x Oct 18, 2026
36 of 37 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Documentation Improvements or additions to documentation Feature New Feature Status: Reviewed Has been reviewed by a maintainer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants