TypeScript (Developer Preview)
Static Resources
Content Asset Files
SVG Resources
Labels
Internationalization
Current User ID
Current Community
Permissions
Client Form Factor
Mobile-Ready Components
Develop Secure Code
Import labels from the @salesforce/label scoped module. 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.
1import labelName from "@salesforce/label/labelReference";labelName—A name that refers to the label.labelReference—The name of the label in your org in the format namespace.labelName. We use this format because it’s the same format used in managed packages, in Visualforce, and in other Salesforce technologies. You can use the same format to access labels, myns.labelName, regardless of where you’re accessing them.This sample code imports two labels.
1// labelExample.js
2import { LightningElement } from "lwc";
3
4// Import the URL for the static resource named 'salesforceLogo'
5import SALESFORCE_LOGO from "@salesforce/resourceUrl/salesforceLogo";
6
7// Import custom labels
8import greeting from "@salesforce/label/c.greeting";
9import salesforceLogoDescription from "@salesforce/label/c.salesforceLogoDescription";
10
11export default class LabelExample extends LightningElement {
12 // Expose the static resource URL to use in the template.
13 logoUrl = SALESFORCE_LOGO;
14
15 // Expose the labels to use in the template.
16 label = {
17 greeting,
18 salesforceLogoDescription,
19 };
20}To use the labels in the template, use the same {property} syntax that you use to reference any JavaScript property.
1<!-- labelExample.html -->
2<template>
3 <c-page-header
4 header="Using static resources and custom labels"
5 description="This sample shows how to reference external items like static resources and custom labels"
6 ></c-page-header>
7
8 <c-card>
9 <img src={logoUrl} alt={label.salesforceLogoDescription} width="100" /><br />
10 <br />
11 {label.greeting}
12 </c-card>
13</template>In a Salesforce DX project, label files can live in any subdirectory of force-app/main/default. For example, this file named ExampleLabels.labels-meta.xml lives in force-app/main/default/mylabels.
1<?xml version="1.0" encoding="UTF-8"?>
2<CustomLabels xmlns="http://soap.sforce.com/2006/04/metadata">
3 <labels>
4 <fullName>quoteManual</fullName>
5 <value>This is a manual quote.</value>
6 <language>en_US</language>
7 <protected>false</protected>
8 <shortDescription>Manual Quote</shortDescription>
9 </labels>
10 <labels>
11 <fullName>quoteAuto</fullName>
12 <value>This is an automatically generated quote.</value>
13 <language>en_US</language>
14 <protected>false</protected>
15 <shortDescription>Automatic Quote</shortDescription>
16 </labels>
17</CustomLabels>See Also