Skip to main content

DeepL integration (auto translate)

A

A friend of mine told me he use DeepL translation service for his translations. So naturally I implemented it into my app. Here is how I did it.

You have to create account on DeepL, free account is enough, at least for me. Then you need to get your auth key and use it in code below.

    FUNCTION get_live_translation (
        in_text         VARCHAR2,
        in_lang         VARCHAR2
    )
    RETURN VARCHAR2
    AS
        l_secret        CONSTANT VARCHAR2(64) := 'YOUR_AUTH_KEY';
        --
        r               VARCHAR2(4000);
    BEGIN
        r := APEX_WEB_SERVICE.MAKE_REST_REQUEST (
            p_url           => 'https://api-free.deepl.com/v2/translate?auth_key=' || l_secret || '&'
                                || 'text=' || UTL_URL.ESCAPE(in_text) || '&'
                                || 'target_lang=' || in_lang || '&'
                                || 'source_lang=' || 'EN',
            p_http_method   => 'GET'
        );
        RETURN JSON_VALUE(JSON_QUERY(r, '$.translations[0]' RETURNING VARCHAR2(4000) PRETTY), '$.text');
    END;


Lets check the before and after Auto translate button was pressed:


That's it. Simple yet powerful.

You should check short sentences (specially single words) to make sure the meaning is what you intended.

And you might need to setup ACL:

DECLARE
    in_schema               VARCHAR2(30) := USER;
    --
    PROCEDURE create_acl (
        in_acl_name         VARCHAR2,
        in_url              VARCHAR2,
        in_port_lower       NUMBER := NULL,
        in_port_upper       NUMBER := NULL
    )
    AS
    BEGIN
        BEGIN
            DBMS_NETWORK_ACL_ADMIN.DROP_ACL(in_acl_name || '.xml');
        EXCEPTION
        WHEN OTHERS THEN
            NULL;
        END;
        --
        DBMS_NETWORK_ACL_ADMIN.CREATE_ACL (
            acl             => in_acl_name || '.xml',
            description     => '',
            principal       => in_schema,
            is_grant        => true,
            privilege       => 'connect'
        );
        --
        DBMS_NETWORK_ACL_ADMIN.ASSIGN_ACL (
            acl             => in_acl_name || '.xml',
            host            => in_url,
            lower_port      => in_port_lower,
            upper_port      => in_port_upper
        );
    END;
BEGIN
    create_acl('DEEPL', 'api-free.deepl.com', NULL, NULL);
END;
/



Comments