1

We are a multilingual org and need to be to dynamically translate field labels according to the running user's settings. Using Lightning:InputField is not always an option and we preferably do not want to use custom labels as we already have translations via the translation workbench.

For example to retrieve the label for Contact.FirstName we could use the following in Visualforce:

{!$ObjectType.Contact.fields.FirstName.Label}

2 Answers 2

4

As of today you cannot access field labels from a lightning component controller or markup and there is no visualforce equivalent. You can access a custom label from markup like that:

{!$Label.c.labelName}

But cannot access a field label. There is an idea you can upvote : Access sObject field lables from lightning component

Reference: Get field label in lightning component

Alternative Approach: You will have to fetch them from server side using apex.

1

I was recently working on an internal tool for this at the office. Effectively a declarative "service component" you can bring into your own Lightning Components to specify a bunch of field/object labels that you need to pull dynamically from Apex, with a few additional hooks to ensure that you can respond appropriately to the delay in getting these items loaded.

I don't think it's ready for sharing with the community yet and I'd need management approval but I could look into making it public at some point. Would there be a great deal of community interest in such a tool?

At the moment, the tool is capable of returning the following:

  • Object:
    • Label (singular and plural)
    • Key prefix (e.g. 001 for Account)
    • Whether it is accessible / createable / deletable / queryable / updateable for the current user
  • Record Types, indexed by Developer Name
    • Id
    • Name
    • Whether it is active
  • Fields
    • Label
    • Type
    • Whether it is accessible / createable / updateable / nillable

What other information would you want out of a declarative Lightning solution to dynamic object/field references?

5
  • Did something similar. It will be nice to specify a language, and get help text/field description
    – kurunve
    Oct 17, 2018 at 15:36
  • I think this sounds great, +1 for help text.
    – Girbot
    Oct 17, 2018 at 17:30
  • @kurunve Specifying a language would be difficult unfortunately because Apex describe only returns labels for user's current language. However it would at least be useful for multi-language orgs as long as you're not trying to display in another user's language.
    – Charles T
    Oct 17, 2018 at 17:48
  • @Girbot Good idea, help text could be included too.
    – Charles T
    Oct 17, 2018 at 17:49
  • @CharlesT I know, met same challenge. What you can potentially do -- create a VF Page with language as parameter with all the fields, and labels and help text printed in it, and the perform getContent() of that VF page to get labels in proper language. And feel free to post a link to github;)
    – kurunve
    Oct 17, 2018 at 18:13

You must log in to answer this question.

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