Skip to content

Commit

Permalink
Add localization cli command
Browse files Browse the repository at this point in the history
Uses the machine translation API of DeepL to automatically translate any missing values
  • Loading branch information
msujew committed Oct 4, 2021
1 parent 9ed915b commit 5b99efd
Show file tree
Hide file tree
Showing 12 changed files with 448 additions and 1 deletion.
1 change: 1 addition & 0 deletions dev-packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"dependencies": {
"@theia/application-manager": "1.18.0",
"@theia/application-package": "1.18.0",
"@theia/localization-manager": "1.18.0",
"@theia/ovsx-client": "1.18.0",
"@types/chai": "^4.2.7",
"@types/mkdirp": "^0.5.2",
Expand Down
45 changes: 44 additions & 1 deletion dev-packages/cli/src/theia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { ApplicationProps } from '@theia/application-package';
import checkHoisted from './check-hoisting';
import downloadPlugins from './download-plugins';
import runTest from './run-test';
import { LocalizationManager } from '@theia/localization-manager';

process.on('unhandledRejection', (reason, promise) => {
throw reason;
Expand Down Expand Up @@ -106,6 +107,7 @@ function theiaCli(): void {
// affecting the global `yargs` instance used by the CLI.
const { appTarget } = defineCommonOptions(yargsFactory()).help(false).parse();
const manager = new ApplicationPackageManager({ projectPath, appTarget });
const localizationManager = new LocalizationManager();
const { target } = manager.pck;
defineCommonOptions(yargs)
.command<{
Expand Down Expand Up @@ -223,7 +225,48 @@ function theiaCli(): void {
handler: async ({ packed }) => {
await downloadPlugins({ packed });
},
}).command<{
})
.command<{
freeApi?: boolean,
deeplKey: string,
file: string,
languages: string[],
sourceLanguage?: string
}>({
command: 'localize [languages...]',
describe: 'Localize json files using the DeepL API',
builder: {
'file': {
alias: 'f',
describe: 'The source file which should be translated',
demandOption: true
},
'deepl-key': {
alias: 'k',
describe: 'DeepL key used for API access. See https://www.deepl.com/docs-api for more information',
demandOption: true
},
'free-api': {
describe: 'Indicates whether the specified DeepL API key belongs to the free API',
boolean: true,
default: false,
},
'source-language': {
alias: 's',
describe: 'The source language of the translation file'
}
},
handler: async ({ freeApi, deeplKey, file, sourceLanguage, languages = [] }) => {
await localizationManager.localize({
sourceFile: file,
freeApi: freeApi ?? true,
authKey: deeplKey,
targetLanguages: languages,
sourceLanguage
});
}
})
.command<{
testInspect: boolean,
testExtension: string[],
testFile: string[],
Expand Down
3 changes: 3 additions & 0 deletions dev-packages/cli/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
{
"path": "../application-package"
},
{
"path": "../localization-manager"
},
{
"path": "../ovsx-client"
}
Expand Down
13 changes: 13 additions & 0 deletions dev-packages/localization-manager/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/** @type {import('eslint').Linter.Config} */
module.exports = {
extends: [
'../../configs/build.eslintrc.json'
],
parserOptions: {
tsconfigRootDir: __dirname,
project: 'tsconfig.json'
},
rules: {
'import/no-dynamic-require': 'off'
}
};
30 changes: 30 additions & 0 deletions dev-packages/localization-manager/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<div align='center'>

<br />

<img src='https://raw.githubusercontent.com/eclipse-theia/theia/master/logo/theia.svg?sanitize=true' alt='theia-ext-logo' width='100px' />

<h2>ECLIPSE THEIA - LOCALIZATION-MANAGER</h2>

<hr />

</div>

## Description

The `@theia/localization-manager` package is used easily create localizations of Theia and Theia extensions for different languages.
In particular it uses the [DeepL API](https://www.deepl.com/docs-api) to automatically translate any missing localization values.

## Additional Information

- [Theia - GitHub](https://github.com/eclipse-theia/theia)
- [Theia - Website](https://theia-ide.org/)

## License

- [Eclipse Public License 2.0](http://www.eclipse.org/legal/epl-2.0/)
- [一 (Secondary) GNU General Public License, version 2 with the GNU Classpath Exception](https://projects.eclipse.org/license/secondary-gpl-2.0-cp)

## Trademark
"Theia" is a trademark of the Eclipse Foundation
https://www.eclipse.org/theia
44 changes: 44 additions & 0 deletions dev-packages/localization-manager/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"name": "@theia/localization-manager",
"version": "1.18.0",
"description": "Theia localization manager API.",
"publishConfig": {
"access": "public"
},
"license": "EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0",
"repository": {
"type": "git",
"url": "https://github.com/eclipse-theia/theia.git"
},
"bugs": {
"url": "https://github.com/eclipse-theia/theia/issues"
},
"homepage": "https://github.com/eclipse-theia/theia",
"files": [
"lib",
"src"
],
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"scripts": {
"build": "theiaext build",
"clean": "theiaext clean",
"compile": "theiaext compile",
"lint": "theiaext lint",
"test": "theiaext test",
"watch": "theiaext watch"
},
"dependencies": {
"@types/bent": "^7.0.1",
"@types/fs-extra": "^4.0.2",
"bent": "^7.1.0",
"colors": "^1.4.0",
"fs-extra": "^4.0.2"
},
"devDependencies": {
"@theia/ext-scripts": "1.18.0"
},
"nyc": {
"extends": "../../configs/nyc.json"
}
}
107 changes: 107 additions & 0 deletions dev-packages/localization-manager/src/deepl-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/********************************************************************************
* Copyright (C) 2021 TypeFox and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import * as bent from 'bent';

const post = bent('POST', 'json', 200);

export function deepl(
parameters: DeeplParameters
): Promise<DeeplResponse> {
const sub_domain = parameters.free_api ? 'api-free' : 'api';
return post(`https://${sub_domain}.deepl.com/v2/translate`, Buffer.from(toFormData(parameters)), {
'Content-Type': 'application/x-www-form-urlencoded',
'User-Agent': 'Theia-Localization-Manager'
});
}

function toFormData(parameters: DeeplParameters): string {
const str: string[] = [];
for (const [key, value] of Object.entries(parameters)) {
if (typeof value === 'string') {
str.push(encodeURIComponent(key) + '=' + encodeURIComponent(value.toString()));
} else if (Array.isArray(value)) {
for (const item of value) {
str.push(encodeURIComponent(key) + '=' + encodeURIComponent(item.toString()));
}
}
}
return str.join('&');
}

export type DeeplLanguage =
| 'BG'
| 'CS'
| 'DA'
| 'DE'
| 'EL'
| 'EN-GB'
| 'EN-US'
| 'EN'
| 'ES'
| 'ET'
| 'FI'
| 'FR'
| 'HU'
| 'IT'
| 'JA'
| 'LT'
| 'LV'
| 'NL'
| 'PL'
| 'PT-PT'
| 'PT-BR'
| 'PT'
| 'RO'
| 'RU'
| 'SK'
| 'SL'
| 'SV'
| 'ZH';

export const supportedLanguages = [
'BG', 'CD', 'DA', 'DE', 'EL', 'EN-GB', 'EN-US', 'EN', 'ES', 'ET', 'FI', 'FR', 'HU', 'IT',
'JA', 'LT', 'LV', 'NL', 'PL', 'PT-PT', 'PT-BR', 'PT', 'RO', 'RU', 'SK', 'SL', 'SV', 'ZH'
];

export function isSupportedLanguage(language: string): language is DeeplLanguage {
return supportedLanguages.includes(language.toUpperCase());
}

export interface DeeplParameters {
free_api: Boolean
auth_key: string
text: string | string[]
source_lang?: DeeplLanguage
target_lang: DeeplLanguage
split_sentences?: '0' | '1' | 'nonewlines'
preserve_formatting?: '0' | '1'
formality?: 'default' | 'more' | 'less'
tag_handling?: string[]
non_splitting_tags?: string[]
outline_detection?: string
splitting_tags?: string[]
ignore_tags?: string[]
}

export interface DeeplResponse {
translations: DeeplTranslation[]
}

export interface DeeplTranslation {
detected_source_language: string
text: string
}
17 changes: 17 additions & 0 deletions dev-packages/localization-manager/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/********************************************************************************
* Copyright (C) 2021 TypeFox and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

export * from './localization-manager';
Loading

0 comments on commit 5b99efd

Please sign in to comment.