Skip to content

Commit

Permalink
add google api
Browse files Browse the repository at this point in the history
  • Loading branch information
sculove committed Dec 26, 2017
1 parent c661a8b commit 7d9ba7e
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 23 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
26 changes: 17 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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",
Expand All @@ -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.
7 changes: 1 addition & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"
}
]
Expand Down
22 changes: 20 additions & 2 deletions src/translator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ export interface TranslatorConfig {
export class Translator {
public get(text: string): Observable<TranslatorResult> {
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);

Expand Down Expand Up @@ -78,6 +78,24 @@ export class Translator {
}
}

private googleAPI(text: string, config, isKo): Observable<TranslatorResult> {
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<TranslatorResult> {
const body = {
source: isKo ? "ko" : "en",
Expand Down

0 comments on commit 7d9ba7e

Please sign in to comment.