Skip to content

Commit 71bba4f

Browse files
committed
feat: update client with translation api
1 parent c0b1d09 commit 71bba4f

File tree

5 files changed

+75
-9
lines changed

5 files changed

+75
-9
lines changed

client/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,14 @@
1414
"vscode": "^1.52.0"
1515
},
1616
"devDependencies": {
17+
"@types/blueimp-md5": "^2.18.0",
18+
"@types/uuid": "^8.3.1",
1719
"@types/vscode": "1.52.0"
1820
},
1921
"dependencies": {
22+
"axios": "^0.21.4",
23+
"blueimp-md5": "^2.18.0",
24+
"uuid": "^8.3.2",
2025
"vscode-languageclient": "^7.0.0"
2126
}
2227
}

client/src/extension.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as path from 'path';
44
import { ExtensionContext, window as Window, Uri, workspace } from 'vscode';
55
import { ExecuteCommandSignature, LanguageClient, LanguageClientOptions, RevealOutputChannelOn, ServerOptions, TransportKind } from 'vscode-languageclient/node';
66

7-
import { getIntlMessage } from './lib/constant';
7+
import { getIntlMessage, TranslationResultMap } from './lib/constant';
88
import { getIntlConfig, initializeWorkplaceIntlConfig, writeConfigIntoWorkSpace, writeResultIntoIntlConfig } from './lib/file';
99
import { getDocumentSelector, getExistingIntl, getIntlIdWithQuickPick, getTranslateResultsWithProgress, processArgsWithSelectResult } from './lib/util';
1010

