Created
February 14, 2022 20:25
-
-
Save brianmfear/c6b6456972adb9799a35be8eb99bc32e to your computer and use it in GitHub Desktop.
Retrieve a label dynamically in LWC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<aura:application extends="force:slds"> | |
<c:q317434 /> | |
</aura:application> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata"> | |
<apiVersion>54.0</apiVersion> | |
<status>Active</status> | |
</ApexClass> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<lightning-input label="Source"></lightning-input> | |
<lightning-button label="Get Label" onclick={handleClick}></lightning-button> | |
{outputLabel} | |
</template> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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**"; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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