diff --git a/CHANGELOG.md b/CHANGELOG.md index 5a6612d..406ba28 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,5 +3,10 @@ All notable changes to the "translator" extension will be documented in this fil Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file. +## [1.1.0] - 2017-12-26 +- add google translate API (default) +- change shortcut from `cmd + alt + t` to `cmd + shift + t` on MacOS +- change shortcut from `ctrl + alt + t` to `ctrl + shift + t` on Window + ## [1.0.0] - 2017-12-26 - Initial release \ No newline at end of file diff --git a/README.md b/README.md index 003b002..ec6216d 100644 --- a/README.md +++ b/README.md @@ -12,15 +12,16 @@ A vscode plugin for Koreans that can help you write classes, variables, and func ![korToEn](https://github.com/sculove/translator/raw/master/images/korToEn.gif) > ### Shortcuts -> - MacOS: `Cmd + Alt + t` -> - Window: `Ctrl + Alt + t` +> - MacOS: `Cmd + shift + t` +> - Window: `Ctrl + shift + t` +## Translate API -## Requirements - -You need NAVER API key. I have a plan that add some other `translate API` next time. -NAVER API 키가 필요합니다. 향후 다른 `번역 API`도 추가할 예정입니다. +You can use limited Google Translate API. (default) +제한된 Google 번역 API를 사용할 수 있습니다. (default) +If you want to use Papago Translate API of NAVER, you need NAVER API key. +만약 네이버의 `파파고 API`를 사용하고자 한다면, NAVER API 키가 필요합니다. ### Naver API - Free up to 10,000 per day @@ -31,14 +32,13 @@ NAVER API 키가 필요합니다. 향후 다른 `번역 API`도 추가할 예정 ## Extension Settings +* `translator.type`: translate API type (google, naver). default is google +* `translator.rules`: suggest prefix rules * `translator.naver.clientId`: Naver API ClientID * `translator.naver.clientSecret`: Naver API clientSecret -* `translator.rules`: suggest prefix rules ```js // ... - "translator.naver.clientId": "Naver API clientID", - "translator.naver.clientSecret": "Naver API clientSecret", "translator.rules": [ { "prefix": "create", @@ -58,11 +58,19 @@ NAVER API 키가 필요합니다. 향후 다른 `번역 API`도 추가할 예정 } // ... ] + "translator.naver.clientId": "Naver API clientID", + "translator.naver.clientSecret": "Naver API clientSecret", ``` ## Release Notes +### 1.1.0 + +- add google translate API (default) +- change shortcut from `cmd + alt + t` to `cmd + shift + t` on MacOS +- change shortcut from `ctrl + alt + t` to `ctrl + shift + t` on Window + ### 1.0.0 Initial release of Translator. diff --git a/package-lock.json b/package-lock.json index 7a190af..cdd5be7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "translator", - "version": "0.0.1", + "version": "1.0.0", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -599,11 +599,6 @@ "integrity": "sha1-+8cfDEGt6zf5bFd60e1C2P2sypE=", "dev": true }, - "formdata-polyfill": { - "version": "3.0.9", - "resolved": "https://registry.npmjs.org/formdata-polyfill/-/formdata-polyfill-3.0.9.tgz", - "integrity": "sha512-KKTifs9ipLF+y6HUdL1l7njs1he2z8QhX7te+IlX2zm7ItNlaMqnXi2GfJNq8xHFbZ1ZOHMvSBWLYs8jR3XCkA==" - }, "from": { "version": "0.1.7", "resolved": "https://registry.npmjs.org/from/-/from-0.1.7.tgz", diff --git a/package.json b/package.json index 3262a34..59895eb 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "translator", "displayName": "translator", "description": "translate for Korean", - "version": "1.0.0", + "version": "1.1.0", "publisher": "sculove", "engines": { "vscode": "^1.18.0" @@ -38,13 +38,14 @@ "translator.type": { "type": "string", "enum": [ + "google", "naver" ], - "default": "naver", - "description": "사용할 번역 API 를 등록해주세요 (naver)" + "default": "google", + "description": "사용할 번역 API 를 등록해주세요 (google, naver)" }, "translator.naver": { - "type": "object | null", + "type": "object", "default": { "clientId": null, "clientSecret": null @@ -181,8 +182,8 @@ "keybindings": [ { "command": "extension.translateForKorean", - "key": "ctrl+alt+t", - "mac": "cmd+alt+t", + "key": "ctrl+shift+t", + "mac": "cmd+shift+t", "when": "editorHasSelection" } ] diff --git a/src/translator.ts b/src/translator.ts index ebb9cdf..30a215c 100644 --- a/src/translator.ts +++ b/src/translator.ts @@ -38,8 +38,8 @@ export interface TranslatorConfig { export class Translator { public get(text: string): Observable { const config = workspace.getConfiguration("translator"); - const hasProperty = Object.keys(config[config.type]).every(v => !!config[config.type][v]); - + const apiConfig = config[config.type]; + const hasProperty = !apiConfig || Object.keys(apiConfig).every(v => !!apiConfig[v]); if (hasProperty) { const isKo = /[ㄱ-ㅎ|ㅏ-ㅣ|가-힣]/.test(text); @@ -78,6 +78,24 @@ export class Translator { } } + private googleAPI(text: string, config, isKo): Observable { + const source = isKo ? "ko" : "en"; + const target = isKo ? "en" : "ko"; + const url = `https://translate.google.com/translate_a/single?client=gtx&sl=${source}&tl=${target}&dt=t&dt=bd&ie=UTF-8&oe=UTF-8&dj=1&source=icon&q=${encodeURI(text)}`; + + return from(fetch(url)) + .pipe( + filter((res: Response) => res.ok), + mergeMap((res: Response) => from(res.json())), + map(msg => ({ + source, + target, + translatedText: msg.sentences.map(v => v.trans).join("") + }) + ), + ); + } + private naverAPI(text: string, config, isKo): Observable { const body = { source: isKo ? "ko" : "en",