8

I am trying to assign the language dynamically in a Visualforce page which I have specified in controller. Right now it is hard coded as

<apex:page language="de">
........
</apex:page>

Is it possible to achieve this via global variables?

<apex:page controller="controller_1" Language="{!code}" >
 <apex:form >
  <apex:inputTextarea value="{!test}"/>
 </apex:form>
</apex:page>

public class controller_1{
    public String test {get; set;}
    public String code {get; set;}

    public controller_1()
    { 
      code ='de';     
      Schema.DescribeFieldResult f = Schema.sObjectType.Lead.fields.Company;
      test = f.getLabel();
   }  
}
1
  • 1
    You can use Custom Labels, where you can add translations for each language supported. Based on the User's language, the corresponding translation from the Custom Label will be displayed. Feb 14, 2013 at 10:35

2 Answers 2

8

Yes, you can do that in your apex:page tag, by using the attribute language, then dynamically bind it to a value in your controller or extension:

<apex:page Controller="PageController" language="{!code}">

From the documentation: The language used to display labels that have associated translations in Salesforce. This value overrides the language of the user viewing the page. Possible values for this attribute include any language keys for languages supported by Salesforce, for example, "en" or "en-US".

http://www.salesforce.com/us/developer/docs/pages/Content/pages_compref_page.htm

8
  • I have tried <apex:page Controller="PageController" language="{!code}"> already, But It is not working for the value in inputTextarea.
    – Vids
    Feb 14, 2013 at 11:11
  • 2
    that's kind of normal, you don't translate the values IN fields (except from picklist values, which you can translate iirc) you can only translate the field labels, custom labels, objects, ... not the data itself in the translation workbench.
    – pjcarly
    Feb 14, 2013 at 11:17
  • 1
    Of course it won't. The language will affect "only" field names, picklist values, custom labels etc. Any data (content entered by users into textareas) will remain exactly as is. Are you saying you expect some kind of automatic Google Translate for your data?
    – eyescream
    Feb 14, 2013 at 11:21
  • 1
    When I am giving <apex:page Controller="PageController" language="de">, It is translating the values IN fields But not when <apex:page Controller="PageController" language="{!code}">. And yes I am expecting some kind of automatic translation functionality. Is there any suggestion?
    – Vids
    Feb 14, 2013 at 12:21
  • 1
    Why does <apex:page Controller="PageController" language="de"> work at page refresh BUT <apex:page Controller="PageController" language="{!code}"> does not work ! The strange part is <apex:page Controller="PageController" renderAs="{!renderas}"> works as well ?????
    – owsata
    Jan 19, 2018 at 9:04
0

This may not be the same issue/solution for others here's how I finally got this working on my end:

1) Language Code: Set (get) the language code value for each user emailed:

   NOTE: This step may vary for others but for me I store the language
   for each user in a custom field that is setup to return a 
   SFDC-friendly language code value like en_US, zh_CN, etc. 
   Reference: https://developer.salesforce.com/forums/?id=906F00000008o2ZIAQ 

2) MyEmail.email (Email component): Add language attribute:

   <messaging:emailTemplate subject="Test Subject" recipientType="Contact"
   relatedToType="MyObject__c" language="{!relatedTo.LanguageCode__c}">

3) MyEmail.cls (Apex Class/Controller): Update SOQL

   NOTE: Make sure the main SOQL pulls MyObject__c.LanguageCode__c value for
   the relatedTo() object

4) MyEmail.component (Visualforce email): Watch Custom Labels in auto-translate themselves

    NOTE: Any custom labels in this email that have 
    translations for the language code found for each 
    user/email will automatically use that translation.

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.