The goal is to create a service that returns the equivalent string in another language for a given string in one language, provided that the external translation API used supports those languages:
- There should be an implementation for integrating DeepL.
- Since costs are incurred per translated character:
- the service counts and limits them;
- the API key is not provided,
- the service is disabled by default.
In addition to the service, the following are required:
- (one) debug configuration for translating a given *.properties file
- (an) Ant task for translating all *.properties files(for now, in the src/META-INF folder)
In the event of an interruption in the middle of a file, both mechanisms save the lines that have already been translated.
Configuration
Enable the service in the application’s *.config.xml file: {{{#!xml <config service-class="com.top_logic.basic.module.ModuleSystem"> <instance>
<modules>
<module key="com.top_logic.basic.translation.TranslationService$Module" value="true" />
</modules>
</instance> </config> }}}
Configuration of the integration (in this example, with DeepL): {{{#!xml <config service-class="com.top_logic.basic.translation.TranslationService"> <instance class="com.top_logic.basic.translation.DeepLTranslationService"
api-host="https://api.deepl.com/v2/translate"
api-key="%TRANSLATION_API_KEY%"
max-accumulated-translation-size="2500"
max-request-size="30k"
split-sentences="nonewlines"
tag-handling="xml"
/> </config> }}} See also tl:DeepLTranslationService
Please note the following regarding the individual parameters:
- api-key: Internally, the configuration has access to the alias %TRANSLATION_API_KEY%, so you don’t need to change anything. For the application, you must obtain an API key from DeepL and store it *encrypted* in the alias in the application configuration:
{{{#!xml <alias> <entry name="%TRANSLATION_API_KEY%" value="..." /> </alias> }}}
- max-accumulated-translation-size is the upper limit on the number of characters that can be translated. The counter is set to 0 when the application starts and is reset every night thereafter.
API
Call one of the translate methods {{{#!java TranslationService.getInstance().translate(...); }}} See tl:TranslationService
Test
The TestDeepLTranslationService test case checks the service’s internal functions responsible for parsing the JSON strings returned by the API. Testing the translation itself was omitted to avoid incurring costs with every test run.