Flow.Launcher/Flow.Launcher.Infrastructure/Alphabet.cs

62 lines
1.7 KiB
C#
Raw Normal View History

using System;
2017-01-12 20:46:40 +00:00
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JetBrains.Annotations;
2020-04-21 09:12:17 +00:00
using Flow.Launcher.Infrastructure.Logger;
using Flow.Launcher.Infrastructure.Storage;
using Flow.Launcher.Infrastructure.UserSettings;
2020-10-15 13:06:57 +00:00
using ToolGood.Words.Pinyin;
using System.Threading.Tasks;
2020-04-21 09:12:17 +00:00
namespace Flow.Launcher.Infrastructure
{
public interface IAlphabet
{
string Translate(string stringToTranslate);
}
public class Alphabet : IAlphabet
{
2020-10-15 13:06:57 +00:00
private ConcurrentDictionary<string, string> _pinyinCache;
private Settings _settings;
2020-10-15 13:06:57 +00:00
public void Initialize([NotNull] Settings settings)
{
_settings = settings ?? throw new ArgumentNullException(nameof(settings));
2017-01-12 02:16:53 +00:00
}
2019-11-15 22:34:27 +00:00
2020-10-15 13:06:57 +00:00
public string Translate(string content)
{
2020-10-15 13:06:57 +00:00
if (_settings.ShouldUsePinyin)
{
2020-10-15 13:06:57 +00:00
string result = _pinyinCache.GetValueOrDefault(content);
if (result == null)
{
2020-10-15 13:06:57 +00:00
if (WordsHelper.HasChinese(content))
2017-01-12 20:46:40 +00:00
{
2020-10-15 13:06:57 +00:00
result = WordsHelper.GetPinyin(content,";");
result = GetFirstPinyinChar(result) + result.Replace(";","");
_pinyinCache[content] = result;
}
else
{
2020-10-15 13:06:57 +00:00
result = content;
2017-01-12 20:46:40 +00:00
}
2017-01-12 02:16:53 +00:00
}
2020-10-15 13:06:57 +00:00
return result;
2017-01-12 02:16:53 +00:00
}
2017-01-12 20:46:40 +00:00
else
{
2020-10-15 13:06:57 +00:00
return content;
2017-01-12 20:46:40 +00:00
}
}
2020-10-15 13:06:57 +00:00
private string GetFirstPinyinChar(string content)
{
2020-10-17 06:17:53 +00:00
return string.Concat(content.Split(';').Select(x => x.First()));
}
}
2020-10-15 13:06:57 +00:00
}