Skip to content

Commit

Permalink
refactor: support auto for source language
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed May 30, 2022
1 parent 0882e36 commit 7b5387f
Show file tree
Hide file tree
Showing 27 changed files with 767 additions and 288 deletions.
5 changes: 5 additions & 0 deletions .changeset/fast-jobs-stare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'deepl-translate': minor
---

refactor: support `auto` for source language
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
coverage
lib
CHANGELOG.md
/public/index.html
!/.*.js
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
.*cache
.type-coverage
.vercel
coverage
lib
node_modules
/public/index.html
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ Options:
This will translate a Spanish (`ES`) text into Russian (`RU`):

```sh
deepl -sl spanish -tl russian -t "¡Buenos días!"
deepl -tl russian -t "¡Buenos días!"
```

```plain
Expand All @@ -107,15 +107,15 @@ deepl -sl spanish -tl russian -t "¡Buenos días!"
This will translate the file (`test.txt`) text from Italian (`IT`) into Portuguese (`PT`):

```sh
deepl -sl IT -tl PT -f test.txt
deepl -tl PT -f test.txt
```

#### Example 3

This will translate a Spanish (`ES`) text into Russian (`RU`) in _formal_ tone:

```sh
deepl -sl ES -tl RU --text "¿Cómo te llamas?" --formal
deepl -tl RU --text "¿Cómo te llamas?" --formal
```

```plain
Expand All @@ -129,7 +129,7 @@ Note: _informal_ would be "_Как **тебя** зовут?_"
This will translate a Japanese (`JP`) text into German (`DE`) in _informal_ tone:

```sh
deepl -sl JP -tl DE --text "お元気ですか?" --formal false
deepl -tl DE --text "お元気ですか?" --formal false
```

```plain
Expand All @@ -147,7 +147,7 @@ This will translate a Chinese (`ZH`) text into Dutch (`NL`):
```js
import { translate } from 'deepl-translate'

translate('ZH', 'NL', '你好')
translate('你好', 'NL', 'ZH')
```

```log
Expand All @@ -161,7 +161,7 @@ This will translate a `danish` text into `german` in informal tone:
```js
import { translate } from 'deepl-translate'

translate('danish', 'german', 'Ring til mig!', undefined, undefined, false)
translate('Ring til mig!', 'german', 'danish', undefined, undefined, false)
```

```log
Expand Down
44 changes: 44 additions & 0 deletions api/translate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { VercelRequest, VercelResponse } from '@vercel/node'

import { SourceLanguage, TargetLanguage, translate } from 'deepl-translate'

export interface RequestParams {
text: string
source_lang?: SourceLanguage
target_lang: TargetLanguage
}

const NO_CONTENT = 204
const NOT_ALLOWED = 405

export default async (
req: VercelRequest,
res: VercelResponse,
): Promise<void> => {
if (req.method !== 'POST') {
res.status(NOT_ALLOWED)
res.end()
return
}

const {
text,
source_lang: sourceLang,
target_lang: targetLang,
// type-coverage:ignore-next-line
} = req.body as RequestParams

if (!text) {
res.status(NO_CONTENT)
res.end()
return
}

try {
const translated = await translate(text, targetLang, sourceLang)
res.end(translated)
} catch (err) {
res.status(500)
res.end(String(err))
}
}
22 changes: 22 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Config } from '@jest/types'

const config: Config.InitialOptions = {
preset: 'ts-jest',
testEnvironment: 'node',
collectCoverage: true,
extensionsToTreatAsEsm: ['.ts'],
moduleNameMapper: {
'^(\\.{1,2}/.*)\\.js$': '$1',
'^deepl-translate$': '<rootDir>/src/index.ts',
},
globals: {
'ts-jest': {
useESM: true,
tsconfig: {
importHelpers: false,
},
},
},
}

export default config
16 changes: 10 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@
"build:r": "r -f cjs",
"build:ts": "tsc -p tsconfig.lib.json",
"lint": "run-p lint:*",
"lint:es": "eslint . --max-warnings 10",
"lint:es": "eslint . --cache --max-warnings 10",
"lint:tsc": "tsc --noEmit",
"prepare": "simple-git-hooks || exit 0",
"test": "vitest run --coverage",
"typecov": "type-coverage"
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
"typecov": "type-coverage",
"vercel-build": "npm run build && esno scripts/build"
},
"dependencies": {
"commander": "^9.3.0",
Expand All @@ -53,10 +54,13 @@
"@1stg/lib-config": "^6.1.3",
"@changesets/changelog-github": "^0.4.4",
"@changesets/cli": "^2.22.0",
"c8": "^7.11.3",
"@octokit/request": "^5.6.3",
"@types/jest": "^27.5.1",
"@vercel/node": "^1.15.3",
"esno": "^0.16.3",
"ts-jest": "^28.0.3",
"type-coverage": "^2.21.1",
"typescript": "^4.7.2",
"vitest": "^0.13.0"
"typescript": "^4.7.2"
},
"typeCoverage": {
"atLeast": 100,
Expand Down
Loading

0 comments on commit 7b5387f

Please sign in to comment.