11import { window as Window , ProgressLocation } from 'vscode'
22import { DocumentSelector } from 'vscode-languageclient'
3+ import { getZhTranslation } from './baidu'
34
45import
56{
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*/
5266export 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
0 commit comments