Skip to content

Commit 489a226

Browse files
committed
al-feat(Translation): Implement GetTranslations procedure and add Translation Buffer table
Add a new procedure GetTranslations to the Translation Implementation codeunit for retrieving translations based on record variants and field IDs. Introduce a new Translation Buffer table to store translation data temporarily, including fields for Language ID, System ID, Table ID, Field ID, and Value. This enhances the translation functionality by allowing retrieval of translations for specific fields or all fields when FieldId is 0.
1 parent b931e3d commit 489a226

File tree

4 files changed

+280
-2
lines changed

4 files changed

+280
-2
lines changed

src/System Application/App/Translation/src/Translation.Codeunit.al

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,4 +195,17 @@ codeunit 3711 Translation
195195
begin
196196
TranslationImplementation.ShowForAllRecords(TableId, FieldId);
197197
end;
198-
}
198+
199+
/// <summary>
200+
/// Gets all available translations for a record into a temporary buffer.
201+
/// If FieldId is 0, it retrieves translations for all fields.
202+
/// </summary>
203+
/// <param name="RecVariant">Source record variant</param>
204+
/// <param name="FieldId">Field ID to get translations for. Use 0 to retrieve translations for all fields.</param>
205+
/// <param name="TranslationBuffer">Output: Temporary buffer with translations</param>
206+
/// <returns>True if translations were found</returns>
207+
procedure GetTranslations(RecVariant: Variant; FieldId: Integer; var TranslationBuffer: Record "Translation Buffer"): Boolean
208+
begin
209+
exit(TranslationImplementation.GetTranslations(RecVariant, FieldId, TranslationBuffer));
210+
end;
211+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// ------------------------------------------------------------------------------------------------
2+
// Copyright (c) Microsoft Corporation. All rights reserved.
3+
// Licensed under the MIT License. See License.txt in the project root for license information.
4+
// ------------------------------------------------------------------------------------------------
5+
6+
namespace System.Globalization;
7+
8+
table 3713 "Translation Buffer"
9+
{
10+
Access = Public;
11+
Caption = 'Translation Buffer';
12+
InherentEntitlements = X;
13+
InherentPermissions = X;
14+
TableType = Temporary;
15+
16+
fields
17+
{
18+
field(1; "Language ID"; Integer)
19+
{
20+
DataClassification = SystemMetadata;
21+
Caption = 'Language ID';
22+
TableRelation = Language."Windows Language ID";
23+
}
24+
field(2; "System ID"; Guid)
25+
{
26+
DataClassification = SystemMetadata;
27+
Caption = 'System ID';
28+
Editable = false;
29+
}
30+
field(3; "Table ID"; Integer)
31+
{
32+
DataClassification = SystemMetadata;
33+
Caption = 'Table ID';
34+
Editable = false;
35+
}
36+
field(4; "Field ID"; Integer)
37+
{
38+
DataClassification = SystemMetadata;
39+
Caption = 'Field ID';
40+
Editable = false;
41+
}
42+
field(5; Value; Text[2048])
43+
{
44+
DataClassification = CustomerContent;
45+
Caption = 'Value';
46+
}
47+
}
48+
49+
keys
50+
{
51+
key(Key1; "Language ID", "System ID", "Field ID")
52+
{
53+
Clustered = true;
54+
}
55+
key(Key2; "Table ID", "Field ID")
56+
{
57+
}
58+
}
59+
}

src/System Application/App/Translation/src/TranslationImplementation.Codeunit.al

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,29 @@ codeunit 3712 "Translation Implementation"
183183
Error(MaxLenghtErr, Translation.Value, StrLen(Translation.Value), Field.Len);
184184
end;
185185

186+
procedure GetTranslations(RecVariant: Variant; FieldId: Integer; var TranslationBuffer: Record "Translation Buffer"): Boolean
187+
var
188+
Translation: Record Translation;
189+
FromRecordRef: RecordRef;
190+
begin
191+
TranslationBuffer.Reset();
192+
TranslationBuffer.DeleteAll();
193+
194+
GetRecordRefFromVariant(RecVariant, FromRecordRef);
195+
Translation.SetRange("System ID", GetSystemIdFromRecordRef(FromRecordRef));
196+
Translation.SetRange("Table ID", FromRecordRef.Number());
197+
if FieldId <> 0 then
198+
Translation.SetRange("Field ID", FieldId);
199+
if Translation.FindSet() then
200+
repeat
201+
TranslationBuffer.Init();
202+
TranslationBuffer.TransferFields(Translation);
203+
TranslationBuffer.Insert();
204+
until Translation.Next() = 0;
205+
206+
exit(TranslationBuffer.FindFirst());
207+
end;
208+
186209
local procedure GetRecordIdCaptionFromVariant(RecVariant: Variant): Text
187210
var
188211
RecordId: RecordId;
@@ -225,4 +248,4 @@ codeunit 3712 "Translation Implementation"
225248

226249
Error(NoRecordIdErr);
227250
end;
228-
}
251+
}

src/System Application/Test/Translation/src/TranslationTests.Codeunit.al

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,189 @@ codeunit 137121 "Translation Tests"
480480
Assert.ExpectedError(CannotTranslateTempRecErr);
481481
end;
482482

