Final Up to date on July 7, 2022 by Rakesh Gupta
Massive Concept or Enduring Query:
- Learn how to use Customized Labels in Lightning Internet Parts?
Targets:
After studying this weblog, you’ll be capable of:
- Perceive the aim of customized label
- Create customized labels and add translation to it
- Import customized labels within the lightning net part
- Entry customized labels within the lightning net part
- and way more
Previously written just a few articles on Lightning Internet Element. Why not test them out while you’re at it?!
- Access Static Resources, Content Asset Files in Lightning Web Component
- Add Confirmation, Alert, and Prompt Dialog Box to Lightning Web Component
Rachel Gillett is working as a Junior Developer at Gurukul on Cloud (GoC). She has created a form utilizing the lightning net part (lwc) after studying this article, as proven beneath:
The fields label are arduous coded within the HTML file of the lightning net part.
<lightning-input
kind="e mail"
label="E mail Deal with"
worth={emailvalue}
onchange={handleEmailChange}>
</lightning-input>
<lightning-input
kind="tel"
title="cellular"
label="Cell"
worth={mobilevalue}
onchange={handleMobileChange}>
</lightning-input>
The Gross sales Director has requested her to make it possible for type labels ought to help a number of languages (primarily English, French, Spanish, and German). So, a person whose language belongs to certainly one of them should see labels of their native language.
What are Customized Labels?
Customized labels are customized textual content values that may be translated into any language that Salesforce helps.
- It’s also useful in offering one of the best in school person expertise by way of multilingual functions that mechanically current info in a person’s native language.
- It could include as much as 1000 characters.
- Customized labels are accessible from Apex, Visualforce pages, Lightning Element, Lightning Internet Parts, and Salesforce Movement.
Create Customized Labels
Create one customized label to retailer the label for the E mail Deal with and its translation.
- Click on Setup.
- Within the Person Interface, kind Customized Labels.
- Click on on the New Customized Label button.
- Enter Quick Description; the Title will auto-populate.
- Now enter the E mail Deal with within the Worth.
- Click on Save.
- Add translation for german and french languages.
Repeat the above steps to create one other customized label for Cell.
Learn how to Entry Customized Labels in Lightning Internet Element?
To entry customized labels in Lightning net parts first we now have to import from @salesforce/label to the part’s JavaScript file.
import LabelName from '@salesforce/label/label-reference';
- labelName – A reputation that refers back to the label.
- labelReference – The title of the label in your org within the format namespace.labelName.
Automation Champion Method (I-do):
Now it’s time to create a lightning net part to programmatically entry customized labels.
customLabelExample.js-meta.xml
A lightning net part can be utilized to construct customized pages for Lightning Expertise and the Salesforce cellular app rapidly with point-and-click instruments. Make certain so as to add the suitable goal for it.
This configuration file makes the part out there for all Lightning web page sorts however restricts help on desktops.
<?xml model="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://cleaning soap.sforce.com/2006/04/metadata">
<apiVersion>54.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<goal>lightning__RecordPage</goal>
<goal>lightning__AppPage</goal>
<goal>lightning__HomePage</goal>
</targets>
</LightningComponentBundle>
customLabelExample.html
To reference a useful resource in a template, use {property} syntax (i.e. {label.emailAddress} in our instance), which is identical syntax we use to reference any JavaScript property.
<!-- customLabelExample.html -->
<template>
<type>
<div
class="slds-p-top_small slds-p-bottom_medium slds-align_absolute-center slds-theme_default slds-text-heading_medium">
<b>Customized Label Instance</b></div>
<div class="slds-box slds-theme_default">
<lightning-input
kind="e mail"
label={label.emailAddress}
worth="">
</lightning-input>
<lightning-input
kind="tel"
title="cellular"
label={label.cellular}
worth="">
</lightning-input>
<div class="slds-m-top_small slds-align_absolute-center">
<lightning-button
variant="Impartial"
label="Cancel"
class="slds-m-left_x-small"
onclick={handleCancel}>
</lightning-button>
<lightning-button
variant="model"
class="slds-m-left_x-small"
label="Subsequent"
onclick={handleNext}>
</lightning-button>
</div>
</div>
</type>
</template>
customLabelExample.js
The JavaScript code imports two customized labels: one for E mail Deal with and one other for Cell.
//customLabelExample.js
import { LightningElement } from 'lwc';
import emailAddress from '@salesforce/label/c.Email_Address';
import cellular from '@salesforce/label/c.Cell';
export default class InputExample extends LightningElement() {
// Expose the labels to make use of within the template.
label = {
emailAddress,
cellular,
};
handleNext(){
}
handleCancel(){
}
}
Proof of Idea
Now, if a person whose language is about up as French views the shape, they’ll see the sphere’s label (E mail handle and Cell) of their native language.
Nice! You’re achieved! Be happy to change the lightning net part code to make use of the customized header and button labels.
Formative Evaluation:
I wish to hear from you!
What’s one factor you discovered from this submit? How do you envision making use of this new information in the actual world? Be happy to share within the feedback beneath.