@@ -13,7 +13,8 @@ let client: LanguageClient;
1313
export async function activate(context: ExtensionContext): Promise<void>
1414
{
1515
// 指明语言服务器路径
16-
const serverModule = context.asAbsolutePath(path.join('server', 'out', 'server.js'));
16+
// 因为我们只能将 client 目录下的文件作为 extension 发布,所以需要复制 server/out 下的文件至 client/out/server 下
17+
const serverModule = context.asAbsolutePath(path.join('client', 'out', 'server', 'server.js'));
1718

1819
// 国际化配置模版路径
1920
const intlConfigTemp = context.asAbsolutePath(path.join('client', 'src', 'lib', 'intl'))
@@ -115,6 +116,9 @@ export async function activate(context: ExtensionContext): Promise<void>
115116

116117
export function deactivate(): Thenable<void> | undefined
117118
{
119+
// 清除翻译缓存
120+
TranslationResultMap.clear()
121+
118122
if (!client)
119123
{
120124
return undefined;

client/src/lib/constant.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ export const ActivationLanguage = [
1010
"onLanguage:vue"
1111
]
1212

13+
// 翻译结果 Map 缓存,将在 deactive 的时候清除
14+
export const TranslationResultMap = new Map<string, string[]>()
15+
1316
export const getIntlMessage = (intlId: string) => `intl.formatMessage({ id: '${intlId}' })`
1417

15-
export const CUSTOM_INTL_ID_REGX = /[A-Z_]+/
18+
export const CUSTOM_INTL_ID_REGX = /[A-Z_]+/g
1619

1720
// json 文件的缩进数
1821
export const JSON_SPACE = 4
@@ -26,4 +29,8 @@ export const CUSTOM_PICK_PLACEHOLDER = '请选择用来替换的国际化文本
2629
// 自定义国际化输入框 placeholder
2730
export const CUSTOM_INPUT_PLACEHOLDER = '请输入自定义的国际化 id 内容'
2831

29-
export const INVALID_CUSTOM_ID_MESSAGE = '国际化 id 只能由大写字符或下划线组成'
32+
// 自定义 intl id 校验失败信息
33+
export const INVALID_CUSTOM_ID_MESSAGE = '国际化 id 只能由大写字符或下划线组成'
34+
35+
// intl id 中的非法字符
36+
export const INVALID_INTL_ID_CHARACTER = /[^A-Za-z\s]/ig

client/src/lib/util.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { window as Window, ProgressLocation } from 'vscode'
22
import { DocumentSelector } from 'vscode-languageclient'
3+
import { getZhTranslation } from './baidu'
34

45
import
56
{
@@ -9,7 +10,9 @@ import
910
CUSTOM_PICK_OPTION,
1011
CUSTOM_PICK_PLACEHOLDER,
1112
getIntlMessage,
12-
INVALID_CUSTOM_ID_MESSAGE
13+
INVALID_CUSTOM_ID_MESSAGE,
14+
INVALID_INTL_ID_CHARACTER,
15+
TranslationResultMap
1316
} from './constant'
1417

1518
/**
@@ -26,6 +29,17 @@ const getIntlIdCount = (number: number): string =>
2629
return numStr
2730
}
2831

32+
/**
33+
* 去掉翻译结果中的特殊字符串,只保留英文字母和空格作为 intl id
34+
*
35+
* @param {string} translationResult - 翻译结果
36+
* @return {*} {string}
37+
*/
38+
export const getCleanIntlId = (translationResult: string): string =>
39+
{
40+
return translationResult.replace(INVALID_INTL_ID_CHARACTER, '')
41+
}
42+
2943
/**
3044
* 获取 package.json 文件配置的激活事件 activationEvents 对应的语言
3145
*
@@ -51,6 +65,10 @@ export const getDocumentSelector = (): DocumentSelector =>
5165
*/
5266
export const getTranslateResultsWithProgress = async (searchText: string): Promise<string[]> =>
5367
{
68+
// 是否有缓存
69+
const cacheResult = TranslationResultMap.get(searchText)
70+
if (Array.isArray(cacheResult)) return cacheResult
71+
5472
try
5573
{
5674
return await Window.withProgress({
@@ -61,9 +79,9 @@ export const getTranslateResultsWithProgress = async (searchText: string): Promi
6179
{
6280
// 获取供选择的 Options
6381
// 这是最终的返回结果
64-
return await new Promise<string[]>((resolve) =>
65-
setTimeout(() => resolve(['Visual Studio', 'Visual Studio Code']), 1500)
66-
)
82+
const result = await getZhTranslation(searchText)
83+
Array.isArray(result) && result.length && TranslationResultMap.set(searchText, result)
84+
return result
6785
})
6886
}
6987
catch (e: any)
@@ -112,7 +130,7 @@ export const getIntlIdWithQuickPick = async (
112130
intlConfig?: Record<string, string>,
113131
): Promise<[string | undefined, string | undefined]> =>
114132
{
115-
const _intlIdOptions = translateResults.map(tr => tr.toUpperCase().split(' ').join('_'))
133+
const _intlIdOptions = translateResults.map(re => getCleanIntlId(re).toUpperCase().split(' ').join('_'))
116134

117135
let intlId = await Window.showQuickPick([..._intlIdOptions, CUSTOM_PICK_OPTION], { placeHolder: CUSTOM_PICK_PLACEHOLDER })
118136

client/yarn.lock

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,38 @@
22
# yarn lockfile v1
33

44

5+
"@types/blueimp-md5@^2.18.0":
6+
version "2.18.0"
7+
resolved "https://registry.yarnpkg.com/@types/blueimp-md5/-/blueimp-md5-2.18.0.tgz#a5c44fa0a61f5840e95a7965eafcec2c30d6c4df"
8+
integrity sha512-f4A+++lGZGJvVSgeyMkqA7BEf2BVQli6F+qEykKb49c5ieWQBkfpn6CP5c1IZr2Yi2Ofl6Fj+v0e1fN18Z8Cnw==
9+
10+
"@types/uuid@^8.3.1":
11+
version "8.3.1"
12+
resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-8.3.1.tgz#1a32969cf8f0364b3d8c8af9cc3555b7805df14f"
13+
integrity sha512-Y2mHTRAbqfFkpjldbkHGY8JIzRN6XqYRliG8/24FcHm2D2PwW24fl5xMRTVGdrb7iMrwCaIEbLWerGIkXuFWVg==
14+
515
"@types/vscode@1.52.0":
616
version "1.52.0"
717
resolved "https://registry.yarnpkg.com/@types/vscode/-/vscode-1.52.0.tgz#61917968dd403932127fc4004a21fd8d69e4f61c"
818
integrity sha512-Kt3bvWzAvvF/WH9YEcrCICDp0Z7aHhJGhLJ1BxeyNP6yRjonWqWnAIh35/pXAjswAnWOABrYlF7SwXR9+1nnLA==
919

20+
axios@^0.21.4:
21+
version "0.21.4"
22+
resolved "https://registry.yarnpkg.com/axios/-/axios-0.21.4.tgz#c67b90dc0568e5c1cf2b0b858c43ba28e2eda575"
23+
integrity sha512-ut5vewkiu8jjGBdqpM44XxjuCjq9LAKeHVmoVfHVzy8eHgxxq8SbAVQNovDA8mVi05kP0Ea/n/UzcSHcTJQfNg==
24+
dependencies:
25+
follow-redirects "^1.14.0"
26+
1027
balanced-match@^1.0.0:
1128
version "1.0.2"
1229
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.2.tgz#e83e3a7e3f300b34cb9d87f615fa0cbf357690ee"
1330
integrity sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==
1431

32+
blueimp-md5@^2.18.0:
33+
version "2.18.0"
34+
resolved "https://registry.yarnpkg.com/blueimp-md5/-/blueimp-md5-2.18.0.tgz#1152be1335f0c6b3911ed9e36db54f3e6ac52935"
35+
integrity sha512-vE52okJvzsVWhcgUHOv+69OG3Mdg151xyn41aVQN/5W5S+S43qZhxECtYLAEHMSFWX6Mv5IZrzj3T5+JqXfj5Q==
36+
1537
brace-expansion@^1.1.7:
1638
version "1.1.11"
1739
resolved "https://registry.yarnpkg.com/brace-expansion/-/brace-expansion-1.1.11.tgz#3c7fcbf529d87226f3d2f52b966ff5271eb441dd"
@@ -25,6 +47,11 @@ concat-map@0.0.1:
2547
resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b"
2648
integrity sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=
2749

50+
follow-redirects@^1.14.0:
51+
version "1.14.3"
52+
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.14.3.tgz#6ada78118d8d24caee595595accdc0ac6abd022e"
53+
integrity sha512-3MkHxknWMUtb23apkgz/83fDoe+y+qr0TdgacGIA7bew+QLBo3vdgEN2xEsuXNivpFy4CyDhBBZnNZOtalmenw==
54+
2855
lru-cache@^6.0.0:
2956
version "6.0.0"
3057
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-6.0.0.tgz#6d6fe6570ebd96aaf90fcad1dafa3b2566db3a94"
@@ -46,6 +73,11 @@ semver@^7.3.4:
4673
dependencies:
4774
lru-cache "^6.0.0"
4875

76+
uuid@^8.3.2:
77+
version "8.3.2"
78+
resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.3.2.tgz#80d5b5ced271bb9af6c445f21a1a04c606cefbe2"
79+
integrity sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==
80+
4981
vscode-jsonrpc@6.0.0:
5082
version "6.0.0"
5183
resolved "https://registry.yarnpkg.com/vscode-jsonrpc/-/vscode-jsonrpc-6.0.0.tgz#108bdb09b4400705176b957ceca9e0880e9b6d4e"

0 commit comments

Comments
 (0)