0

someone has been able to pass the language by parameters to a ligthing component as visualforce does.

<apex: page language = "en">

I have a requirement where the user can select different languages and the format changes I want to do it in ligthing.

I think I'm going to have to do it with Visualforce and vuejs

0

There is an answer in this post, but it is not a full answer.

Full answer is - you cannot do the same thing in VisualForce as of today.

I had the same requirement and I found a solution based on labels with their translations.

First you need to set all the label data and it's translations Then, when you want to change the language - you should change the salesforce user language from apex:

// change the user's language to the local given (can be 'en_US' or other local)
private static void changeUserLanguage(String locale) {

    // get the current user Id
    String userId = UserInfo.getUserId();

    // get the user object
    User currentUser = [SELECT Id, LanguageLocaleKey FROM User WHERE Id = :userId];

    // change the user local and update the record
    currentUser.LanguageLocaleKey = locale;
    update currentUser;
}

After you change the current user local, all field labels and custom labels get their translated values in the component, so you need to reload your component so it loads the translated data.

1
  • I would not work if community is public because there would be single user...changing user locale will change experience of other user who is browsing! – Ayub Jun 13 '19 at 11:56
0

I was facing the same issue.The detailed solution is given below.Using some code as given by @Itai Shmida

Controller:

    doInit : function (cmp, event, helper) {
    var language = $A.get("$Locale.language");//for setting the language select value on reload
    var action = cmp.get("c.siteLanguageSetting");
            action.setParams({
                language : language
            });
            action.setCallback(this, function (response) {
                var state = response.getState();
                window.setTimeout(
                    $A.getCallback( function() {
                        // Now set our preferred value
                        cmp.find("languageList").set("v.value", setLabel);
                    }));
            });
            $A.enqueueAction(action); 
},
reloadPageForLanguage : function (cmp, event, helper) {
        var language ='en';
        language = cmp.get("v.ls");
        var action = cmp.get("c.siteLanguageSetting");
        action.setParams({
            language : language
        });
        action.setCallback(this, function (response) {
            var state = response.getState();
            //cmp.set("v.submitted", true);
        });
        $A.enqueueAction(action);
        var url = window.location.href.split('?')[0] + '?language=' + language;
        window.location.href = url;
    } 

Component:

<aura:attribute name="ls" type="String"/>
<aura:attribute name="languageOptions" type="List" default="[
    {'label': 'English (US)', 'value': 'en_US'},
    {'label': 'Español', 'value': 'es'},
    {'label': '中文 (CN)', 'value': 'zh_CN'},
    {'label': '日本語', 'value': 'ja'},
    {'label': 'français', 'value': 'fr'},
    {'label': '한국어', 'value': 'ko'},
    ]"/>
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>

    <aura:if isTrue="{!!v.submitted}">
        <lightning:layout multipleRows="true">
            <lightning:layoutItem padding="around-small" size="12" flexibility="auto">
                <lightning:select name="languageSelection" label="" aura:id="languageList" value="{!v.ls}"
                                  onchange="{!c.reloadPageForLanguage}" class="select-auto-width">
                    <aura:iteration items="{!v.languageOptions}" var="item">
                        <option text="{!item.label}" value="{!item.value}"/>
                    </aura:iteration>
                </lightning:select>
            </lightning:layoutItem>

The language is selected in picklist and the onchange call initiates the language reload.

apex:

@AuraEnabled
    public static void siteLanguageSetting(String language){
    system.debug('Lang'+language) ;   
        // get the current user Id
    String userId = UserInfo.getUserId();

    // get the user object
    User currentUser = [SELECT Id, LanguageLocaleKey FROM User WHERE Id = :userId];

    // change the user local and update the record
    currentUser.LanguageLocaleKey = language;
    update currentUser;
    }

This should do the trick for you.Please share if anyone comes up simple ways of doing this.

0

I have developed the solution with visualforce and angular I think that changing the user's language is creative but I do not want to enable permissions on the user.

I built the components in angular as I would in lightning to migrate it in the future.

Thanks for your comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.