2

I am trying to implement i18n, I have custom labels for that.

/CustomLabels.labels-meta.xml

<?xml version="1.0" encoding="UTF-8"?>
<CustomLabels xmlns="http://soap.sforce.com/2006/04/metadata">
    <labels>
        <fullName>translate</fullName>
        <language>en_US</language>
        <protected>true</protected>
        <shortDescription>desc.</shortDescription>
        <value>Translate</value>
    </labels>
</CustomLabels>

/en_US.translation-meta.xml

<?xml version="1.0" encoding="UTF-8"?>
<Translations xmlns="http://soap.sforce.com/2006/04/metadata">
    <customLabels>
        <label>Translate</label>
        <name>translate</name>
    </customLabels>
</Translations>

/pt_BR.translation-meta.xml

<?xml version="1.0" encoding="UTF-8"?>
<Translations xmlns="http://soap.sforce.com/2006/04/metadata">
    <customLabels>
        <label>Tradução</label>
        <name>translate</name>
    </customLabels>
</Translations>

I imported in the label in my LWC

export default class Community_delivery_create extends LightningElement {
    label = {
        TRANSLATE
    }
}

And I am rendering the label in my html file

<div>
    <p>Translated Text</p>
    <p>{label.TRANSLATE}</p>
</div>

Now I am trying to figure out how to use the correct label given the user language. I can access the language through the @salesforce/i18n/lang but how would I do use it in such a way?

1 Answer 1

3

There is clear documentation around how to use labels in LWC.

import transalte from '@salesforce/label/c.translate';

export default class Community_delivery_create extends LightningElement {
   // Expose the labels to use in the template.
   label = {
    transalte
   };
   
}

In your template use the below

<div>
<p>Translated Text</p>
<p>{label.translate}</p>

Salesforce will automatically take care of looking at the translation from the translation workbench and substituting the result.

5
  • Sorry, the docs aren't very clear. I have tried this as well, but if I have 2 labels with the same name and different languages and values, it won't deploy. If I have them with different names, one for English and another for Portuguese, it will still only use the English one because I am only rendering it. {label.translate} < English. Can you elaborate your answer please, I can't get this to work.
    – Windbox
    Commented Mar 8, 2021 at 16:47
  • You need only one label! Then you create transactions for that label in languages of your choice! Then based on your salesforce user language in your user record the translation will appear! Commented Mar 8, 2021 at 17:01
  • What do you mean by "create transactions"? I am quite new to salesforce. How would I do that?
    – Windbox
    Commented Mar 8, 2021 at 17:05
  • 1
    Ah I see! Sorry assumed you have good knowledge of translation workbench! Check this video m.youtube.com/watch?v=6h060hk78x0 Commented Mar 8, 2021 at 17:07
  • 1
    That worked. Thanks a lot!
    – Windbox
    Commented Mar 9, 2021 at 18:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .