QuestionQ29

Working with Databases and EAV

An Adobe Commerce developer works on a Magento 2 instance containing B2C and B2B websites, each with three store views for English-, Welsh-, and French-language users. The developer must add a link between the B2C and B2B websites through a generic link template used throughout the sites, but wants those links to appear in English regardless of the store view.

The developer creates a custom block for this template and, before rendering, sets the translation locale and starts environment emulation with the following code:

Question Image

The template text is nevertheless translated into each store’s language. Why does this happen?

  • A startEnvironmentEmulation() sets and locks the locale by using the setLocale() optional second $lock parameter, i.e. setLocale($newLocaleCode, true), to override and lock the locale of the emulated store. If this is set and locked initially then the environment emulation will not be able to override this.
  • B startEnvironmentEmulation() resets the translation locale to the one of the emulated stores, which overrides the locale the developer has set when the order of setLocale and startEnvironmentEmulstion is used as displayed above.
  • C setLocale() does not change translation locale after it has been initially set, the $this>_translate->emulate($newLocaleCode) method exists to temporarily modify this by pushing the new locale to the top of the current emulatedLocales stack.
Explanation

Starting store environment emulation applies the emulated store’s configured locale to the translation component. That locale assignment overwrites the locale set beforehand, so template translations use the language of the emulated store view.

Learn more

Community Discussion

No comments yet. Be the first to start the discussion!