11using System ;
22using System . Collections . Concurrent ;
3- using System . Collections . Generic ;
43using System . Linq ;
54using System . Text ;
65using JetBrains . Annotations ;
7- using Flow . Launcher . Infrastructure . Logger ;
8- using Flow . Launcher . Infrastructure . Storage ;
96using Flow . Launcher . Infrastructure . UserSettings ;
107using ToolGood . Words . Pinyin ;
11- using System . Threading . Tasks ;
128
139namespace Flow . Launcher . Infrastructure
1410{
@@ -27,7 +23,6 @@ public void Initialize([NotNull] Settings settings)
2723 _settings = settings ?? throw new ArgumentNullException ( nameof ( settings ) ) ;
2824 }
2925
30-
3126 public string Translate ( string content )
3227 {
3328 if ( _settings . ShouldUsePinyin )
@@ -36,10 +31,40 @@ public string Translate(string content)
3631 {
3732 if ( WordsHelper . HasChinese ( content ) )
3833 {
39- var result = WordsHelper . GetPinyin ( content , ";" ) ;
40- result = GetFirstPinyinChar ( result ) + result . Replace ( ";" , "" ) ;
41- _pinyinCache [ content ] = result ;
42- return result ;
34+ var resultList = WordsHelper . GetPinyinList ( content ) ;
35+
36+ StringBuilder resultBuilder = new StringBuilder ( ) ;
37+
38+ for ( int i = 0 ; i < resultList . Length ; i ++ )
39+ {
40+ if ( content [ i ] >= 0x3400 && content [ i ] <= 0x9FD5 )
41+ resultBuilder . Append ( resultList [ i ] . First ( ) ) ;
42+ }
43+
44+ resultBuilder . Append ( ' ' ) ;
45+
46+ bool pre = false ;
47+
48+ for ( int i = 0 ; i < resultList . Length ; i ++ )
49+ {
50+ if ( content [ i ] >= 0x3400 && content [ i ] <= 0x9FD5 )
51+ {
52+ resultBuilder . Append ( ' ' ) ;
53+ resultBuilder . Append ( resultList [ i ] ) ;
54+ pre = true ;
55+ }
56+ else
57+ {
58+ if ( pre )
59+ {
60+ pre = false ;
61+ resultBuilder . Append ( ' ' ) ;
62+ }
63+ resultBuilder . Append ( resultList [ i ] ) ;
64+ }
65+ }
66+
67+ return _pinyinCache [ content ] = resultBuilder . ToString ( ) ;
4368 }
4469 else
4570 {
@@ -56,10 +81,5 @@ public string Translate(string content)
5681 return content ;
5782 }
5883 }
59-
60- private string GetFirstPinyinChar ( string content )
61- {
62- return string . Concat ( content . Split ( ';' ) . Select ( x => x . First ( ) ) ) ;
63- }
6484 }
6585}
0 commit comments