Dynamics 365 Business Central: How to get translated FieldCaption (Get field caption in other languages)

Dynamics 365 Business Central

Hi, Readers.
Today I would like to briefly talk about how to get translated FieldCaption via AL in Business Central.

As you might know, we can easily use the Record.FieldCaption(Any) Method to get the current caption of the specified field as a string.
For example,

When adding a language pack and switching to that language, the field caption will also be for the current language.

Test Video:

For more information about Multi-language, see How to install a Language Pack and XLIFF translation files (Working with Translations)

The question is, can we get caption in other languages when you set the local language??? For example, get Japanese Field Caption in English environment.

Yes, there is an easy way to do this. This time we’re going to use codeunit 53 “Translation Helper”.

There is a function GetTranslatedFieldCaption(), retrieves a translated field caption.

Source Code:

    // <summary>
    // Retrieves a translated field caption
    // </summary>
    // <param name="LanguageCode">The code of the language to which the field caption will be translated.</param>
    // <param name="TableID">The ID of the table where the field is located.</param>
    // <param name="FieldId">The ID of the field for which the caption will be translated.</param>
    // <returns>The field's caption translated in the specified language</returns>
    procedure GetTranslatedFieldCaption(LanguageCode: Code[10]; TableID: Integer; FieldId: Integer) TranslatedText: Text
    var
        "Field": Record "Field";
        Language: Codeunit Language;
        LanguageIdToSet: Integer;
        CurrentLanguageId: Integer;
    begin

        CurrentLanguageId := GlobalLanguage;
        LanguageIdToSet := Language.GetLanguageIdOrDefault(LanguageCode);
        if (LanguageCode <> '') and (LanguageIdToSet <> CurrentLanguageId) then begin
            GlobalLanguage(LanguageIdToSet);
            Field.Get(TableID, FieldId);
            TranslatedText := Field."Field Caption";
            GlobalLanguage(CurrentLanguageId);
        end else begin
            Field.Get(TableID, FieldId);
            TranslatedText := Field."Field Caption";
        end;
    end;

Next, let’s look at a simple example.

As with the above test, I create a Japanese translation file (I only translated one field😑).

Then I installed two language packs, Danish language (Denmark) and French language (France)

Adding test code.

Seems to be working fine.

Test Video:

Source Code:

pageextension 50114 ZYCustomerCardExt extends "Customer Card"
{
    trigger OnOpenPage()
    var
        TranslationHelper: Codeunit "Translation Helper";
        Msg: Label 'Name in current language is "%1"\Name in Japanese is "%2"\Name in Danish is "%3"\Name in French is "%4"';
    begin
        Message(Msg, Rec.FieldCaption(Name),
                    TranslationHelper.GetTranslatedFieldCaption('JPN', Database::Customer, Rec.FieldNo(Name)),
                    TranslationHelper.GetTranslatedFieldCaption('DAN', Database::Customer, Rec.FieldNo(Name)),
                    TranslationHelper.GetTranslatedFieldCaption('FRA', Database::Customer, Rec.FieldNo(Name)));
    end;
}

I hope this will help you when developing some features in multiple languages, give it a try!!!😁

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL