Codeunit 3712 Translation Implementation in 27

App
System Application
Namespace
System.Globalization

Procedures, 14

Versions171819202122232425262728latest

Source242526272829

Source in 27

src/System Application/App/Translation/src/TranslationImplementation.Codeunit.al251 lines, Copyright (c) Microsoft Corporation. MIT

// ------------------------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
// ------------------------------------------------------------------------------------------------

namespace System.Globalization;

using System.Reflection;

codeunit 3712 "Translation Implementation"
{
    Access = Internal;
    InherentEntitlements = X;
    InherentPermissions = X;
    Permissions = tabledata Translation = rimd;

    var
        CannotTranslateTempRecErr: Label 'Translations cannot be added or retrieved for temporary records.';
        DifferentTableErr: Label 'The records cannot belong to different tables.';
        NoRecordIdErr: Label 'The variant passed is not a record.';
        NotAValidRecordForTranslationErr: Label 'Translations cannot be added for the record on table %1.', Comment = '%1 - Table number';
        MaxLenghtErr: Label 'The provided translation "%1" must not exceed %2 characters (Current Length: %3).', Comment = '%1 = Translation, %2 = Max Length for translation, %3 = Current Length of Translation';

    procedure Any(): Boolean
    var
        Translation: Record Translation;
    begin
        exit(not Translation.IsEmpty());
    end;

    procedure Get(RecVariant: Variant; FieldId: Integer; LanguageId: Integer; FallbackToWindows: Boolean): Text
    var
        Translation: Record Translation;
        SystemId: Guid;
        TableNo: Integer;
    begin
        GetSystemIdFromVariant(RecVariant, SystemId, TableNo);
        if Translation.Get(LanguageId, SystemId, FieldId) then
            exit(Translation.Value);

        if FallbackToWindows then
            if Translation.Get(WindowsLanguage(), SystemId, FieldId) then
                exit(Translation.Value);

        exit('');
    end;

    procedure Get(RecVariant: Variant; FieldId: Integer; LanguageId: Integer): Text
    begin
        exit(Get(RecVariant, FieldId, LanguageId, false));
    end;

    procedure Set(RecVariant: Variant; FieldId: Integer; Value: Text[2048])
    begin
        Set(RecVariant, FieldId, GlobalLanguage(), Value);
    end;

    procedure Set(RecVariant: Variant; FieldId: Integer; LanguageId: Integer; Value: Text[2048])
    var
        Translation: Record Translation;
        Exists: Boolean;
        SystemId: Guid;
        TableNo: Integer;
    begin
        GetSystemIdFromVariant(RecVariant, SystemId, TableNo);
        Exists := Translation.Get(LanguageId, SystemId, FieldId);
        if Exists then begin
            if Translation.Value <> Value then begin
                Translation.Value := Value;
                Translation.Modify(true);
            end;
        end else begin
            Translation.Init();
            Translation."Language ID" := LanguageId;
            Translation."System ID" := SystemId;
            Translation."Table ID" := TableNo;
            Translation."Field ID" := FieldId;
            Translation.Value := Value;
            Translation.Insert(true);
        end;
    end;

    procedure Delete(RecVariant: Variant)
    var
        Translation: Record Translation;
    begin
        DeleteTranslations(RecVariant, Translation);
    end;

    procedure Delete(RecVariant: Variant; FieldId: Integer)
    var
        Translation: Record Translation;
    begin
        Translation.SetRange("Field ID", FieldId);
        DeleteTranslations(RecVariant, Translation);
    end;

    procedure Delete(TableID: Integer)
    var
        Translation: Record Translation;
    begin
        Translation.SetRange("Table ID", TableID);
        Translation.DeleteAll(true);
    end;

    procedure Copy(FromRecVariant: Variant; ToRecVariant: Variant; FieldId: Integer)
    var
        Translation: Record Translation;
        FromRecordRef: RecordRef;
        ToRecordRef: RecordRef;
    begin
        GetRecordRefFromVariant(FromRecVariant, FromRecordRef);
        GetRecordRefFromVariant(ToRecVariant, ToRecordRef);
        if FromRecordRef.Number() <> ToRecordRef.Number() then
            Error(DifferentTableErr);
        Translation.SetRange("System ID", GetSystemIdFromRecordRef(FromRecordRef));
        Translation.SetRange("Table ID", FromRecordRef.Number());
        if FieldId <> 0 then
            Translation.SetRange("Field ID", FieldId);
        if Translation.FindSet() then
            repeat
                Set(ToRecVariant, Translation."Field ID", Translation."Language ID", Translation.Value);
            until Translation.Next() = 0;
    end;

    procedure Copy(FromRecVariant: Variant; FromFieldId: Integer; ToRecVariant: Variant; ToFieldId: Integer)
    var
        Translation: Record Translation;
        FromRecordRef: RecordRef;
    begin
        GetRecordRefFromVariant(FromRecVariant, FromRecordRef);
        Translation.SetRange("System ID", GetSystemIdFromRecordRef(FromRecordRef));
        Translation.SetRange("Field ID", FromFieldId);
        if Translation.FindSet() then
            repeat
                Set(ToRecVariant, ToFieldId, Translation."Language ID", Translation.Value);
            until Translation.Next() = 0;
    end;

    local procedure DeleteTranslations(RecVariant: Variant; var TranslationWithFilters: Record Translation)
    var
        RecordRef: RecordRef;
    begin
        GetRecordRefFromVariant(RecVariant, RecordRef);

        TranslationWithFilters.SetRange("Table ID", RecordRef.Number());
        TranslationWithFilters.SetRange("System ID", GetSystemIdFromRecordRef(RecordRef));
        TranslationWithFilters.DeleteAll(true);
    end;

    procedure Show(RecVariant: Variant; FieldId: Integer; CheckFieldLength: Boolean)
    var
        Translation: Record Translation;
        TranslationPage: Page Translation;
        SystemID: Guid;
        TableNo: Integer;
    begin
        GetSystemIdFromVariant(RecVariant, SystemID, TableNo);
        Translation.SetRange("System ID", SystemID);
        Translation.SetRange("Field ID", FieldId);
        TranslationPage.SetTableId(TableNo);
        TranslationPage.SetCaption(GetRecordIdCaptionFromVariant(RecVariant));
        TranslationPage.SetTableView(Translation);
        TranslationPage.SetCheckFieldLength(CheckFieldLength);
        TranslationPage.Run();
    end;

    procedure ShowForAllRecords(TableId: Integer; FieldId: Integer)
    var
        Translation: Record Translation;
    begin
        Translation.SetRange("Table ID", TableId);
        Translation.SetRange("Field ID", FieldId);
        Page.Run(Page::Translation, Translation);
    end;

    procedure CheckLengthOfTranslationValue(var Translation: Record Translation)
    var
        Field: Record Field;
    begin
        Field.Get(Translation."Table ID", Translation."Field ID");
        if StrLen(Translation.Value) > Field.Len then
            Error(MaxLenghtErr, Translation.Value, StrLen(Translation.Value), Field.Len);
    end;

    procedure GetTranslations(RecVariant: Variant; FieldId: Integer; var TranslationBuffer: Record "Translation Buffer"): Boolean
    var
        Translation: Record Translation;
        FromRecordRef: RecordRef;
    begin
        TranslationBuffer.Reset();
        TranslationBuffer.DeleteAll();

        GetRecordRefFromVariant(RecVariant, FromRecordRef);
        Translation.SetRange("System ID", GetSystemIdFromRecordRef(FromRecordRef));
        Translation.SetRange("Table ID", FromRecordRef.Number());
        if FieldId <> 0 then
            Translation.SetRange("Field ID", FieldId);
        if Translation.FindSet() then
            repeat
                TranslationBuffer.Init();
                TranslationBuffer.TransferFields(Translation);
                TranslationBuffer.Insert();
            until Translation.Next() = 0;

        exit(TranslationBuffer.FindFirst());
    end;

    local procedure GetRecordIdCaptionFromVariant(RecVariant: Variant): Text
    var
        RecordId: RecordId;
        RecordRef: RecordRef;
    begin
        GetRecordRefFromVariant(RecVariant, RecordRef);
        RecordId := RecordRef.RecordId();
        exit(Format(RecordId, 0, 1));
    end;

    local procedure GetSystemIdFromVariant(RecVariant: Variant; var SystemId: Guid; var TableNo: Integer)
    var
        RecordRef: RecordRef;
    begin
        GetRecordRefFromVariant(RecVariant, RecordRef);
        SystemId := GetSystemIdFromRecordRef(RecordRef);
        TableNo := RecordRef.Number();
    end;

    local procedure GetSystemIdFromRecordRef(RecordRef: RecordRef) SystemId: Guid
    begin
        Evaluate(SystemId, Format(RecordRef.Field(RecordRef.SystemIdNo()).Value())); // TODO: Uptake new method to directly get SystemID from record ref as soon as it is available
    end;

    local procedure GetRecordRefFromVariant(RecVariant: Variant; var RecordRef: RecordRef)
    begin
        if RecVariant.IsRecord() then begin
            RecordRef.GetTable(RecVariant);
            if RecordRef.IsTemporary() then
                Error(CannotTranslateTempRecErr);
            if RecordRef.Number() = 0 then
                Error(NotAValidRecordForTranslationErr, 0);
            exit;
        end;

        if RecVariant.IsRecordRef() then begin
            RecordRef := RecVariant;
            exit;
        end;

        Error(NoRecordIdErr);
    end;
}

Procedures, 14

NameParametersReturnsAccessObsolete
Any()Booleanpublic-
Get(Variant, Integer, Integer, Boolean)Textpublic-
Get(Variant, Integer, Integer)Textpublic-
Set(Variant, Integer, Text[2048])public-
Set(Variant, Integer, Integer, Text[2048])public-
Delete(Variant)public-
Delete(Variant, Integer)public-
ShowForAllRecords(Integer, Integer)public-
Copy(Variant, Variant, Integer)public-
Copy(Variant, Integer, Variant, Integer)public-
Delete(Integer)public-
Show(Variant, Integer, Boolean)public-
CheckLengthOfTranslationValue(var Record Translation)public-
GetTranslations(Variant, Integer, var Record Translation Buffer)Booleanpublic-