5

How can one retrieve these translations for a custom label via the metadata API?

Enter image description here

1
  • You can retrieve Custom Label Translations by using "Translations" Tag in the Package.xml (help.salesforce.com/…)
    – Vinay
    Commented Jun 19, 2020 at 19:18

3 Answers 3

14

You need to use both CustomLabel and Translations.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
     <types>
      <members>*</members>
      <name>CustomLabels</name>
    </types>
     <types>
      <members>*</members>
      <name>Translations</name>
    </types>
    <version>49.0</version>
</Package>

After retrieving with this, you are going to get all translated custom labels under translations folder. labels folder will have original versions. You can use a language code in translations to pull specific language.

<types>
  <members>es</members>
  <name>Translations</name>
</types>

Also quoting Usage part in the above link

When you use the retrieve() call to get translations in your organization, the files returned in the .translations folder only include translations for the other metadata types referenced in package.xml.

2
  • @snl oh sh... Seems like I am blind. Sorry about this. Please feel free to let the answerer know via comments etc. if they are answering the wrong question.
    – Lafexlos
    Commented Jun 20, 2020 at 11:11
  • 1
    Thank you very much for your tips! i couldn't find this advice anywhere and keep getting translations files empty before knowing i had to retrieve also customlabels in the same package.xml. Thanks a lot @lafexlos
    – sissy
    Commented Feb 17, 2022 at 13:44
5

I retrieved the details using the apex-mdapi. Example code below:

List<String> labels = new List<String>{'Demo1'};

MetadataService.MetadataPort service = new MetadataService.MetadataPort();
service.SessionHeader = new MetadataService.SessionHeader_element();
service.SessionHeader.sessionId = UserInfo.getSessionId();

Map<String, String> valuesByKey = new Map<String, String>();
MetadataService.Translations translations = (MetadataService.Translations) service.readMetadata('Translations',
                new String[] { 'en_US' }).getRecords()[0];
for(MetadataService.CustomLabelTranslation customLabelTranslation : translations.customLabels) {
    if (labels.contains(customLabelTranslation.name)) {
        valuesByKey.put(customLabelTranslation.name, customLabelTranslation.label);
    }
}
System.debug(valuesByKey);
0

I used CustomLabel instead of CustomLabels (plural) and worked for me

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
     <types>
      <members>*</members>
      <name>CustomLabel</name>
    </types>
     <types>
      <members>*</members>
      <name>Translations</name>
    </types>
    <version>48.0</version>
</Package>

You must log in to answer this question.

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