Wednesday, June 30, 2010

How to translate an enumerated text

You can easily convert an enumerated text to a string value with the enum2str function. Example:

info(enum2str(SalesType::Sales));

But this will get you the label in the default language. What about other languages?
The following small job will get you the translated label info for all values of a specified enum, with a specified language.

static void Enum2Label(Args _args)
{   DictEnum                dictEnum;

int                     valueIndex;
LanguageId              languageId='de';

int                     enumId=enumNum(SalesType);
str                     labelId;
;

dictEnum = new DictEnum(enumId);
if (dictEnum)
{
for (valueIndex = 0 ; valueIndex < dictEnum.values(); valueIndex++)
{
labelId = dictEnum.index2LabelId(valueIndex);
info(SysLabel::labelId2String2(labelId, languageId));
}
}
}
Result: Journal Angebot Dauerauftrag Auftrag Zurückgegebener Auftrag Abrufauftrag Artikelbedarf

Similar code as above is used in the SRS classes in Ax. They make it possible for humans to understand and interpret the different enum values, as they merely are integer values in the database and as such for third party applications.

No comments:

Post a Comment