Skip to content

Commit

Permalink
Add "LiberTranslate" provider (#140)
Browse files Browse the repository at this point in the history
- Add "LiberTranslate" provider
- Bump dependencies to the latest version
  • Loading branch information
fabasoad authored Jan 12, 2024
1 parent d3ba8cc commit 61e96b2
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 72 deletions.
59 changes: 50 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ more details for each provider below.
* [Providers](#providers)
* [DeepL](#deepl)
* [Google](#google)
* [LibreTranslate](#libretranslate)
* [Linguatools](#linguatools)
* [Microsoft](#microsoft)
* [MyMemory](#mymemory)
Expand All @@ -32,13 +33,13 @@ more details for each provider below.

## Inputs

| Name | Required | Description | Default | Possible values |
|--------------------------|----------|--------------------------------------------------------------------------------------------------|---------|------------------------------------------------------------------------------------------------------------------------------------------------------|
| source | Yes | Can be text or path to the file for translation | | _<Path>_,_<String>_ |
| provider | Yes | Provider identifier | | [deepl](#deepl), [google](#google), [linguatools](#linguatools), [microsoft](#microsoft), [mymemory](#mymemory), [funtranslations](#funtranslations) |
| api_key | No | API key that should be used for chosen [provider](#providers) | `""` | _<String>_ |
| api_additional_parameter | No | Additional parameter for the API. eg the region for Microsoft: `canadacentral` | `""` | _<String>_ |
| lang | Yes | The translation direction. Should be one of the option proposed by chosen [provider](#providers) | | _<String>_ |
| Name | Required | Description | Default | Possible values |
|--------------------------|----------|--------------------------------------------------------------------------------------------------|---------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| source | Yes | Can be text or path to the file for translation | | _<Path>_,_<String>_ |
| provider | Yes | Provider identifier | | [deepl](#deepl), [google](#google), [libretranslate](#libretranslate), [linguatools](#linguatools), [microsoft](#microsoft), [mymemory](#mymemory), [funtranslations](#funtranslations) |
| api_key | No | API key that should be used for chosen [provider](#providers) | `""` | _<String>_ |
| api_additional_parameter | No | Additional parameter for the API. eg the region for Microsoft: `canadacentral` | `""` | _<String>_ |
| lang | Yes | The translation direction. Should be one of the option proposed by chosen [provider](#providers) | | _<String>_ |

## Outputs

Expand Down Expand Up @@ -68,7 +69,7 @@ jobs:
name: DeepL
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: fabasoad/translation-action@main
id: deepl-step
with:
Expand Down Expand Up @@ -105,7 +106,7 @@ jobs:
name: Google
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: fabasoad/translation-action@main
id: google-step
with:
Expand All @@ -124,6 +125,46 @@ Output is the following:
Translation is 'Victory'
```

### LibreTranslate

* Identifier is `libretranslate`.
* Supported translation directions can be found [here](https://libretranslate.com/languages).
* Be aware that source and target languages should be separated by `-` (hyphen)
character while using them in `lang` input. For example, `en-es` should be used
in case you want to translate text from English into Spanish. See example
below for more details.
* How to get API key:
* Sign up to [LibreTranslate](https://portal.libretranslate.com/).
* Go to `Home -> Get API Key` section

Example of translating "Victory" word from English into Ukrainian:

```yaml
jobs:
libretranslate:
name: LibreTranslate
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: fabasoad/translation-action@main
id: libretranslate-step
with:
provider: libretranslate
lang: en-uk
source: Victory
api_key: ${{ secrets.LIBRETRANSLATE_API_KEY }}
- name: Print the result
run: echo "Translation is '${{ steps.libretranslate-step.outputs.text }}'"
shell: sh
```
Output is the following:
```text
> echo "Translation is 'Перемога'"
Translation is 'Перемога'
```

### Linguatools

* Identifier is `linguatools`. API Key is not needed for this provider.
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@
"@actions/core": "1.10.1",
"cross-fetch": "4.0.0",
"deepl-node": "1.11.0",
"google-translate-api-x": "10.6.7",
"google-translate-api-x": "10.6.8",
"typed-rest-client": "1.8.11"
},
"devDependencies": {
"@microsoft/eslint-formatter-sarif": "3.0.0",
"@types/jest": "29.5.11",
"@typescript-eslint/eslint-plugin": "6.17.0",
"@typescript-eslint/parser": "6.17.0",
"@typescript-eslint/eslint-plugin": "6.18.1",
"@typescript-eslint/parser": "6.18.1",
"@vercel/ncc": "0.38.1",
"dotenv": "16.3.1",
"eslint": "8.56.0",
Expand Down
26 changes: 26 additions & 0 deletions src/providers/LibreTranslateProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import ProviderBase from './ProviderBase'

/* eslint-disable camelcase */
type LibreTranslateResponse = { translatedText: string }

export default class LibreTranslateProvider extends ProviderBase {
private apiKey: string

constructor(apiKey: string) {
super('https://libretranslate.com/translate')
this.apiKey = apiKey
}

translate(text: string, lang: string): Promise<string[]> {
const [source, target] = lang.split('-', 2)
const data = {
q: text,
source,
target,
format: 'text',
api_key: this.apiKey
}
return this.api<LibreTranslateResponse>({ data, url: '/', method: 'POST' })
.then(({ translatedText }) => [translatedText])
}
}
8 changes: 6 additions & 2 deletions src/providers/ProviderFactory.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import ProviderBase from './ProviderBase'
import FunTranslationsProvider from './FunTranslationsProvider'
import LibreTranslateProvider from './LibreTranslateProvider'
import LinguaToolsProvider from './LinguaToolsProvider'
import MicrosoftProvider from './MicrosoftProvider'
import MyMemoryProvider from './MyMemoryProvider'
import DeeplProvider from './DeeplProvider'
import GoogleProvider from './GoogleProvider';
import GoogleProvider from './GoogleProvider'

export type ProviderType =
'deepl' |
'google' |
'funtranslations' |
'linguatools' |
'microsoft' |
'mymemory'
'mymemory' |
'libretranslate'

export default class ProviderFactory {
getProvider(
Expand All @@ -25,6 +27,8 @@ export default class ProviderFactory {
return new GoogleProvider()
case 'funtranslations':
return new FunTranslationsProvider()
case 'libretranslate':
return new LibreTranslateProvider(apiKey)
case 'linguatools':
return new LinguaToolsProvider()
case 'microsoft':
Expand Down
114 changes: 57 additions & 57 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1039,15 +1039,15 @@ __metadata:
languageName: node
linkType: hard

"@typescript-eslint/eslint-plugin@npm:6.17.0":
version: 6.17.0
resolution: "@typescript-eslint/eslint-plugin@npm:6.17.0"
"@typescript-eslint/eslint-plugin@npm:6.18.1":
version: 6.18.1
resolution: "@typescript-eslint/eslint-plugin@npm:6.18.1"
dependencies:
"@eslint-community/regexpp": "npm:^4.5.1"
"@typescript-eslint/scope-manager": "npm:6.17.0"
"@typescript-eslint/type-utils": "npm:6.17.0"
"@typescript-eslint/utils": "npm:6.17.0"
"@typescript-eslint/visitor-keys": "npm:6.17.0"
"@typescript-eslint/scope-manager": "npm:6.18.1"
"@typescript-eslint/type-utils": "npm:6.18.1"
"@typescript-eslint/utils": "npm:6.18.1"
"@typescript-eslint/visitor-keys": "npm:6.18.1"
debug: "npm:^4.3.4"
graphemer: "npm:^1.4.0"
ignore: "npm:^5.2.4"
Expand All @@ -1060,68 +1060,68 @@ __metadata:
peerDependenciesMeta:
typescript:
optional: true
checksum: 44a3c914b72607b12925d07c04be97d325f8795f5d7de8501054a4405accc35b35eaa2aa93983c602d13e842503d49bdbf1f5af5c0a69d700351c005681dcd52
checksum: fbcfae9b92f35ce10212f44f43f93c43f6eb3e28a571da7ed0d424396916aaf080f16ce91a5bffb9e1b42ca2d6003a3e2ad65131b4ef72ed2f94a4bedb35a735
languageName: node
linkType: hard

"@typescript-eslint/parser@npm:6.17.0":
version: 6.17.0
resolution: "@typescript-eslint/parser@npm:6.17.0"
"@typescript-eslint/parser@npm:6.18.1":
version: 6.18.1
resolution: "@typescript-eslint/parser@npm:6.18.1"
dependencies:
"@typescript-eslint/scope-manager": "npm:6.17.0"
"@typescript-eslint/types": "npm:6.17.0"
"@typescript-eslint/typescript-estree": "npm:6.17.0"
"@typescript-eslint/visitor-keys": "npm:6.17.0"
"@typescript-eslint/scope-manager": "npm:6.18.1"
"@typescript-eslint/types": "npm:6.18.1"
"@typescript-eslint/typescript-estree": "npm:6.18.1"
"@typescript-eslint/visitor-keys": "npm:6.18.1"
debug: "npm:^4.3.4"
peerDependencies:
eslint: ^7.0.0 || ^8.0.0
peerDependenciesMeta:
typescript:
optional: true
checksum: 66b53159688083eb48259de5b4daf076f3de284ac3b4d2618bda3f7ab2d8ee27b01ae851b08e8487047e33ff3668424f17d677d66413164cb231f1519dcff82f
checksum: 78cf87c49be224a7fc7c9b1580b015b79e6f6b78d3db60843825b9657e6c5b852566ca7fcb9a51e7b781e910a89a73cdc36dfcd180ccb34febc535ad9b5a0be1
languageName: node
linkType: hard

"@typescript-eslint/scope-manager@npm:6.17.0":
version: 6.17.0
resolution: "@typescript-eslint/scope-manager@npm:6.17.0"
"@typescript-eslint/scope-manager@npm:6.18.1":
version: 6.18.1
resolution: "@typescript-eslint/scope-manager@npm:6.18.1"
dependencies:
"@typescript-eslint/types": "npm:6.17.0"
"@typescript-eslint/visitor-keys": "npm:6.17.0"
checksum: b7ac7d9c39515c2a1b3844577fab967bf126ec25ccf28076240748b3f42d60ab3e64131bfffee61f66251bdf2d59e50e39f5cb0bee7987c85c49140c75d26b5f
"@typescript-eslint/types": "npm:6.18.1"
"@typescript-eslint/visitor-keys": "npm:6.18.1"
checksum: 66ef86688a2eb69988a15d6c0176e5e1ec3994ab96526ca525226a1815eef63366e10e3e6a041ceb2cd63d1cced27874d2313045b785418330af68a288e50771
languageName: node
linkType: hard

"@typescript-eslint/type-utils@npm:6.17.0":
version: 6.17.0
resolution: "@typescript-eslint/type-utils@npm:6.17.0"
"@typescript-eslint/type-utils@npm:6.18.1":
version: 6.18.1
resolution: "@typescript-eslint/type-utils@npm:6.18.1"
dependencies:
"@typescript-eslint/typescript-estree": "npm:6.17.0"
"@typescript-eslint/utils": "npm:6.17.0"
"@typescript-eslint/typescript-estree": "npm:6.18.1"
"@typescript-eslint/utils": "npm:6.18.1"
debug: "npm:^4.3.4"
ts-api-utils: "npm:^1.0.1"
peerDependencies:
eslint: ^7.0.0 || ^8.0.0
peerDependenciesMeta:
typescript:
optional: true
checksum: 15bc9ba2d7f12c3825eced4e5c2283616496e4bca57914c98e895af23d920f94e47e2081fb4fd59da13d274809e08667ae43a76a2f1494a7043c75f980f21114
checksum: 5198752a51649afd960205708c4d765e0170a46a1eb96c97e706890fecb2642933a6377337cf3632f9737915da0201607872a46c9c551d1accf9176b0e025023
languageName: node
linkType: hard

"@typescript-eslint/types@npm:6.17.0":
version: 6.17.0
resolution: "@typescript-eslint/types@npm:6.17.0"
checksum: c458d985b9ab4f369018536bcb88f0aedafb0c8c4b22ffd376e0c0c768a44e3956475c85ebeef40ae44238841c8df268893477b85873aa2621995c37e738e37e
"@typescript-eslint/types@npm:6.18.1":
version: 6.18.1
resolution: "@typescript-eslint/types@npm:6.18.1"
checksum: 58c1a1bcf2403891a4fcb0d21aac643a6f9d06119423230dad74ef2b95adf94201da7cf48617b0c27b51695225b622e48c739cf4186ef5f99294887d2d536557
languageName: node
linkType: hard

"@typescript-eslint/typescript-estree@npm:6.17.0":
version: 6.17.0
resolution: "@typescript-eslint/typescript-estree@npm:6.17.0"
"@typescript-eslint/typescript-estree@npm:6.18.1":
version: 6.18.1
resolution: "@typescript-eslint/typescript-estree@npm:6.18.1"
dependencies:
"@typescript-eslint/types": "npm:6.17.0"
"@typescript-eslint/visitor-keys": "npm:6.17.0"
"@typescript-eslint/types": "npm:6.18.1"
"@typescript-eslint/visitor-keys": "npm:6.18.1"
debug: "npm:^4.3.4"
globby: "npm:^11.1.0"
is-glob: "npm:^4.0.3"
Expand All @@ -1131,34 +1131,34 @@ __metadata:
peerDependenciesMeta:
typescript:
optional: true
checksum: 5a858288bb05f45a2a45b04394115826ff19f85555144bfb67dc281d4e75fc3a1e1aceb3dee68022e86b91f199d1310c15bda3100a4890004b8e474d86afad51
checksum: 5bca8f58d3134c5296c7e6cbeef512feb3918cdc88b5b22e656a7978277278e7a86187690e7e3be3f3708feb98c952a6ab4d8bbc197fff3826e3afa8bc1e287e
languageName: node
linkType: hard

"@typescript-eslint/utils@npm:6.17.0":
version: 6.17.0
resolution: "@typescript-eslint/utils@npm:6.17.0"
"@typescript-eslint/utils@npm:6.18.1":
version: 6.18.1
resolution: "@typescript-eslint/utils@npm:6.18.1"
dependencies:
"@eslint-community/eslint-utils": "npm:^4.4.0"
"@types/json-schema": "npm:^7.0.12"
"@types/semver": "npm:^7.5.0"
"@typescript-eslint/scope-manager": "npm:6.17.0"
"@typescript-eslint/types": "npm:6.17.0"
"@typescript-eslint/typescript-estree": "npm:6.17.0"
"@typescript-eslint/scope-manager": "npm:6.18.1"
"@typescript-eslint/types": "npm:6.18.1"
"@typescript-eslint/typescript-estree": "npm:6.18.1"
semver: "npm:^7.5.4"
peerDependencies:
eslint: ^7.0.0 || ^8.0.0
checksum: a85907c5fbe0a54944fff25df05bf5b8bbe524bb1907fb54c7c68135cf764aa45344e679965c17e235b328ad32e74b1357057c43035203ce874915c4687daa93
checksum: b9dcb2fa7cc8c46254c22fee190032320a5dd8ce282fb01e99cb35da6c00e33b157f4285b062d841942e9aad1d7ce1a16aaa46dd05ca7d81de706aedbbfff396
languageName: node
linkType: hard

"@typescript-eslint/visitor-keys@npm:6.17.0":
version: 6.17.0
resolution: "@typescript-eslint/visitor-keys@npm:6.17.0"
"@typescript-eslint/visitor-keys@npm:6.18.1":
version: 6.18.1
resolution: "@typescript-eslint/visitor-keys@npm:6.18.1"
dependencies:
"@typescript-eslint/types": "npm:6.17.0"
"@typescript-eslint/types": "npm:6.18.1"
eslint-visitor-keys: "npm:^3.4.1"
checksum: 75a48f5810c6a69bc1c082b07d2b840c40895807b1b4ecf9d3ab9eb783176eeb3e7b11eb89d652e8331da79d604f82300f315ffc21cd937819197a8601b48d1d
checksum: f3dacdd1db7347908ac207968da4fa72efb31e38a6dde652651633c5283f054832045f2ad00b4ca7478e7f2e09fe4ae6e3a32b76580c036b9e5c7b8dd55af9f3
languageName: node
linkType: hard

Expand Down Expand Up @@ -2415,10 +2415,10 @@ __metadata:
languageName: node
linkType: hard

"google-translate-api-x@npm:10.6.7":
version: 10.6.7
resolution: "google-translate-api-x@npm:10.6.7"
checksum: cd40d7b9ada9dab1b368b5d5f62f0e41e3c75c7dc0541bb579d40ded778c38ed25800e4dbe70aaba90fae9ec6e610d4cf0a2872d08fa97bffceca559b3b3fdfb
"google-translate-api-x@npm:10.6.8":
version: 10.6.8
resolution: "google-translate-api-x@npm:10.6.8"
checksum: eaaef78c0d51a18bfd6f08bec8b7eefe7249de28527c12e49863d1e98ac44b6b53b1f44f8bb7f47142bf422012c634627d3051e6786b2c290e2ce62c7f359139
languageName: node
linkType: hard

Expand Down Expand Up @@ -4423,15 +4423,15 @@ __metadata:
"@actions/core": "npm:1.10.1"
"@microsoft/eslint-formatter-sarif": "npm:3.0.0"
"@types/jest": "npm:29.5.11"
"@typescript-eslint/eslint-plugin": "npm:6.17.0"
"@typescript-eslint/parser": "npm:6.17.0"
"@typescript-eslint/eslint-plugin": "npm:6.18.1"
"@typescript-eslint/parser": "npm:6.18.1"
"@vercel/ncc": "npm:0.38.1"
cross-fetch: "npm:4.0.0"
deepl-node: "npm:1.11.0"
dotenv: "npm:16.3.1"
eslint: "npm:8.56.0"
eslint-config-google: "npm:0.14.0"
google-translate-api-x: "npm:10.6.7"
google-translate-api-x: "npm:10.6.8"
jest: "npm:29.7.0"
jest-circus: "npm:29.7.0"
mocha-param: "npm:2.0.1"
Expand Down

0 comments on commit 61e96b2

Please sign in to comment.