New in the web SDK release that includes language support.
Signature
Parameters
Behavior
- The SDK keeps the language in the browser’s
localStorageuntil you callclearLanguage(), so it survives page reloads and laterconfigure()calls. WherelocalStorageis unavailable it falls back tosessionStorage, then to memory for the current page. Encore never stores it on its servers. - It is a preference of the browser, not user state, so
reset()and logout leave it in place. - When the language changes, the SDK drops its cached copy and offers and fetches its configuration again right away. The web SDK does not load offers ahead of time, so they load in the new language on the next
show(). - Setting the language already in effect does nothing.
- An invalid tag, such as
pt_BRorenglish, is ignored with a console warning, whatever your log level.setLanguage()never throws, and the language already in effect stays. - The language is served only when your app has it enabled. Otherwise Encore falls back to the browser languages, then your app’s default language. See Localization.
- The browser language is still sent as
Accept-Language. This call adds the override that outranks it. - Call it after
configure(). Beforeconfigure()the call does nothing, andconfigure()takes no language.
Replaces the language attribute
Setting the language through the language attribute is deprecated, and it is removed at the next major release. It still works: setUserAttributes() and identify() take language out of the attributes, call setLanguage() with it, and log one warning per page load. identify() does this after it has switched to the new user, so copy is fetched again for that user. The language is no longer returned by getUserAttributes().
A language saved through the attribute on an earlier release moves into the override the first time configure() runs, and is removed from the saved attributes. A language you already set with setLanguage() wins, and a saved value that is not a valid tag is dropped. Unlike setLanguage(), the attribute and the carry-over still accept a locale identifier and turn it into a tag, so pt_BR becomes pt-BR and de_DE@currency=EUR becomes de-DE. See the deprecated attribute. A language set through the attribute is stored as the same override, so it also survives reset().
Usage
Call it from your app’s own language picker, and callclearLanguage() when the user picks “System default”:
Related
- clearLanguage(): go back to the browser language
- setUserAttributes(): the deprecated
languageattribute - Localization: how Encore picks the language it serves