Skip to content
Arthur van de Vondervoort edited this page Jun 8, 2024 · 5 revisions

GlobalLanguage() must be implemented through the Translation Helper codeunit from the Base Application.

Implementing the GlobalLanguage() function through the Translation Helper codeunit wil result in more readable code.

  • Automatically saves the current value of the GlobalLanguage
  • Support for LanguageCode next to LanguageId
  • Support for setting the GlobalLanguage (back) to default application Language (without hardcoding the 1033 value)

Source: https://github.com/StefanMaron/MSDyn365BC.Code.History/blob/master/BaseApp/Source/Base%20Application/System/Utilities/TranslationHelper.Codeunit.al

Example

Calling the GlobalLanguage() function directly

local procedure MyAwesomeFunction()
var
    Language: Codeunit Language;
    CurrentGlobalLanguageId: Integer;
    LanguageId: Integer;
begin
    CurrentGlobalLanguageId := GlobalLanguage();
    LanguageId := Language.GetLanguageId(Rec."Language Code");
    GlobalLanguage(LanguageId);
    // Some code here
    GlobalLanguage(CurrentGlobalLanguageId);
end;

Calling the GlobalLanguage() function through the Translation Helper codeunit

local procedure MyAwesomeFunction()
var
   TranslationHelper: Codeunit "Translation Helper";
begin
   TranslationHelper.SetGlobalLanguageByCode(Rec."Language Code");
   // Some code here
   TranslationHelper.RestoreGlobalLanguage();
end;

Exceptions

There are some exceptions where calling directly the GlobalLanguage() could make sense. For example setting the report language.

trigger OnSomething()
begin
    CurrReport.Language := GlobalLanguage(); // Should not raise warning LC0022 (allow to use for return value)
end;

This could also be implemented like

trigger OnSomething()
var
    Language: Codeunit Language;
begin
    CurrReport.Language := Language.GetLanguageIdOrDefault('');
end;

Obsolete?

In the Translation Helper codeunit there's a remark This functionality should be provided in the platform and this codeunit should be deleted.. This means that in the undefined future this implementation will probably change. Using this codeunit is still beter then writing the code yourself. And if it gets implemented in the platform, that probably should be easy to refactor.

Clone this wiki locally