Skip to content

Commit 7d9ba7e

Browse files
committed
add google api
1 parent c661a8b commit 7d9ba7e

File tree

5 files changed

+50
-23
lines changed

5 files changed

+50
-23
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,10 @@ All notable changes to the "translator" extension will be documented in this fil
33

44
Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file.
55

6+
## [1.1.0] - 2017-12-26
7+
- add google translate API (default)
8+
- change shortcut from `cmd + alt + t` to `cmd + shift + t` on MacOS
9+
- change shortcut from `ctrl + alt + t` to `ctrl + shift + t` on Window
10+
611
## [1.0.0] - 2017-12-26
712
- Initial release

README.md

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,16 @@ A vscode plugin for Koreans that can help you write classes, variables, and func
1212
![korToEn](https://github.com/sculove/translator/raw/master/images/korToEn.gif)
1313

1414
> ### Shortcuts
15-
> - MacOS: `Cmd + Alt + t`
16-
> - Window: `Ctrl + Alt + t`
15+
> - MacOS: `Cmd + shift + t`
16+
> - Window: `Ctrl + shift + t`
1717
18+
## Translate API
1819

19-
## Requirements
20-
21-
You need NAVER API key. I have a plan that add some other `translate API` next time.
22-
NAVER API 키가 필요합니다. 향후 다른 `번역 API`도 추가할 예정입니다.
20+
You can use limited Google Translate API. (default)
21+
제한된 Google 번역 API를 사용할 수 있습니다. (default)
2322

23+
If you want to use Papago Translate API of NAVER, you need NAVER API key.
24+
만약 네이버의 `파파고 API`를 사용하고자 한다면, NAVER API 키가 필요합니다.
2425

2526
### Naver API
2627
- Free up to 10,000 per day
@@ -31,14 +32,13 @@ NAVER API 키가 필요합니다. 향후 다른 `번역 API`도 추가할 예정
3132

3233
## Extension Settings
3334

35+
* `translator.type`: translate API type (google, naver). default is google
36+
* `translator.rules`: suggest prefix rules
3437
* `translator.naver.clientId`: Naver API ClientID
3538
* `translator.naver.clientSecret`: Naver API clientSecret
36-
* `translator.rules`: suggest prefix rules
3739

3840
```js
3941
// ...
40-
"translator.naver.clientId": "Naver API clientID",
41-
"translator.naver.clientSecret": "Naver API clientSecret",
4242
"translator.rules": [
4343
{
4444
"prefix": "create",
@@ -58,11 +58,19 @@ NAVER API 키가 필요합니다. 향후 다른 `번역 API`도 추가할 예정
5858
}
5959
// ...
6060
]
61+
"translator.naver.clientId": "Naver API clientID",
62+
"translator.naver.clientSecret": "Naver API clientSecret",
6163
```
6264

6365

6466
## Release Notes
6567

68+
### 1.1.0
69+
70+
- add google translate API (default)
71+
- change shortcut from `cmd + alt + t` to `cmd + shift + t` on MacOS
72+
- change shortcut from `ctrl + alt + t` to `ctrl + shift + t` on Window
73+
6674
### 1.0.0
6775

6876
Initial release of Translator.

package-lock.json

Lines changed: 1 addition & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "translator",
33
"displayName": "translator",
44
"description": "translate for Korean",
5-
"version": "1.0.0",
5+
"version": "1.1.0",
66
"publisher": "sculove",
77
"engines": {
88
"vscode": "^1.18.0"
@@ -38,13 +38,14 @@
3838
"translator.type": {
3939
"type": "string",
4040
"enum": [
41+
"google",
4142
"naver"
4243
],
43-
"default": "naver",
44-
"description": "사용할 번역 API 를 등록해주세요 (naver)"
44+
"default": "google",
45+
"description": "사용할 번역 API 를 등록해주세요 (google, naver)"
4546
},
4647
"translator.naver": {
47-
"type": "object | null",
48+
"type": "object",
4849
"default": {
4950
"clientId": null,
5051
"clientSecret": null
@@ -181,8 +182,8 @@
181182
"keybindings": [
182183
{
183184
"command": "extension.translateForKorean",
184-
"key": "ctrl+alt+t",
185-
"mac": "cmd+alt+t",
185+
"key": "ctrl+shift+t",
186+
"mac": "cmd+shift+t",
186187
"when": "editorHasSelection"
187188
}
188189
]

src/translator.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ export interface TranslatorConfig {
3838
export class Translator {
3939
public get(text: string): Observable<TranslatorResult> {
4040
const config = workspace.getConfiguration("translator");
41-
const hasProperty = Object.keys(config[config.type]).every(v => !!config[config.type][v]);
42-
41+
const apiConfig = config[config.type];
42+
const hasProperty = !apiConfig || Object.keys(apiConfig).every(v => !!apiConfig[v]);
4343
if (hasProperty) {
4444
const isKo = /[-|-|-]/.test(text);
4545

@@ -78,6 +78,24 @@ export class Translator {
7878
}
7979
}
8080

81+
private googleAPI(text: string, config, isKo): Observable<TranslatorResult> {
82+
const source = isKo ? "ko" : "en";
83+
const target = isKo ? "en" : "ko";
84+
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)}`;
85+
86+
return from(fetch(url))
87+
.pipe(
88+
filter((res: Response) => res.ok),
89+
mergeMap((res: Response) => from(res.json())),
90+
map(msg => ({
91+
source,
92+
target,
93+
translatedText: msg.sentences.map(v => v.trans).join("")
94+
})
95+
),
96+
);
97+
}
98+
8199
private naverAPI(text: string, config, isKo): Observable<TranslatorResult> {
82100
const body = {
83101
source: isKo ? "ko" : "en",

0 commit comments

Comments
 (0)