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 && data.results) {
let objectInfo = data.results.reduce((map, obj) => (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
}
}
}