6

Is there a way to combine custom settings with custom labels ? So that the output from a setting value would be a custom label ?

To explain:

In example, we have an international organisation, and say countries are stored as an ISO code. As this is a limited and stable set of data, using custom settings would be the appropriate way to deal with say the full country name. The issue arises when I do not just want to store the English country name, but want to have it translatable, presenting the right translation depending on the user's locale. Now, this is the point where I'd like to integrate custom settings with custom labels.

So that say the custom setting with name 'some iso code' doesn't return the full country name, but Label.Country'some iso code', which is than automatically translated in the right version.

Sounds good right. But as custom settings are fixed to certain types, as far as I've gotten is put the label name in the settings as a string value. And then .. i'm stuck :). No way to cast a string to a Custom label, or have it be interpreted (and translated) as such, or is there ?

I'm aware this can be done in a custom object & apex, I was hoping for a more automated, "cleaner", less resource consuming way, better integrated, configurable way.

Additional clarrification: *The goal is to extend the use of custom labels (translation workbench), by adding a mapping from any data-based value to a custom label.*

While we could do a long matching process in apex, it would be faster & neater if the custom settings mechanism could be used. There are disadvantaged to custom objects, they require soql & apex, while custom settings & custom labels are cached. They too can be used in many other places. Countries were an example, I'd use this mechanism for every variable value that needs to be translatable.

3 Answers 3

5

Sdry, I have wanted to do exactly this ever since Custom Settings came out. The problem, though, is that there is no Apex API for Custom Labels (click the link to vote for the idea for it!), so Custom Labels cannot be dynamically retrieved or requested within Apex. However, it CAN, as of Spring 12, be done in Visualforce, using Dynamic Components.

Here, I think is what you are looking for in Apex (that cannot yet be done):

Custom Settings + Custom Labels IN APEX (**not yet possible**)

// Get the stored Custom Setting record for User Preferences
User_Prefs__c p = User_Prefs__c.getInstance(UserInfo.getUserId());

// Get the stored ISO Code
String isoCode = p.ISO_Code__c;

// Get the running user's language
String languageKey = [
    select LanguageLocaleKey from User 
     where Id = :UserInfo.getUserId() 
     limit 1
].LanguageLocaleKey;

// Get the translation of the ISO Code corresponding to this user's language
// **THIS IS NOT POSSIBLE YET IN APEX**
String translatedCode = System.Label.getLabel('ISOCode_'+isoCode).getTranslation(languageKey);

And here would be an equivalent if you are using Visualforce:

Custom Settings + Custom Labels IN VISUALFORCE (possible right now)

/* APEX CONTROLLER */
public class Controller {

    public String getStoredISOCode() {
        // Get the stored Custom Setting record for User Preferences,
        // and return the ISO code
        return User_Prefs__c.getInstance(UserInfo.getUserId()).ISO_Code__c;
    }

}

/* VF PAGE */
<apex:page controller="MyController">

    <b>ISO Code</b>{!storedISOCode}
    <b>Translated</b>{!$Label['ISOCode_' & storedISOCode]}

</apex:page>
2
  • In the interim you can get around it by adding fields to your custom setting corresponding to each language you want to support. Not great but do-able. Oct 12, 2012 at 17:44
  • Note that if you're creating a package, the ISOCode_ Custom Labels will not get packaged unless that are elsewhere referenced as {!$Label.ISOCode_en} or System.Label.ISOCode_en, for example.
    – xn.
    Dec 14, 2012 at 1:58
2

Why wouldn't you just use Custom Labels with the Translation workbench. I'm struggling to grasp the value that Custom Settings add here.

If it must be custom settings (assuming List Custom Setting), probably have a field for each translated language.

So example (these are field names) on a Custom Setting called Translation_Local__c

Name -- English Values

Russian__c -- Russian translation

French__c -- French translation

And so on

You could just have a field for each supported labguage

So if you wanted to get the French value for England, you'd say

String userLanguage = // deduce language from User Record

Translation_Local_c.getInstance('England').get(userLanguage + '_c');

3
  • Is my question that badly put ? It's about mapping+translation. to not only go from 'A' to 'first letter in the alphabet' but to go from 'A' to 'translation of "first letter in the alphabet"' where A could also be B, or C, or D etc. To avoid having to do a lot of if/else in apex, or use soql, custom settings already provide an mechanism for this, but it seems it can't be done. Was hopeing some of the greater minds would have a solution. Oct 11, 2012 at 13:56
  • You can do that with Custom labels. Also did you look at what I suggested? What did you think? Oct 11, 2012 at 14:23
  • May have misunderstood myself, will try to prototype it. Oct 11, 2012 at 14:46
-1

Is there a reason you aren't using the default translation workbench?

Also it usually works out better to use a custom object to store countries, as you can then use this table in many other places.

Edit: There is a country/state picklist coming out in winter 13. This only covers Leads/Accounts/Contacts at the moment i believe. Though from talks at dreamforce I believe they want to roll it into a university picklist offering.

2
  • I've added an additional clarification to my question. Countries served as an example only. Oct 11, 2012 at 12:26
  • You do not answer the question asked.
    – Saariko
    Oct 11, 2012 at 18:18

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.