Skip to content

Instantly share code, notes, and snippets.

@brianmfear
Created February 14, 2022 20:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianmfear/c6b6456972adb9799a35be8eb99bc32e to your computer and use it in GitHub Desktop.
Save brianmfear/c6b6456972adb9799a35be8eb99bc32e to your computer and use it in GitHub Desktop.
Retrieve a label dynamically in LWC
<aura:application extends="force:slds">
<c:q317434 />
</aura:application>
<?xml version="1.0" encoding="UTF-8"?>
<AuraDefinitionBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>54.0</apiVersion>
<description>Container for Dynamic Labels in LWC</description>
</AuraDefinitionBundle>
public class label {
@AuraEnabled(cacheable=true) public static String getLabel(String label) {
PageReference pageRef = Page.label;
pageRef.getParameters().put('label', label);
return pageRef.getContent().toString();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>54.0</apiVersion>
<status>Active</status>
</ApexClass>
<apex:page sidebar="false"
showChat="false"
showHeader="false"
applyBodyTag="false"
applyHtmlTag="false"
contentType="application/json"
language="{!$CurrentPage.parameters.lang}">{
"value": "{!JSENCODE($Label[$CurrentPage.Parameters.label])}"
}</apex:page>
<?xml version="1.0" encoding="UTF-8"?>
<ApexPage xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>49.0</apiVersion>
<availableInTouch>false</availableInTouch>
<confirmationTokenRequired>false</confirmationTokenRequired>
<label>label</label>
</ApexPage>
<template>
<lightning-input label="Source"></lightning-input>
<lightning-button label="Get Label" onclick={handleClick}></lightning-button>
{outputLabel}
</template>
import { LightningElement } from "lwc";
import getLabel from "@salesforce/apex/label.getLabel";
export default class Q317434 extends LightningElement {
outputLabel;
async handleClick(event) {
try {
const label = this.template.querySelector("lightning-input").value;
this.outputLabel = JSON.parse(await getLabel({ label })).value;
} catch (e) {
this.outputLabel = "**Unknown**";
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>52.0</apiVersion>
<isExposed>false</isExposed>
</LightningComponentBundle>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment