Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Google Translate with multiple API support #4

Merged
merged 3 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"unicorn/switch-case-braces": "off",
"valid-jsdoc": "off",
"guard-for-in": "off",
"unicorn/numeric-separators-style": "off",
"unicorn/catch-error-name": [
"error",
{
Expand Down
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ EXAMPLES
$ ctv cache --help
```

_See code: [src/commands/cache.ts](https://github.com/4746/transverto/blob/v1.0.0/src/commands/cache.ts)_
_See code: [src/commands/cache.ts](https://github.com/4746/transverto/blob/v1.1.0/src/commands/cache.ts)_

## `ctv export:csv [LANGCODE]`

Expand Down Expand Up @@ -89,7 +89,7 @@ EXAMPLES
$ ctv export:csv --eol=lf
```

_See code: [src/commands/export/csv.ts](https://github.com/4746/transverto/blob/v1.0.0/src/commands/export/csv.ts)_
_See code: [src/commands/export/csv.ts](https://github.com/4746/transverto/blob/v1.1.0/src/commands/export/csv.ts)_

## `ctv help [COMMANDS]`

Expand Down Expand Up @@ -133,7 +133,7 @@ EXAMPLES
$ ctv init --force
```

_See code: [src/commands/init.ts](https://github.com/4746/transverto/blob/v1.0.0/src/commands/init.ts)_
_See code: [src/commands/init.ts](https://github.com/4746/transverto/blob/v1.1.0/src/commands/init.ts)_

## `ctv label [ADD] [DELETE] [GET] [REPLACE] [SYNC]`

Expand All @@ -154,7 +154,7 @@ DESCRIPTION
Represents a label management command.
```

_See code: [src/commands/label/index.ts](https://github.com/4746/transverto/blob/v1.0.0/src/commands/label/index.ts)_
_See code: [src/commands/label/index.ts](https://github.com/4746/transverto/blob/v1.1.0/src/commands/label/index.ts)_

## `ctv label:add [LABEL]`

Expand Down Expand Up @@ -186,7 +186,7 @@ EXAMPLES
$ ctv label:add "hello.world" -t "Hello World!"
```

_See code: [src/commands/label/add.ts](https://github.com/4746/transverto/blob/v1.0.0/src/commands/label/add.ts)_
_See code: [src/commands/label/add.ts](https://github.com/4746/transverto/blob/v1.1.0/src/commands/label/add.ts)_

## `ctv label:delete LABEL`

Expand All @@ -211,7 +211,7 @@ EXAMPLES
$ ctv label:delete hello.world
```

_See code: [src/commands/label/delete.ts](https://github.com/4746/transverto/blob/v1.0.0/src/commands/label/delete.ts)_
_See code: [src/commands/label/delete.ts](https://github.com/4746/transverto/blob/v1.1.0/src/commands/label/delete.ts)_

## `ctv label:replace LABEL`

Expand Down Expand Up @@ -239,7 +239,7 @@ EXAMPLES
$ ctv label:replace hello.world -t="Hello world!!!" -fen
```

_See code: [src/commands/label/replace.ts](https://github.com/4746/transverto/blob/v1.0.0/src/commands/label/replace.ts)_
_See code: [src/commands/label/replace.ts](https://github.com/4746/transverto/blob/v1.1.0/src/commands/label/replace.ts)_

## `ctv label:sync`

Expand All @@ -263,7 +263,7 @@ EXAMPLES
$ ctv label:sync "hello.world" -f="en"
```

_See code: [src/commands/label/sync.ts](https://github.com/4746/transverto/blob/v1.0.0/src/commands/label/sync.ts)_
_See code: [src/commands/label/sync.ts](https://github.com/4746/transverto/blob/v1.1.0/src/commands/label/sync.ts)_
<!-- commandsstop -->

---
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
],
"author": "Vadim",
"license": "MIT",
"version": "1.0.1",
"version": "1.1.0",
"bugs": "https://github.com/4746/transverto/issues",
"homepage": "https://github.com/4746/transverto",
"repository": "4746/transverto",
Expand Down
70 changes: 70 additions & 0 deletions src/commands/translate/google.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import {Args, Flags} from '@oclif/core'

import {GoogleEngine} from "../../shared/engines/google.engine.js";
import {LabelBaseCommand} from "../../shared/label-base.command.js";
import {TGoogleLangCode, TGoogleLangCodeExtend} from "../../shared/lang.google.js";

/**
* node --loader ts-node/esm ./bin/dev translate:google --help
* node --loader ts-node/esm ./bin/dev translate:google Text
* node --loader ts-node/esm ./bin/dev translate:google Text -tuk
*/
export default class TranslateGoogle extends LabelBaseCommand<typeof TranslateGoogle> {
static args = {
text: Args.string({description: 'The text to be translated, can\'t be blank. The maximum text length is 1000.', required: true}),
}

static description = 'A simple and free API for Google Translator.'

static enableJsonFlag = true;

static examples = [
'<%= config.bin %> <%= command.id %>',
'<%= config.bin %> <%= command.id %> Text -tuk',
'<%= config.bin %> <%= command.id %> Text -tuk --json',
]

static flags = {
from: Flags.string({char: 'f', default: GoogleEngine.defaultFrom, description: 'The language code of source text.'}),
to: Flags.string({char: 't', default: GoogleEngine.defaultTo, description: 'The language in which the text should be translated.'}),
}

static hidden = true;

/**
* source language code. `auto-detect` by default.
*/
private from: TGoogleLangCodeExtend;
/**
* content to be translated
*/
private text: string;
/**
* target language code. `en` by default.
*/
private to: TGoogleLangCode;

public async run(): Promise<NonNullable<object> | void> {
const {args, flags} = await this.parse(TranslateGoogle)

await this.readCliConfig()

this.text = args.text.trim();
this.from = (flags.from || 'auto-detect') as TGoogleLangCodeExtend;
this.to = (flags.to || 'en') as TGoogleLangCode;

const be = new GoogleEngine({...this.cliConfig.google}, flags.userAgent || this.cliConfig.userAgent);

const response = await be.translate({
from: this.from,
text: this.text,
to: this.to,
})

if (this.jsonEnabled()) {
return response;
}

this.log(response.trans);
}
}
7 changes: 7 additions & 0 deletions src/shared/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {DEFAULT_USER_AGENT} from "./constants.js";
import {IBingConfig} from "./entities/bing.config.js";
import {IGoogleConfig} from "./entities/google.config.js";
import {ITerraConfig} from "./entities/terra.config.js";
import {TEngineTranslation} from "./entities/translation.engine.js";

Expand All @@ -21,6 +22,10 @@ export interface IConfig {
bing?: IBingConfig;
engine: TEngineTranslation;
engineUseCache?: false;
/**
* Configuration options for Google setting.
*/
google?: IGoogleConfig;
/**
* Regular expression pattern for validating labels.
*
Expand All @@ -38,6 +43,7 @@ export interface IConfig {
* Configuration options for TerraPrint.
*/
terra?: ITerraConfig;
userAgent: string;
}

export const CONFIG_DEFAULT: IConfig = {
Expand Down Expand Up @@ -82,4 +88,5 @@ export const CONFIG_DEFAULT: IConfig = {
toLangCode: 'uk',
userAgent: DEFAULT_USER_AGENT
},
userAgent: DEFAULT_USER_AGENT
}
Loading