function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
a_pitsaer.ax333a_pitsaer.ax333 

Retrieve Custom Labels Translation with Apex

Hi,

 

We are using Custom Labels for the translation of our application. In Apex Code, the method getLabel() retrieve the value of a given label in the language of the user. For example to retrieve the value of custom label we use: 

 

Schema.DescribeFieldResult F = Quote__c.Quote_Comment__c.getDescribe();

F.getLabel();

 

This getLabel() method returns the text label in the language of the user. However, we would like to get it in other languages. 

 

Let's illustrate this with an example. We've built a multi-langual quote engine (that can print quote in pdf thanks to a Visualforce page). As of now the Quoteis generated in the language of the user. However, we would like the user to be able to manually select in which language the Quote needs to be printed.

Therefore we need to be able to retrieve all CustomLabels translation in the Apex Code controller of the page to display thefields in the correct language (as chosen by the user).

 

It does not seem possible to do something like getLabel('Spanish') to retrieve the label translated in Spanish. Does anyone can confirm Apex Code do not support such a functionality? 

 

Thanks a lot in advance, 

 

Alexandre 

Message Edited by a_pitsaer on 08-31-2009 06:30 AM
Message Edited by a_pitsaer on 08-31-2009 06:31 AM
Message Edited by a_pitsaer on 08-31-2009 09:04 AM
Best Answer chosen by Admin (Salesforce Developers) 
a_pitsaer.ax333a_pitsaer.ax333

Paul,

 

If you only need to display the tanslated label in a visualforce page, you can add the language attribute in the header of the page as follows and have a method getDisplayLanguageCode() in your controller that returns the language code (e.g. en_US).

 

 

<apex:page standardController="Quote__c" language="{!displayLanguageCode}" showHeader="false" extensions="QuoteWizardController" action="{!wizardStartCheckPrint}">

 


 

 

My problem is that I need those label not only to display them on the VF page but also to make some string operations in Apex code. 

Message Edited by a_pitsaer on 08-31-2009 09:13 AM

All Answers

paul-lmipaul-lmi
we'd like to know how to do this as well.  getLabel() is useless in VF controllers with Force.com Sites, because you have to define a language for the sites user, but the actual language in use is not likely to be the same in all cases.
a_pitsaer.ax333a_pitsaer.ax333

Paul,

 

If you only need to display the tanslated label in a visualforce page, you can add the language attribute in the header of the page as follows and have a method getDisplayLanguageCode() in your controller that returns the language code (e.g. en_US).

 

 

<apex:page standardController="Quote__c" language="{!displayLanguageCode}" showHeader="false" extensions="QuoteWizardController" action="{!wizardStartCheckPrint}">

 


 

 

My problem is that I need those label not only to display them on the VF page but also to make some string operations in Apex code. 

Message Edited by a_pitsaer on 08-31-2009 09:13 AM
This was selected as the best answer
paul-lmipaul-lmi
i hit the solution accept button by mistake.  my problem is that i need the translated value in the controller itself.  for instance, I'm building a select list in apex, and the values contain a custom label.  i need that value to be the language of the VF page (per your example that I'm already using), not the language of the Apex running user.
Anand@SAASAnand@SAAS

Probably not the most elegant solution but when ever the user changes their language, can't you update the language on their user record to the new language and then your getLabel() would automatically pick the labels in the new language?

 

The side effect is that if the user navigates out of the page,  you'll have an issue with the language being changed to the new language that the user will have to go and reset in his "My Personal Information".

a_pitsaer.ax333a_pitsaer.ax333
Changing the user language is indeed a solution that might be considered but it is not very elegant nor robust.
paul-lmipaul-lmi
yeah, i don't have that option at all.  i need this for a page on sites, and that page is called with a lang parameter which is passed into the controller and set as the language attribute of the apex:page tag.
MaxFrielMaxFriel

This is the code I used to do what you are looking for.  This collects the labels named LILLY_PASSWORD_RESET_SUBJECT and LILLY_PASSWORD_RESET_BODY and puts their values in a map for use later on in the code...

 

 

 Map<String,String> bodyMap = new Map<String,String>();
                Map<String,String> subjMap = new Map<String,String>();
                //Collect all the labels
                ExternalString[] extStrArr = [Select Id,Value,Name FROM ExternalString WHERE Name = 'LILLY_PASSWORD_RESET_SUBJECT' OR Name = 'LILLY_PASSWORD_RESET_BODY'];
                Id bodyId = null;
                Id subjId = null;
                for(ExternalString es : extStrArr){
                	if(es.Name.equals('LILLY_PASSWORD_RESET_SUBJECT')){
                		subjId = es.Id;
                		subjMap.put(org.LanguageLocaleKey,es.Value);
                	}else{
                		bodyId = es.Id;
                		bodyMap.put(org.LanguageLocaleKey,es.Value);
                	}
                }
                
                //Collect all the translations for the labels
                ExternalStringLocalization[] eslArr = [Select ExternalStringId,Value,Language FROM ExternalStringLocalization WHERE ExternalStringId = :bodyId OR ExternalStringId = :subjId];
                for(ExternalStringLocalization esl : eslArr){
                	
                	if(esl.ExternalStringId == bodyId){
                		bodyMap.put(esl.Language,esl.Value);
                	}else{
                		subjMap.put(esl.Language,esl.Value);
                	}
                }

 

cristinacristina
I am facing the same issue, did you find a solution for this? Otherwise we might have to use custom settings instead..
a_pitsaer.ax333a_pitsaer.ax333
sorry but I don't remember it was such a long time ago. I've stopped working with Salesforce almost 5 years ago... Good luck
Helge KosuchHelge Kosuch
As I expected the code above from MaxFriel does not work. You get "sObject type 'ExternalString' is not supported."
Piotr B. KożuchowskiPiotr B. Kożuchowski
@MaxFriel
As far as I know, ExternalString is only querable in Roche environments