We have a requirement to implement multilingual community (English and French) in LWR for authenticated users.

  1. When user logs in then content should be displayed based on locale and language preference set on user record. This works fine.
  2. User should be able to switch language when logged in via Language selector. This also works fine. But this should only be valid for the session of user. If user logs out and logs back in again then the content should be loaded with language preference from User object.

But since user has switched the language using language selector, every time the website is loading in the language selected. In Language selector documentation, it has been mentioned that For authenticated users on private pages, the site typically appears in their profile language automatically. But when users have credentials and they’re not yet logged in, they’re treated like a guest user. So if the user selects a language on a public page that contains a Language Selector component—let’s say the Home page—that language choice persists after the user logs in. Similarly, if a private page contains a custom language selector, the user's selection overrides their profile language. over here https://help.salesforce.com/s/articleView?id=sf.rss_language_picker.htm&type=5

Salesforce is adding a cookie in browser PreferredLanguage<18-char SiteID> for example, PreferredLanguage0DMxx0000004C93GAE based on which salesforce is detecting the language everytime user visits the community. Documentation is available in https://help.salesforce.com/s/articleView?id=sf.networks_cookies.htm&type=5

Salesforce has provided in documentation that we can raise a case to support to stop automatic language detection, but there is nothing much mentioned what impact it will have and how the language will be detected in that case. https://developer.salesforce.com/docs/atlas.en-us.exp_cloud_lwr.meta/exp_cloud_lwr/multilingual_lwr_automatic_language_detection.htm

I have tried updating the cookie to the value selected in user's locale and reload the page once user will log in but I am unable to access this cookie in any of the custom LWC.

I tried to access this cookie in head markup of community, where I was able to fetch the cookie value but I am a rookie in LWR implementation so do not know how to get user's locale in head markup.

What is the best possible solution to achieve this?

1 Answer 1

var myDate = new Date();
myDate.setMonth(myDate.getMonth() + 12);

document.cookie = "PreferredLanguage0DMxx0000004C93GAE="+this.selectedLang+";domain=REPLACE WITH DOMAIN NAME;path=/;expires=" + myDate;

Tested and confirmed working in a site with 6 languages active.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .