How can one retrieve these translations for a custom label via the metadata API?
3 Answers
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.
-
@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.– LafexlosCommented Jun 20, 2020 at 11:11
-
1Thank 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– sissyCommented Feb 17, 2022 at 13:44
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);
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>