function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Changing user language from public facing vf page

Is it possible to change the current system language (Language Settings) from an Apex controller of a public facing vf page? We are creating a configuration menu on a vf page where the user can change the current default system language.

There is a drop down for languages in the vf page from where the user selects his language. I looked into LanguageLocaleKey and I was able to change the current language by querying user and setting different language.


This is sample code i created as proof of concept:


<apex:page controller="LanguageChangeTestController">
  Current system language : <apex:outputText value="{!currentLanguage}"/>
  <apex:form >
      <apex:commandButton value="Change language to Portuguese" action="{!changeLanguage}"/>
      <apex:commandButton value="Go back to US English" action="{!goBackToUSEng}"/>



public with sharing class LanguageChangeTestController {

    public String currentLanguage {get; set;}
    public LanguageChangeTestController() {
        currentLanguage =  UserInfo.getLanguage();
    public void changeLanguage() {
        //get current user
        String currentuserId = UserInfo.getUserId();
        User currentUserInfo = [select LanguageLocaleKey from User where Id = :UserInfo.getUserId()];
        currentUserInfo.LanguageLocaleKey = 'pt_BR';
        update currentUserInfo;
    public void goBackToUSEng() {
        User currentUserInfo = [select LanguageLocaleKey from User where Id = :UserInfo.getUserId()];
        currentUserInfo.LanguageLocaleKey = 'en_US';
        update currentUserInfo;


However I am not sure if I can access user id from the public facing vf page.


Any idea on how this will work for a public facing vf page?


Well, typically in case of a public page, the localizations usually are at a view level only i.e. the data is presented in the localized form and we do not change the language values with the user record as all the request which reaches salesforce from the public site is through a "Guest User" for which you set the profile while setting up the public site. So unless the user record information is explicitly sent from from public page to salesforce your controllers would have little to do.


However, you can use the ISO codes in your language drop down and also the client's browser language preference along with translation workbench to set the current language of the display page on the fly.This you can do in the <apex:page> tag in the language attribute, it accepts 2 character ISO Codes ( you can also read the same from the browser's setting through a simple javascript}. So, when the user change the language value the page would get localized contents from workbench and show it. 


We did a very simillar implementation and it works pretty well.