1

In a lightning component, we have to display some records in a datatable with selectable rows and where the table header will display each column field label. To cater for multi-language, the header column label must be translated in 4 languages (English, French, Dutch, German). Currently in lightning markup we do not have any global variable to access an object field label, so we are trying to retrieve the field label dynamically from apex using getDescribe methods and pass the data as a map to the lightning component.

But there seem to be an issue with getDescribe when accessing field label for standard system field like Owner. Here is the sample code for Account object

for(Schema.SobjectField strFld: Account.SobjectType.getDescribe().fields.getMap().Values()){
        Schema.DescribeFieldResult dfield = strFld.getDescribe();   
        if( dfield.getName()=='OwnerId'){
            system.debug('API Name='+dfield.getName()+ ' ,label= ' + dfield.getLabel());// API Name=OwnerId ,label= Owner ID
            system.debug('Relationshipname = '+ dField.getRelationshipName()); // Relationshipname = Owner
        }
    }

The first debug log shows the field label as Owner Id, but we would like to have label "Owner" only so that this get translated to other languages. Currently, this shows as "ID du proprietaire" in French for example.

Now if we try to use the getRelationshipName() method, it shows "Owner" in all languages, so this is not translated at all.

Is there any other workaround besides defining a custom label?

Thank you

1 Answer 1

0

If I'm not mistaken this has been a problem for time immemorial with labels and standard relationship fields.

In some cases, I've reverted to using the object type of the related record. For instance, if I need the Account field label of a Contact record, the problem, as you state is I would do the following:

System.debug(Schema.SObjectType.Contact.fields.AccountId.label);  

Output: Account ID

I can, as an alternative use the sobject label itself:

System.debug(Schema.SObjectType.Account.label);

Output: Account

But with all the different User relationship fields (OwnerId, CreatedById, LastModifiedById, etc.), the problem here is of course, they are all User.

In a single-language org, you could easily trim off the "ID" using substring or String.remove(). In multilanguage, you're for sure signing up for some degree of String acrobatics. This is a lot easier now with the improvements in String methods in recent versions, but still fragile code when it boils down to it. You've not stated which four languages, but let's take French, Spanish, English, and German. Consider:

'ID de proprietaire'.remove('ID').remove('de').trim(); // proprietaire
'Owner ID'.remove('ID').remove('de').trim();           // Owner
'ID de propietario'.remove('ID').remove('de').trim();  // proprietario
'Besitzer ID'.remove('ID').remove('de').trim();        // Besitzer

But throw Portuguese, Italian, or any number of other languages and you're doing more remove calls, or going further with using regex, perhaps.

And what about CreatedById and LastModifiedById? Will it hold up?

Or, as you stated it, you use a custom label. In my opinion, given the finite number of these fields, and built in translation, you're signing yourself up for a much better quality of maintenance in this instance, given the several languages you're using.

1
  • Thanks for your detailed answer. I have added the required 4 languages in the question post. No info yet but other languages can be added in the near future. Trimming off the string labels is nice but like you said the number of remove calls really depends on the language. for this lightning component, we only need owner field so yes looks like custom label is best option as a workaround
    – Shamina
    Commented Jun 22, 2019 at 6:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .