3

I have a use-case where I want to get a translation of field labels based on user locale and language.

How to do that using Apex or any other alternative is available ?

Basically query data from translation what SF is doing in translation workbech.
PS: I am looking for only coding option

Shamina
  • 4,245
  • 2
  • 21
  • 45
Nachiket Deshpande
  • 2,222
  • 4
  • 26
  • 54
  • I think toLabel(fieldName) should resolve your issues , it works on the basis of locale of user who is firing the query. – Elijah May 17 '22 at 08:39

2 Answers2

6

Here are 2 possible coding options to retrieve translated field label based on current user language

In APEX

Using Schema describe methods, the field label is translated based on user language

Map<String, Schema.SObjectField> objFieldMap = Schema.getGlobalDescribe().get('Account').getDescribe().fields.getMap();

for (String fieldName: objFieldMap.keySet()) { System.debug(LoggingLevel.DEBUG, 'Field API Name= '+fieldName + ' ,Field label=' + objFieldMap.get(fieldName).getDescribe().getLabel()); }

In LWC

getObjectInfos retrieves object metadata info for multiple objects at once. Parse the result of this wire method to extract field label, which is translated as per current user language

import {LightningElement, api, wire, track } from 'lwc';
import { getObjectInfos } from 'lightning/uiObjectInfoApi';
import ACCOUNT_OBJECT from '@salesforce/schema/Account';
import CONTACT_OBJECT from '@salesforce/schema/Contact';

export default class lwcSample extends LightningElement {

@wire(getObjectInfos, {
        objectApiNames: [ ACCOUNT_OBJECT, CONTACT_OBJECT]
    })
    wireObjInfos({
        data,
        error
    }){
        if (data &amp;&amp; data.results) {
            let objectInfo = data.results.reduce((map, obj) =&gt; (map[obj.result.apiName] = obj, map), {});

            for (const field in objectInfo['Account'].result.fields) {
                console.log('field API Name = ' + field + ' ,label = ' , objectInfo['Account'].result.fields[field].label);
            }

        }else if(error){
            //handleError
        }
    }

}

Shamina
  • 4,245
  • 2
  • 21
  • 45
-3

Translate Custom Labels:

Translations for custom labels determine what text to display for the label’s value when a user’s default language is the translation language.

  1. From Setup, in the Quick Find box, enter Custom Labels, then select Custom Labels.
  2. Select the name of the custom label to open.
  3. In the Translations related list, click New to enter a new translation or Edit next to the language to change a translation.
  4. Select the Language you are translating into.
  5. In the Translation Text field, enter the translated value. This text overrides the value specified in the label's Value field when a user's default language is the translation language.
  6. Save your changes.

Refer the help article.

Translation Workbench:

Use Translation Workbench to maintain translated values for metadata and data labels in your Salesforce org. Specify languages for translation and assign translators for each language. Manage translated values for any Salesforce supported language. Translators can maintain translations directly through the workbench, or you can export translation files for bulk translation imports.

You can select which user would see which languag. From SF Docs:


Adding Translated Languages and Translators Who are translators? • A translator is any user who is designated to translate for a specific language. • Translators must have the “View Salesforce Configuration” permission to access the setup pages.

To add or edit translated languages and translators:

  1. Click Your Name > Setup > Translation Workbench > Translation Settings.
  2. Click Add to activate a new language or Edit to change an existing supported language.
  3. If adding a new language, choose a language.
  4. To make the entered translations available to your users - select Active. Users can change their personal language anytime whether or not it's active in the Translation Workbench. Selecting Active makes the translations available to the users in that language. Tip: We recommend you don't make a language active until the translators have translated all values.
  5. To assign translators for this language, select them from the Available List and click Add. If you don't see the member you want to add, enter keywords in the search box and click Find. Important: Ensure all translators have the "View Setup and Configuration" permission so that they can begin translating. Users can only translate languages they're assigned to.
  6. Click Save.

Custom objects are not available in the translation workbench. Use the rename tabs and labels.

Refer the help article.

Ankaiah Bandi
  • 1,392
  • 1
  • 4
  • 6
  • 3
    Unfortunately this doesn't answer the question which is asking for a way to programmatically get the translation detail for a given field based on a locale and language. – Phil W May 17 '22 at 07:43