@@ -31,12 +31,27 @@ public PinyinAlphabet()
31
31
if ( e . PropertyName == nameof ( Settings . UseDoublePinyin ) ||
32
32
e . PropertyName == nameof ( Settings . DoublePinyinSchema ) )
33
33
{
34
- LoadDoublePinyinTable ( ) ;
35
- _pinyinCache . Clear ( ) ;
34
+ Reload ( ) ;
36
35
}
37
36
} ;
38
37
}
39
38
39
+ public void Reload ( )
40
+ {
41
+ LoadDoublePinyinTable ( ) ;
42
+ _pinyinCache . Clear ( ) ;
43
+ }
44
+
45
+ private void CreateDoublePinyinTableFromStream ( Stream jsonStream )
46
+ {
47
+ Dictionary < string , Dictionary < string , string > > table = JsonSerializer . Deserialize < Dictionary < string , Dictionary < string , string > > > ( jsonStream ) ;
48
+ if ( ! table . TryGetValue ( _settings . DoublePinyinSchema , out var value ) )
49
+ {
50
+ throw new InvalidOperationException ( "DoublePinyinSchema is invalid or double pinyin table is broken." ) ;
51
+ }
52
+ currentDoublePinyinTable = new ReadOnlyDictionary < string , string > ( value ) ;
53
+ }
54
+
40
55
private void LoadDoublePinyinTable ( )
41
56
{
42
57
if ( _settings . UseDoublePinyin )
@@ -45,12 +60,7 @@ private void LoadDoublePinyinTable()
45
60
try
46
61
{
47
62
using var fs = File . OpenRead ( tablePath ) ;
48
- Dictionary < string , Dictionary < string , string > > table = JsonSerializer . Deserialize < Dictionary < string , Dictionary < string , string > > > ( fs ) ;
49
- if ( ! table . TryGetValue ( _settings . DoublePinyinSchema , out var value ) )
50
- {
51
- throw new InvalidOperationException ( "DoublePinyinSchema is invalid." ) ;
52
- }
53
- currentDoublePinyinTable = new ReadOnlyDictionary < string , string > ( value ) ;
63
+ CreateDoublePinyinTableFromStream ( fs ) ;
54
64
}
55
65
catch ( System . Exception e )
56
66
{
@@ -73,7 +83,7 @@ public bool ShouldTranslate(string stringToTranslate)
73
83
74
84
public ( string translation , TranslationMapping map ) Translate ( string content )
75
85
{
76
- if ( ! _settings . ShouldUsePinyin )
86
+ if ( ! _settings . ShouldUsePinyin || ! WordsHelper . HasChinese ( content ) )
77
87
return ( content , null ) ;
78
88
79
89
return _pinyinCache . TryGetValue ( content , out var value )
@@ -83,11 +93,6 @@ public bool ShouldTranslate(string stringToTranslate)
83
93
84
94
private ( string translation , TranslationMapping map ) BuildCacheFromContent ( string content )
85
95
{
86
- if ( ! WordsHelper . HasChinese ( content ) )
87
- {
88
- return ( content , null ) ;
89
- }
90
-
91
96
var resultList = WordsHelper . GetPinyinList ( content ) ;
92
97
93
98
var resultBuilder = new StringBuilder ( ) ;
0 commit comments