Skip to content

Commit aaa9381

Browse files
committed
Manually add sapce to only the cinese character and the surrounding.
1 parent eaf3267 commit aaa9381

File tree

1 file changed

+38
-4
lines changed

1 file changed

+38
-4
lines changed

Flow.Launcher.Infrastructure/PinyinAlphabet.cs

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Flow.Launcher.Infrastructure.UserSettings;
1010
using ToolGood.Words.Pinyin;
1111
using System.Threading.Tasks;
12+
using Microsoft.AspNetCore.Localization;
1213

1314
namespace Flow.Launcher.Infrastructure
1415
{
@@ -36,10 +37,43 @@ public string Translate(string content)
3637
{
3738
if (WordsHelper.HasChinese(content))
3839
{
39-
var result = WordsHelper.GetPinyin(content, " ");
40-
result = GetFirstPinyinChar(result) + result;
41-
_pinyinCache[content] = result;
42-
return result;
40+
var resultList = WordsHelper.GetPinyinList(content);
41+
42+
List<int> chineseIndexs = new List<int>();
43+
44+
for (int i = 0; i < content.Length; i++)
45+
{
46+
if (resultList[i].Length != 1 || !(resultList[i][0] == content[i]))
47+
chineseIndexs.Add(i);
48+
}
49+
StringBuilder resultBuilder = new StringBuilder();
50+
resultBuilder.Append(string.Concat(resultList.Where((r, i) => chineseIndexs.Contains(i)).Select(s => s.First())));
51+
resultBuilder.Append(' ');
52+
53+
int currentChineseIndex = 0;
54+
int lastChineseIndex = -1;
55+
for (int i = 0; i < resultList.Length; i++)
56+
{
57+
if (currentChineseIndex < chineseIndexs.Count && chineseIndexs[currentChineseIndex] == i)
58+
{
59+
resultBuilder.Append(' ');
60+
61+
resultBuilder.Append(resultList[i]);
62+
currentChineseIndex++;
63+
lastChineseIndex = i;
64+
}
65+
else
66+
{
67+
if (i == lastChineseIndex + 1)
68+
{
69+
resultBuilder.Append(' ');
70+
}
71+
resultBuilder.Append(resultList[i]);
72+
}
73+
}
74+
75+
76+
return _pinyinCache[content] = resultBuilder.ToString();
4377
}
4478
else
4579
{

0 commit comments

Comments
 (0)