0

I have a visualforce page with a couple of Required Fields. The fields are made Required on the front-end by setting the Required attribute to True. There is a picklist on the top-right corner of the page that has multiple language options. Whenever I select a value from the picklist, it fires up validations on that page and doesn't update the picklist value, hence not translating the page. It is probably due to Apex:actionsupport but I'm not sure.

I've tried using immediate="True" on actionsupport, but that doesn't seem to work.

Is there another way to create this kind of functionality? Using Javascript/Jquery or even through the current method?

Apex Function:

public PageReference translate(){
        webformObject.put('Language__c', language);
        update webformObject;
        PageReference pageRef = new PageReference(urlToCurrentPage);
        pageRef.getParameters().put('lang',language);
        pageRef.setRedirect(true);
        return pageRef;
    }

Picklist Markup:

<apex:selectList id="langList" value="{!language}" size="1">
        <apex:selectoptions value="{!AvailableLanguages}"/>
        <apex:actionsupport action="{!translate}" event="onchange"/>
</apex:selectList>

owner info

  • 1
    Can you post all the visual force. Is the language selection in the same form as all other fields? – Zach Hutchins Jan 3 at 14:03
  • The entire visualforce would be quite long. But yeah, the entire form is under 1 apex:form tag – vedant gupta Jan 3 at 15:06
0

I also was unable to make this work, from the code that was provided as my base. So I went a different route that does a decent job and is supportable.


<apex:page controller="test" 
               docType="html-5.0" 
               lightningStylesheets="true" 
               language="{!language}">
        <apex:form>
            <select id="languageSelection" onchange="changeLanguage()">
                <apex:repeat var="langCode" value="{!languages}">
                    <option value="{!langCode.Label}">{!langCode.Value}</option>
                </apex:repeat>
            </select>
            <apex:pageBlock >    
                <apex:pageBlockSection title="My Content Section" columns="2">
                    <apex:inputField value="{!something.name}" required="true"/>
                </apex:pageBlockSection>
                <apex:pageBlockButtons >
                    <apex:commandButton action="{!save}" value="Save New Account Name"/>
                </apex:pageBlockButtons>
             </apex:pageBlock>
        </apex:form>
        <script>
        function changeLanguage(){
                window.location.href = 
                    '{!JSENCODE($Site.BaseUrl)}?lang='+document.getElementById("languageSelection").value;
        }

        </script>
    </apex:page>

public class test {
    public String language {get;set;}
    public Account something { get; set; }
    public List<SelectOption> languages {get;set;}
    public PageReference save(){
        return null;
    }

    public testPdfController(){
        this.languages = new List<SelectOption>();
        this.languages.add(new SelectOption('French','fr'));
        this.languages.add(new SelectOption('English','en'));
        this.languages.add(new SelectOption('Spanish','es'));
        this.language = ApexPages.currentPage().getParameters().get('lang');
        System.debug(ApexPages.currentPage().getParameters());
        this.something = [select id,name from account limit 1];      
        if(this.language == null){
            this.language = 'en';
        }
        setDefaultSelectLanguage();
    }

    private void setDefaultSelectLanguage(){
        for(integer i = 0; i < languages.size(); i++){
            if(languages.get(i).getLabel() == this.language){
                SelectOption defaultOption = languages.remove(i);
                List<SelectOption> options = new List<SelectOption>();
                options.add(defaultOption);
                options.addAll(this.languages);
                this.languages = options;
                return;
            }
        }
    }
}
| improve this answer | |
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - From Review – identigral Jan 3 at 15:43
  • Thank you for hiding the answer, the answer was men't to be a comment. I have changed the answer to be an actual answer. – Zach Hutchins Jan 7 at 23:24

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.