[SalesForce] Visualforce Page Force.com Sites – Language Translation – How

I have a force.com site with many Visualforce pages in it. I want to convert those pages into different languages (Ex: Spanish) based on the Locale of the user who has logged in.

Our force.com site sign-up page has "Locale" as user input field. So, planning to change the site content based on this field. Ideally, we would check the Locale and then translate the VF pages into that language for that logger in user.

We have enabled translation workbench (and activated the required languages).

What I want to know is how to proceed further, how will the page be translated? So, all my VF pages code (or apex class) should be changed to check for the Locale? What should be written in the "IF" condition? Lets say,

if(locale__c=='Spanish') { 
      //what should I do here?
  }

I don't think, this is the right way of doing. Is there an easier way? I should use this code in all the pages/controllers?

What should be my next steps? Please let me know.

Best Answer

You could use the language attribute of the VF page:

<apex:page language="es">
    <apex:outputText value="{!$Label.Foo}">

    <apex:pageBlock>
        <apex:pageBlockSection>
            <apex:outputField value="{!Account.BillingStreet}"/>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:page>

The label Foo and the fieldlabel for the Billing Street are in spanish then. Of course you must provide a spanish translation for the custom label.

The language attribute does accept ISO country codes plus an optional locale like

  • en
  • en_US
  • de
  • de_DE

You may also refer to a variable in order to set the locale dynamically:

<apex:page language="{!lang}">

This should allow you to write a VF page only once and use the platform specific translation mechanisms to translate all texts and labels.

Related Topic