0

I have a few cases where I wish to translate labels in lightning controllers and I can't get it to work.

For instance, the label head in a lightning:datatable won't work with $A.get("!$Label.c.Model"), - it'll just leave the column header blank. Worth mentioning is that these fields are translated in in regular Salesforce correctly, is it possible to set the label down below to what fieldName is referencing? Model__c in this case.

doInit: function(component, event, helper) {
    component.set('v.mycolumns', [
        {type: "button", typeAttributes: {
            label: '',
            iconName: 'utility:add',
            name: 'selectRecord',
            title: 'selectRecord',
            disabled: false,
            value: 'edit',
            variant: {fieldName: 'variantValue'},
        }},
        {label: 'Model', fieldName: 'Model__c', type: 'text', sortable: true},

            ]);
    },

Also, inside $A.createComponents - it works to use labels in the function call (as specified below), but not on the actual lightning button label - it'll also, like above, just leave a blank button.

handleShowModal: function(component, event, helper) {
    var myAccount = component.get("v.theAccountName")
    var modalBody;
    $A.createComponents([
        ["c:ModalContent", {
            "accountName": myAccount,
            }],
        ["lightning:button",{
            "label": $A.get("!$Label.c.Booking_Confirmation"),
            "variant": "brand",
            "class": "slds-align_absolute-center",
            "onclick": component.getReference("c.fireEvent")
            }],
    ],
   function(content, status) {
       if (status === "SUCCESS") {
           modalBody = content;
           component.find('overlayLib').showCustomModal({
               header: $A.get("!$Label.c.Booking_Confirmation"),
               body: modalBody,
               showCloseButton: true,
               cssClass: "mymodal",

           })
       }
   });
},

Any particular reason why?

TL;DR: What do I need to do to have custom labels working in lightning:datatables and $A.createComponents.

2

It should be:

$A.get("$Label.c.Model")

i.e. no ! in the string.

The ! is part of the expression delimiter {! in the component.

4
  • Awesome, that solves the button thing, thanks a lot! Is there a way to have the column headers just reference the fieldName, or would I need to create a custom label for each header as well? I kinda want the header to be exactly what fieldName is – erikvm Jun 15 '18 at 9:19
  • @erikvm At present you have to build your own code to get hold of the describe data that includes field labels that you could use as column headers which is a fair bit of work. Using labels as you suggest will work too. – Keith C Jun 15 '18 at 10:56
  • Thanks Keith, one final thing, do you know how to handle custom labels inside aura attributes. I've got a default list that looks like this: <aura:attribute name="options" type="List" default="[ {'label': 'Phone', 'value': 'option1'}, {'label': 'Email', 'value': 'option2'}, ]"/> However, I'm getting an error if I simply try to go "{!$Label.c.Phone}" in the label in the phone field. Any potential solution? – erikvm Jun 15 '18 at 11:37
  • @erikvm I think you'll have to build the data in an init handler; I'm not 100% sure but get the impression that the default only supports constant data not expressions. – Keith C Jun 15 '18 at 12:57

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.