How to access Custom Labels in Lightning Web Components (lwc)

What is a Custom Label?
Custom labels are text values stored in Salesforce that can be translated into any language that Salesforce supports. Use custom labels to create multilingual applications that present information (for example, help text or error messages) in a user’s native language.

To import Custom Labels we use @salesforce/label scope module in Lightning Web Components

Syntax

import labelName from '@salesforce/label/labelReference';

labelName  —> it is a name that refers to the label.labelReference —> The name of the label in your org in the format namespace.labelName
Note:
if you don’t have any namespace in your org use default namespace c
Ex: c.labelReference

Example

CustomLabelDemo.html

<template>
    <lightning-card  title="Custom Label Example" variant="narrow" icon-name="standard:opportunity">
        <p>{label.WelcomeLabel}</p>
    </lightning-card>
</template>

CustomLabelDemo.js

import { LightningElement } from 'lwc';

// importing Custom Label
import WelcomeLabel from '@salesforce/label/c.SalesforceCodeCrack';
export default class CustomLabelDemo extends LightningElement {
    label = {
        WelcomeLabel,
    };
}

CustomLabelDemo.js-meta.xml

<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata" fqn="CustomLabelDemo">
    <apiVersion>45.0</apiVersion>
    <isExposed>false</isExposed>
    <targets>
        <target>lightning__AppPage</target>
        <target>lightning__RecordPage</target>
        <target>lightning__HomePage</target>
    </targets>
</LightningComponentBundle>

Result

Happy Learning!!!

Source : https://www.salesforcecodecrack.com/2019/01/how-to-access-custom-labels-in.html

2 Thoughts to “How to access Custom Labels in Lightning Web Components (lwc)”

  1. These are really great ideas in regarding blogging.
    You have touched some good factors here. Any way keep up wrinting.
    It is appropriate time to make some plans for the future and it
    is time to be happy. I have read this post
    and if I could I desire to suggest you some interesting things or suggestions.

    Maybe you could write next articles referring to this
    article. I desire to read even more things about it!
    It is the best time to make some plans for the longer term and
    it is time to be happy. I have read this post and
    if I may just I wish to counsel you few attention-grabbing things or tips.
    Perhaps you can write subsequent articles relating to this article.
    I desire to read more things approximately it! http://apple.com

  2. I am now not positive where you’re getting your
    info, but great topic. I needs to spend some time finding out much more
    or understanding more. Thanks for excellent info I was searching for this information for my mission.

Leave a Comment