Skip to content

Commit

Permalink
Initial translation support
Browse files Browse the repository at this point in the history
  • Loading branch information
Armored-Dragon committed Nov 26, 2024
1 parent 7c84cbf commit 4e6c6af
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 0 deletions.
56 changes: 56 additions & 0 deletions scripts/modules/translation/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Translation Module

This module provides an easy way to preform translation tasks using http services.

## Including the module
You can use this translation module by using [Script.include](https://apidocs.overte.org/Script.html#.include) at the top of your file.

```javascript
Script.include('/~/modules/translation/translation.js');
```

## Using the module
This module exposes the `TranslationModule` object.

Supported functions are:
```javascript
TranslationModule.translate(
textToTranslate, // The text string to translate
targetLanguageCode, // Target language to translate to, as a locale code. ("en" is English, "es" is Spanish, .etc)
sourceLanguageCode // Optional. The source language of the message. This ensures that the translation service knows what the source language is if it is supported.
)
```


## Supported services
`LibreTranslate` - [LibreTranslate](https://github.com/LibreTranslate/LibreTranslate) is a self hosted translation service

## Settings
`General`:
- `enabled` - Whether to enable the translation functionality globally or not.

`LibreTranslate`:
- `serverURL` - The hostname of the LibreTranslate instance to be used when making a translation request

## Development
### Settings format
When interfacing with the Overte Settings api. make sure to follow the format `translate/<service>/<specific>`
For example, if you want to adjust a setting exposed to the "LibreTranslate" service, you would use `translate/libreTranslate/<specific>`. Please note that the settings are registered using standard camel case formatting.

There may be specific settings that do not include a "service" and instead simply use the `translate/<specific>` format. For example: `translate/enabled`.

### Adding a service
The intended way to add services to this module is to create a function named after the service, followed by the word "request".
Example: The service `LibreTranslate` turns into `libreTranslateRequest(...)`.

### Calling a service
Each service is intended to get the same information as every other. The inputs will be the text to translate, the language intended to be translated to, and the source language. Some services may not accept a source language and it is ignored when calling the service. See "Using the module" for the specific inputs.

When returning data retrieved from a service, it should be returned in an object in the following format:
```javascript
{
success: bool, // Success state of the whole translation request.
text: string, // Translated text upon success, otherwise undefined.
error: string // Error reason, otherwise undefined.
}
```
67 changes: 67 additions & 0 deletions scripts/modules/translation/translation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//
// translation.js
//
// Created by Armored Dragon in November 2024
// Copyright 2024, Overte e.V.
//
// This module is used to request translations from webservers
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html

// TODO: Libretranslate API key support
// TODO: Default service
// TODO: Override default service
// TODO: Detect language

const TranslationModule = {
translate: async (text = "", targetLanguage = "", sourceLanguage = "auto") => {
if (Settings.getValue('translate/enabled', false) == false) return; // Translation disabled

let translationResponse = await libreTranslateRequest(sourceLanguage, targetLanguage, text);
return translationResponse;
}
}

/*
When creating new translation services, always be sure you are returning the same exact response.
The response should be something like this:
{
success: bool, // Success state of the whole translation request.
text: string, // Translated text upon success, otherwise undefined.
error: string, // Error reason, otherwise undefined.
}
*/

function libreTranslateRequest(sourceLanguage, targetLanguage, text){
const url = Settings.getValue('translate/libreTranslate/serverURL');

const packet = {
q: text,
source: sourceLanguage,
target: targetLanguage,
format: "text",
alternatives: 0,
}
return new Promise((resolve, reject) => {
if (sourceLanguage == targetLanguage) return reject({success: false, error: "Can not translate from and to the same language."}); // No need to translate
if (text == "") return reject({success: false, error: "No text to translate provided."}); // Nothing to translate
if (targetLanguage == "") return reject({success: false, error: "No target language provided."}); // We don't know what to translate to

var req = new XMLHttpRequest();
req.open("POST", url, true);
req.setRequestHeader('Content-Type', 'application/json');
req.onreadystatechange = function () {
if (req.readyState === 4) {
if (req.status === 200) {
const parsed = JSON.parse(req.responseText);
return resolve({success: true, text: parsed.translatedText});
}

return resolve({success: false, error: `Unexpected HTTP code: ${req.status}`});
}
};
req.send(JSON.stringify(packet));
})
}

0 comments on commit 4e6c6af

Please sign in to comment.