Skip to content

Commit

Permalink
[releases/24.0] Language Id and Format Region override methods (#740)
Browse files Browse the repository at this point in the history
This pull request backports #549 to releases/24.0

Fixes
[[AB#507000](https://dynamicssmb2.visualstudio.com/1fcb79e7-ab07-432a-a3c6-6cf5a88ba4a5/_workitems/edit/507000)]

Co-authored-by: Magnus Hartvig Grønbech <[email protected]>
  • Loading branch information
Groenbech96 and Magnus Hartvig Grønbech authored Mar 18, 2024
1 parent 1ef5dca commit 4938123
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/System Application/App/Language/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
"publisher": "Microsoft"
}
],
"dependencies": [
{
"dependencies": [
{
"id": "7e3b999e-1182-45d2-8b82-d5127ddba9b2",
"name": "DotNet Aliases",
"publisher": "Microsoft",
Expand Down
49 changes: 49 additions & 0 deletions src/System Application/App/Language/src/Language.Codeunit.al
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,55 @@ codeunit 43 Language
exit(LanguageImpl.GetLanguageId(LanguageCode));
end;

/// <summary>
/// Overrides the language ID returned by the GetLanguageIdOrDefault function.
/// </summary>
/// <param name="LanguageId">The ID of the language to use. This must be a valid language ID.</param>
/// <remarks>
/// This override will be reset after it's used once in the GetLanguageIdOrDefault function. To keep the override throughout the application, use SetOverrideLanguageId(LanguageId: Integer; ResetOverride: Boolean) method.
/// </remarks>
procedure SetOverrideLanguageId(LanguageId: Integer)
begin
SetOverrideLanguageId(LanguageId, true);
end;

/// <summary>
/// Overrides the language ID returned by the GetLanguageIdOrDefault function.
/// </summary>
/// <param name="LanguageId">The ID of the language to use. This must be a valid language ID.</param>
/// <param name="ResetOverride">A boolean value indicating whether the override should be reset after use in the GetLanguageIdOrDefault function. If set to true, the override is reset after it's used once. If set to false, the override remains until it's manually reset or the session is restarted.</param>

procedure SetOverrideLanguageId(LanguageId: Integer; ResetOverride: Boolean)
var
LanguageImpl: Codeunit "Language Impl.";
begin
LanguageImpl.SetOverrideLanguageId(LanguageId, ResetOverride);
end;

/// <summary>
/// Overrides the format region returned by the GetFormatRegionOrDefault function.
/// </summary>
/// <param name="FormatRegion">The region to use for formatting purposes. This must be a valid region code.</param>
/// <remarks>
/// This override will be reset after it's used once in the GetFormatRegionOrDefault function. To keep the override throughout the session, use SetOverrideFormatRegion(FormatRegion: Text[80]; ResetOverride: Boolean) method.
/// </remarks>
procedure SetOverrideFormatRegion(FormatRegion: Text[80])
begin
SetOverrideFormatRegion(FormatRegion, true);
end;

/// <summary>
/// Overrides the format region returned by the GetFormatRegionOrDefault function.
/// </summary>
/// <param name="FormatRegion">The region to use for formatting purposes. This must be a valid region code.</param>
/// <param name="ResetOverride">A boolean value indicating whether the override should be reset after use in the GetFormatRegionOrDefault function. If set to true, the override is reset after it's used once. If set to false, the override remains until it's manually reset or the session is restarted.</param>
procedure SetOverrideFormatRegion(FormatRegion: Text[80]; ResetOverride: Boolean)
var
LanguageImpl: Codeunit "Language Impl.";
begin
LanguageImpl.SetOverrideFormatRegion(FormatRegion, ResetOverride);
end;

/// <summary>
/// Gets the code for a language based on its ID.
/// </summary>
Expand Down
35 changes: 34 additions & 1 deletion src/System Application/App/Language/src/LanguageImpl.Codeunit.al
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ codeunit 54 "Language Impl."
tabledata "Windows Language" = r;

var
ResetLanguageIdOverrideAfterUse, ResetFormatRegionOverrideAfterUse : Boolean;
LanguageIdOverride: Integer;
FormatRegionOverride: Text[80];
LanguageNotFoundErr: Label 'The language %1 could not be found.', Comment = '%1 = Language ID';
LanguageIdOverrideMsg: Label 'LanguageIdOverride has been applied in GetLanguageIdOrDefault. The new Language Id is %1.', Comment = '%1 - Language ID';
FormatRegionOverrideMsg: Label 'FormatRegionOverride has been applied in GetFormatRegionOrDefault. The new FormatRegion is %1.', Comment = '%1 - Format Region';
LanguageCategoryTxt: Label 'Language';

procedure GetUserLanguageCode() UserLanguageCode: Code[10]
var
Expand All @@ -38,8 +44,15 @@ codeunit 54 "Language Impl."
var
LanguageId: Integer;
begin
LanguageId := GetLanguageId(LanguageCode);
if LanguageIdOverride <> 0 then begin
LanguageId := LanguageIdOverride;
Session.LogMessage('0000MJQ', StrSubstNo(LanguageIdOverrideMsg, LanguageId), Verbosity::Normal, DataClassification::SystemMetadata, TelemetryScope::All, 'Category', LanguageCategoryTxt);
if ResetLanguageIdOverrideAfterUse then
LanguageIdOverride := 0;
exit(LanguageId);
end;

LanguageId := GetLanguageId(LanguageCode);
if LanguageId = 0 then
LanguageId := GlobalLanguage();

Expand All @@ -52,6 +65,14 @@ codeunit 54 "Language Impl."
UserSessionSettings: SessionSettings;
LocalId: Integer;
begin
if FormatRegionOverride <> '' then begin
FormatRegion := FormatRegionOverride;
Session.LogMessage('0000MJR', StrSubstNo(FormatRegionOverrideMsg, FormatRegion), Verbosity::Normal, DataClassification::SystemMetadata, TelemetryScope::All, 'Category', LanguageCategoryTxt);
if ResetFormatRegionOverrideAfterUse then
FormatRegionOverride := '';
exit(FormatRegion);
end;

if FormatRegion <> '' then
exit(FormatRegion);

Expand All @@ -65,6 +86,18 @@ codeunit 54 "Language Impl."
exit('en-US');
end;

procedure SetOverrideLanguageId(LanguageId: Integer; ResetOverride: Boolean)
begin
LanguageIdOverride := LanguageId;
ResetLanguageIdOverrideAfterUse := ResetOverride;
end;

procedure SetOverrideFormatRegion(FormatRegion: Text[80]; ResetOverride: Boolean)
begin
FormatRegionOverride := FormatRegion;
ResetFormatRegionOverrideAfterUse := ResetOverride;
end;

procedure GetLanguageId(LanguageCode: Code[10]): Integer
var
Language: Record Language;
Expand Down

0 comments on commit 4938123

Please sign in to comment.