Skip to content

Commit 0928b05

Browse files
authored
Merge pull request #235 from taooceros/AddSpaceForPinyin
Add space for pinyin translation
2 parents 920668d + 4c773db commit 0928b05

File tree

1 file changed

+34
-14
lines changed

1 file changed

+34
-14
lines changed

Flow.Launcher.Infrastructure/PinyinAlphabet.cs

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
using System;
22
using System.Collections.Concurrent;
3-
using System.Collections.Generic;
43
using System.Linq;
54
using System.Text;
65
using JetBrains.Annotations;
7-
using Flow.Launcher.Infrastructure.Logger;
8-
using Flow.Launcher.Infrastructure.Storage;
96
using Flow.Launcher.Infrastructure.UserSettings;
107
using ToolGood.Words.Pinyin;
11-
using System.Threading.Tasks;
128

139
namespace 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

Comments
 (0)