483+
[Test]
484+
[Scope('OnPrem')]
485+
procedure GetTranslationsForOneFieldFromRecord()
486+
var
487+
TranslationTestTable: Record "Translation Test Table";
488+
TranslationBuffer: Record "Translation Buffer";
489+
begin
490+
// [SCENARIO] Translation must be retrieved correctly for one field
491+
492+
Initialize();
493+
PermissionsMock.Set(TranslationEditRoleTok);
494+
495+
// [GIVEN] Create a record in TableA and set a translation for the fields FieldA and FieldB
496+
CreateRecord(TranslationTestTable);
497+
Translation.Set(TranslationTestTable, TranslationTestTable.FieldNo(TextField), Text1Txt);
498+
Translation.Set(TranslationTestTable, TranslationTestTable.FieldNo(TextField), GetDanishLanguageId(), Text2Txt);
499+
Translation.Set(TranslationTestTable, TranslationTestTable.FieldNo(SecondTextField), Text3Txt);
500+
501+
// [WHEN] Translations are retrieved for a specific field
502+
Assert.IsTrue(
503+
Translation.GetTranslations(TranslationTestTable, TranslationTestTable.FieldNo(TextField), TranslationBuffer),
504+
'GetTranslations should return true when translations exist');
505+
506+
// [THEN] Verify that only translations for the requested field are returned
507+
Assert.AreEqual(2, TranslationBuffer.Count(), 'Should have 2 translations for the TextField');
508+
509+
// [THEN] Verify the translation values
510+
TranslationBuffer.SetRange("Language ID", GetEnglishLanguageId());
511+
Assert.IsTrue(TranslationBuffer.FindFirst(), 'English translation should exist');
512+
Assert.AreEqual(Text1Txt, TranslationBuffer.Value, 'Incorrect English translation value');
513+
Assert.AreEqual(TranslationTestTable.FieldNo(TextField), TranslationBuffer."Field ID", 'Incorrect Field ID');
514+
Assert.AreEqual(TranslationTestTable.SystemId, TranslationBuffer."System ID", 'Incorrect System ID');
515+
516+
TranslationBuffer.SetRange("Language ID", GetDanishLanguageId());
517+
Assert.IsTrue(TranslationBuffer.FindFirst(), 'Danish translation should exist');
518+
Assert.AreEqual(Text2Txt, TranslationBuffer.Value, 'Incorrect Danish translation value');
519+
520+
// [THEN] Verify no translations for SecondTextField are included
521+
TranslationBuffer.SetRange("Language ID");
522+
TranslationBuffer.SetRange("Field ID", TranslationTestTable.FieldNo(SecondTextField));
523+
Assert.IsTrue(TranslationBuffer.IsEmpty(), 'No translations for SecondTextField should be returned');
524+
end;
525+
526+
[Test]
527+
[Scope('OnPrem')]
528+
procedure GetTranslationsForAllFieldsFromRecord()
529+
var
530+
TranslationTestTable: Record "Translation Test Table";
531+
TranslationBuffer: Record "Translation Buffer";
532+
begin
533+
// [SCENARIO] Translation must be retrieved correctly for all fields when FieldId is 0
534+
535+
Initialize();
536+
PermissionsMock.Set(TranslationEditRoleTok);
537+
538+
// [GIVEN] Create a record in TableA and set a translation for the fields FieldA and FieldB
539+
CreateRecord(TranslationTestTable);
540+
Translation.Set(TranslationTestTable, TranslationTestTable.FieldNo(TextField), Text1Txt);
541+
Translation.Set(TranslationTestTable, TranslationTestTable.FieldNo(TextField), GetDanishLanguageId(), Text2Txt);
542+
Translation.Set(TranslationTestTable, TranslationTestTable.FieldNo(SecondTextField), Text3Txt);
543+
Translation.Set(TranslationTestTable, TranslationTestTable.FieldNo(SecondTextField), GetFrenchLanguageId(), Text4Txt);
544+
545+
// [WHEN] Translations are retrieved for all fields (FieldId = 0)
546+
Assert.IsTrue(
547+
Translation.GetTranslations(TranslationTestTable, 0, TranslationBuffer),
548+
'GetTranslations should return true when translations exist');
549+
550+
// [THEN] Verify that translations for all fields are returned
551+
Assert.AreEqual(4, TranslationBuffer.Count(), 'Should have 4 translations total (2 fields x 2 languages each)');
552+
553+
// [THEN] Verify TextField translations
554+
TranslationBuffer.SetRange("Field ID", TranslationTestTable.FieldNo(TextField));
555+
Assert.AreEqual(2, TranslationBuffer.Count(), 'Should have 2 translations for TextField');
556+
557+
TranslationBuffer.SetRange("Language ID", GetEnglishLanguageId());
558+
Assert.IsTrue(TranslationBuffer.FindFirst(), 'English translation for TextField should exist');
559+
Assert.AreEqual(Text1Txt, TranslationBuffer.Value, 'Incorrect English translation for TextField');
560+
561+
TranslationBuffer.SetRange("Language ID", GetDanishLanguageId());
562+
Assert.IsTrue(TranslationBuffer.FindFirst(), 'Danish translation for TextField should exist');
563+
Assert.AreEqual(Text2Txt, TranslationBuffer.Value, 'Incorrect Danish translation for TextField');
564+
565+
// [THEN] Verify SecondTextField translations
566+
TranslationBuffer.SetRange("Language ID");
567+
TranslationBuffer.SetRange("Field ID", TranslationTestTable.FieldNo(SecondTextField));
568+
Assert.AreEqual(2, TranslationBuffer.Count(), 'Should have 2 translations for SecondTextField');
569+
570+
TranslationBuffer.SetRange("Language ID", GetEnglishLanguageId());
571+
Assert.IsTrue(TranslationBuffer.FindFirst(), 'English translation for SecondTextField should exist');
572+
Assert.AreEqual(Text3Txt, TranslationBuffer.Value, 'Incorrect English translation for SecondTextField');
573+
574+
TranslationBuffer.SetRange("Language ID", GetFrenchLanguageId());
575+
Assert.IsTrue(TranslationBuffer.FindFirst(), 'French translation for SecondTextField should exist');
576+
Assert.AreEqual(Text4Txt, TranslationBuffer.Value, 'Incorrect French translation for SecondTextField');
577+
end;
578+
579+
[Test]
580+
[Scope('OnPrem')]
581+
procedure GetTranslationsNoTranslationsExist()
582+
var
583+
TranslationTestTable: Record "Translation Test Table";
584+
TranslationBuffer: Record "Translation Buffer";
585+
begin
586+
// [SCENARIO] GetTranslations returns false when no translations exist
587+
588+
Initialize();
589+
PermissionsMock.Set(TranslationEditRoleTok);
590+
591+
// [GIVEN] Create a record without any translations
592+
CreateRecord(TranslationTestTable);
593+
594+
// [WHEN] Translations are retrieved
595+
// [THEN] GetTranslations should return false
596+
Assert.IsFalse(
597+
Translation.GetTranslations(TranslationTestTable, TranslationTestTable.FieldNo(TextField), TranslationBuffer),
598+
'GetTranslations should return false when no translations exist');
599+
600+
// [THEN] Buffer should be empty
601+
Assert.IsTrue(TranslationBuffer.IsEmpty(), 'Translation buffer should be empty');
602+
end;
603+
604+
[Test]
605+
[Scope('OnPrem')]
606+
procedure GetTranslationsWithRecordRef()
607+
var
608+
TranslationTestTable: Record "Translation Test Table";
609+
TranslationBuffer: Record "Translation Buffer";
610+
RecRef: RecordRef;
611+
begin
612+
// [SCENARIO] GetTranslations works correctly with RecordRef variant
613+
614+
Initialize();
615+
PermissionsMock.Set(TranslationEditRoleTok);
616+
617+
// [GIVEN] Create a record and set translations
618+
CreateRecord(TranslationTestTable);
619+
Translation.Set(TranslationTestTable, TranslationTestTable.FieldNo(TextField), Text1Txt);
620+
Translation.Set(TranslationTestTable, TranslationTestTable.FieldNo(TextField), GetDanishLanguageId(), Text2Txt);
621+
622+
// [GIVEN] Get RecordRef from the record
623+
RecRef.GetTable(TranslationTestTable);
624+
625+
// [WHEN] Translations are retrieved using RecordRef
626+
Assert.IsTrue(
627+
Translation.GetTranslations(RecRef, TranslationTestTable.FieldNo(TextField), TranslationBuffer),
628+
'GetTranslations should work with RecordRef');
629+
630+
// [THEN] Verify translations are returned correctly
631+
Assert.AreEqual(2, TranslationBuffer.Count(), 'Should have 2 translations');
632+
633+
TranslationBuffer.SetRange("Language ID", GetEnglishLanguageId());
634+
Assert.IsTrue(TranslationBuffer.FindFirst(), 'English translation should exist');
635+
Assert.AreEqual(Text1Txt, TranslationBuffer.Value, 'Incorrect English translation value');
636+
end;
637+
638+
[Test]
639+
[Scope('OnPrem')]
640+
procedure GetTranslationsBufferContainsCorrectMetadata()
641+
var
642+
TranslationTestTable: Record "Translation Test Table";
643+
TranslationBuffer: Record "Translation Buffer";
644+
begin
645+
// [SCENARIO] Translation Buffer contains correct metadata fields
646+
647+
Initialize();
648+
PermissionsMock.Set(TranslationEditRoleTok);
649+
650+
// [GIVEN] Create a record and set a translation
651+
CreateRecord(TranslationTestTable);
652+
Translation.Set(TranslationTestTable, TranslationTestTable.FieldNo(TextField), GetDanishLanguageId(), Text2Txt);
653+
654+
// [WHEN] Translations are retrieved
655+
Translation.GetTranslations(TranslationTestTable, TranslationTestTable.FieldNo(TextField), TranslationBuffer);
656+
657+
// [THEN] Verify all metadata fields are populated correctly
658+
Assert.IsTrue(TranslationBuffer.FindFirst(), 'Translation should exist');
659+
Assert.AreEqual(GetDanishLanguageId(), TranslationBuffer."Language ID", 'Incorrect Language ID');
660+
Assert.AreEqual(TranslationTestTable.SystemId, TranslationBuffer."System ID", 'Incorrect System ID');
661+
Assert.AreEqual(Database::"Translation Test Table", TranslationBuffer."Table ID", 'Incorrect Table ID');
662+
Assert.AreEqual(TranslationTestTable.FieldNo(TextField), TranslationBuffer."Field ID", 'Incorrect Field ID');
663+
Assert.AreEqual(Text2Txt, TranslationBuffer.Value, 'Incorrect translation value');
664+
end;
665+
483666
local procedure Initialize()
484667
var
485668
TranslationTestTable: Record "Translation Test Table";

0 commit comments

Comments
 (0)