Skip to content

Commit aa3177d

Browse files
authored
Merge pull request #183 from taooceros/PinyinLibraryChange
Pinyin library change
2 parents e866820 + da801f8 commit aa3177d

File tree

5 files changed

+69
-183
lines changed

5 files changed

+69
-183
lines changed

Flow.Launcher.Infrastructure/Alphabet.cs

Lines changed: 0 additions & 178 deletions
This file was deleted.

Flow.Launcher.Infrastructure/Flow.Launcher.Infrastructure.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@
5353
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
5454
<PackageReference Include="NLog.Schema" Version="4.7.0-rc1" />
5555
<PackageReference Include="NLog.Web.AspNetCore" Version="4.9.0" />
56-
<PackageReference Include="Pinyin4DotNet" Version="2016.4.23.4" />
5756
<PackageReference Include="System.Drawing.Common" Version="4.7.0" />
5857
<PackageReference Include="System.Runtime" Version="4.3.1" />
5958
<PackageReference Include="PropertyChanged.Fody" Version="2.5.13" />
59+
<PackageReference Include="ToolGood.Words.Pinyin" Version="3.0.1.4" />
6060
</ItemGroup>
6161

6262
<ItemGroup>
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System;
2+
using System.Collections.Concurrent;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using JetBrains.Annotations;
7+
using Flow.Launcher.Infrastructure.Logger;
8+
using Flow.Launcher.Infrastructure.Storage;
9+
using Flow.Launcher.Infrastructure.UserSettings;
10+
using ToolGood.Words.Pinyin;
11+
using System.Threading.Tasks;
12+
13+
namespace Flow.Launcher.Infrastructure
14+
{
15+
public interface IAlphabet
16+
{
17+
string Translate(string stringToTranslate);
18+
}
19+
20+
public class PinyinAlphabet : IAlphabet
21+
{
22+
private ConcurrentDictionary<string, string> _pinyinCache = new ConcurrentDictionary<string, string>();
23+
private Settings _settings;
24+
25+
public void Initialize([NotNull] Settings settings)
26+
{
27+
_settings = settings ?? throw new ArgumentNullException(nameof(settings));
28+
}
29+
30+
31+
public string Translate(string content)
32+
{
33+
if (_settings.ShouldUsePinyin)
34+
{
35+
if (!_pinyinCache.ContainsKey(content))
36+
{
37+
if (WordsHelper.HasChinese(content))
38+
{
39+
var result = WordsHelper.GetPinyin(content, ";");
40+
result = GetFirstPinyinChar(result) + result.Replace(";", "");
41+
_pinyinCache[content] = result;
42+
return result;
43+
}
44+
else
45+
{
46+
return content;
47+
}
48+
}
49+
else
50+
{
51+
return _pinyinCache[content];
52+
}
53+
}
54+
else
55+
{
56+
return content;
57+
}
58+
}
59+
60+
private string GetFirstPinyinChar(string content)
61+
{
62+
return string.Concat(content.Split(';').Select(x => x.First()));
63+
}
64+
}
65+
}

Flow.Launcher/App.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public partial class App : IDisposable, ISingleInstanceApp
2929
private SettingWindowViewModel _settingsVM;
3030
private readonly Updater _updater = new Updater(Flow.Launcher.Properties.Settings.Default.GithubRepo);
3131
private readonly Portable _portable = new Portable();
32-
private readonly Alphabet _alphabet = new Alphabet();
32+
private readonly PinyinAlphabet _alphabet = new PinyinAlphabet();
3333
private StringMatcher _stringMatcher;
3434

3535
[STAThread]

Flow.Launcher/PublicAPIInstance.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ public class PublicAPIInstance : IPublicAPI
2121
{
2222
private readonly SettingWindowViewModel _settingsVM;
2323
private readonly MainViewModel _mainVM;
24-
private readonly Alphabet _alphabet;
24+
private readonly PinyinAlphabet _alphabet;
2525

2626
#region Constructor
2727

28-
public PublicAPIInstance(SettingWindowViewModel settingsVM, MainViewModel mainVM, Alphabet alphabet)
28+
public PublicAPIInstance(SettingWindowViewModel settingsVM, MainViewModel mainVM, PinyinAlphabet alphabet)
2929
{
3030
_settingsVM = settingsVM;
3131
_mainVM = mainVM;
@@ -76,7 +76,6 @@ public void SaveAppAllSettings()
7676
_settingsVM.Save();
7777
PluginManager.Save();
7878
ImageLoader.Save();
79-
_alphabet.Save();
8079
}
8180

8281
public void ReloadAllPluginData()

0 commit comments

Comments
 (0)