0

I have created a custom picklist field, and have added values for different languages using translation workbench. We have a requirement to display the field on a VF page for a specified language. We also have a requirement to use VF dynamic components. When I use the "language" attribute directly on the VF page, this works perfectly.

VF Page:

<apex:page controller="setranstest" language="fr" showHeader="false" sidebar="false">
 <apex:outputPanel >
 <apex:dynamicComponent componentValue="{!DynamicText}" invokeAfterAction="true"/>
 </apex:outputPanel>
</apex:page>

Apex Class:

public class setranstest {
    public Case casex {get;set;}
    public setranstest() {
        casex = [select Id,toLabel(CEC_Case_Category__c) from Case where Id = '500Dh00000Db4xMIAR'];
    }
    public Component.Apex.OutputText getDynamicText() {
        Component.Apex.OutputText outx = new Component.Apex.OutputText();    
        outx.expressions.value = 'The category is:{!casex.CEC_Case_Category__c}';
        outx.escape = false;
        return outx;   
    } 
}

Output:

The category is:Bonjour

Now we then try to make the language dynamic, based on a variable in the Apex controller.

VF Page:

<apex:page controller="setranstest" language="{!specifiedLanguage}" showHeader="false" sidebar="false">
 <apex:outputPanel >
 <apex:dynamicComponent componentValue="{!DynamicText}" invokeAfterAction="true"/>
 </apex:outputPanel> 
</apex:page>

Apex Class:

public class setranstest {
    public Case casex {get;set;}
    public string specifiedLanguage {get;set;}
    public setranstest() {
        specifiedLanguage = 'fr';
        casex = [select Id,toLabel(CEC_Case_Category__c) from Case where Id = '500Dh00000Db4xMIAR'];
    }
    public Component.Apex.OutputText getDynamicText() {
        Component.Apex.OutputText outx = new Component.Apex.OutputText();    
        outx.expressions.value = 'The category is:{!casex.CEC_Case_Category__c}';
        outx.escape = false;
        return outx;   
    } 
}

Output:

The category is:Hello

So the question is, why the discrepancy? Why does the picklist value get translated correctly when the language is hard coded, but not get translated correctly when we try and do it dynamically? This looks like a SF bug, and according to this post, it's been around for at least 11 years.

1 Answer 1

1

So for some reason, if I query the case in the getter method getDynamicText instead of in the constructor, it works correctly with the dynamic language. I'm guessing it has something to do with the exact order under the hood that the VF engine is using i.e if the language is already set when running the query, e.g by having it hard coded in the page attribute, it uses the language; if the language is dynamically set in the same method as the query, then it does not use the language. So by setting the language in the constructor (which is always the first method that is run), and then running the query in a getter method, the query then uses the specified language.

Here is the working version of the class:

public class setranstest {
    public Case casex {get;set;}
    public string specifiedLanguage {get;set;}
    public setranstest() {
        specifiedLanguage = 'fr';
    }
    public Component.Apex.OutputText getDynamicText() {
        casex = [select Id,toLabel(CEC_Case_Category__c) from Case where Id = '500Dh00000Db4xMIAR'];
        Component.Apex.OutputText outx = new Component.Apex.OutputText();    
        outx.expressions.value = 'The category is:{!casex.CEC_Case_Category__c}';
        outx.escape = false;
        return outx;   
    } 
}

You must log in to answer this question.

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