You need to sign in to do that
Don't have an account?

LWC: how to get translated value from Label dynamically
Hi,
We used to be able to do this in Aura:
$A is not accessible in LWC
Following the documentation I can only see a way to get access to Label's value through import.
As far as I know import don't work dynamically. You can only write an import if you know the name of the label.
I was hoping for a solution involving Apex and/or SOQL but could not find anything.
Any idea?
We used to be able to do this in Aura:
String labelName = 'mylabel'; $A.getReference("$Label.c."+ labelName);
$A is not accessible in LWC
Following the documentation I can only see a way to get access to Label's value through import.
As far as I know import don't work dynamically. You can only write an import if you know the name of the label.
I was hoping for a solution involving Apex and/or SOQL but could not find anything.
Any idea?
https://rajvakati.com/2019/02/10/using-custom-labels-in-lightning-web-components/
https://www.salesforcecodecrack.com/2019/01/how-to-access-custom-labels-in.html
https://developer.salesforce.com/docs/component-library/documentation/lwc/create_labels.html
The name of the label would be stored in a String. We can't concatenate strings in import so that would not work.
In the question the emphasis is on "dynamically".
To dynamically get the custom label in LWC you can use Apex as a workaround.
Import the apex function : Wire the imported function :
The Apex method takes a string parameter called searchKey, and returns the custom label for that parameter: https://developer.salesforce.com/docs/component-library/documentation/lwc/lwc.apex
Hope this helps you
If this helps kindly mark it as solved so that it may help others in future.
Thanks and Regards
The solution is to add this to one of your Apex classes and to call it with the label name as a string:
The situation I'm having is as follows:
I have an LWC with a table that shows data, which is coming from an external system (http endpoint). The data we receive contains codes that we need to translate to a custom label in Salesforce, because we don't want to show codes to our users. I've made an Apex method that returns a list/json array where the codes are translated to the correct custom label names.
But when putting that data in the table in LWC, I want the custom label value to be shown. I think your solution could help me with that, but I'm not completely sure how exactly.
If you want this to be accessible from LWC, just annotate it with @AuraEnabled. If you want to use it via a wire it would also have to be cacheable, but if you call it imperatively (which I suspect is what you'd want to do) you don't need it to be cacheable - that said making it cacheable makes sense since labels don't tend to change much.
In your Apex class, let's call it Labels:
The LWC can then use the Labels.getLabelString function imperatively in this way:
Hopefully that gives you enough to work with.
Thank you for your quick response!
If I try that, it compiles fine, but when I try to call the method I get an error:
System.VisualforceException: Visualforce Component types can only be created within a controller class for a Visualforce page.
I've found another example where someone mentioned a similar solution (https://success.salesforce.com/answers?id=90630000000hIRaAAM), but also a comment from someone there:
The answer above for accessing labels dynamically does not work any longer. Salesforce returns an error stating that it must be used in a VF Controller.
So sadly enough I don't think we can use that anymore..
I've also tried the solution Rushikesh Khandale mentions above, but that also doesn't work for me. Can't get 'searchKey' to be dynamic, it always expects the exact custom label name already.
I prefer to use LWC, much easier to work with compared to VF or Aura in my opinion. But this seems to be one of those situations where you need to go back to VF or Aura.. Unless you, or anyone else, has any other ideas :)
Just tried with API versions going back to 28 (Summer '13), all return te same error: Visualforce Component types can only be created within a controller class for a Visualforce page.
Seems to have been blocked for all, ignoring the API versions.
If I find a solution for this, I'll try to remember this thread and get back to it :)
NB: This would be a fair amount of effort, but if you google around you can find details on both LWC "pubsub" and LMS.
Maybe a bit too much effort for something like this, but good to get some experience with it I guess. I'll give it a go, thanks for remembering me about it!
Translate value automatically change according your org lenguage. so you need to mentioned in custom label with different lenguages
below in LWC code to get the custom label values :-
If this solution is usefull for you, Please mark as a Best Answer to help others.
Regards
Mukesh
Use this as pagereference in Aura methods
In the LWC component,
I implemented your suggested code. The issue is I am getting the same string in return without getting converted in new language.
Can you suggest if any thing else I need to do for conversation ?
I tried the link above with all values. It shows in english only for all languages.
The provided example by Priyanka works fine for me. Just to be sure, you have added translations for the label you're testing it with? If there are no translations, it always returns the 'master' value, which is in most cases English.
I've copied the example, just added stuff where needed. For a complete overview, see below. Almost all is from the example of Priyanka, I just added some things to actually get it to work.
You can add this to a record page layout to test.
LabelTranslator.page
LabelTranslatorController.cls
LWC labelTranslatorTest
LabelTranslatorTestController (used by LWC):
Custom label with a Dutch translation:
Thanks @Niels and @priyanka.
I got the results. I was missing to enable the settings from my ORG.
Thanks for your update,
Hello @priyanka @Niels @akash

I am trying to do a similar thing. The code above works as mentioned. On passing the language & label parameters, the custom label is showing appropriately.
The thing I am trying to achieve is to render the entire form [this is a lightning record form containing custom labels] in a language of whose parameter is passed in the URL. In the below image "Bienvenue" come from the translated custom label when passed as parameter from url.
I want if the language=fr Then, the entire form should render in French language (custom label already have translated values in french and spanish).
Note: Component should not be rendering based on user locale.
Any help/guidance is highly appreciated.
thanks,
Sapan Gupta
@Priyanka
Does this method still work?
all i seem to get is an error 'Error occurred during Creation, see the developer console / debug logs for details.' unfortunatly that is the line from the dubug console on finest debug