You need to sign in to do that
Don't have an account?
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
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.
All Answers
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.
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".
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...
As far as I know, ExternalString is only querable in Roche environments