mirror of
https://github.com/Flow-Launcher/Flow.Launcher.git
synced 2026-03-11 08:54:32 +00:00
62 lines
No EOL
1.7 KiB
C#
62 lines
No EOL
1.7 KiB
C#
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using JetBrains.Annotations;
|
|
using Flow.Launcher.Infrastructure.Logger;
|
|
using Flow.Launcher.Infrastructure.Storage;
|
|
using Flow.Launcher.Infrastructure.UserSettings;
|
|
using ToolGood.Words.Pinyin;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Flow.Launcher.Infrastructure
|
|
{
|
|
public interface IAlphabet
|
|
{
|
|
string Translate(string stringToTranslate);
|
|
}
|
|
|
|
public class Alphabet : IAlphabet
|
|
{
|
|
private ConcurrentDictionary<string, string> _pinyinCache;
|
|
private Settings _settings;
|
|
|
|
public void Initialize([NotNull] Settings settings)
|
|
{
|
|
_settings = settings ?? throw new ArgumentNullException(nameof(settings));
|
|
}
|
|
|
|
|
|
public string Translate(string content)
|
|
{
|
|
if (_settings.ShouldUsePinyin)
|
|
{
|
|
string result = _pinyinCache.GetValueOrDefault(content);
|
|
if (result == null)
|
|
{
|
|
if (WordsHelper.HasChinese(content))
|
|
{
|
|
result = WordsHelper.GetPinyin(content,";");
|
|
result = GetFirstPinyinChar(result) + result.Replace(";","");
|
|
_pinyinCache[content] = result;
|
|
}
|
|
else
|
|
{
|
|
result = content;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
else
|
|
{
|
|
return content;
|
|
}
|
|
}
|
|
|
|
private string GetFirstPinyinChar(string content)
|
|
{
|
|
return string.Concat(content.Split(';').Select(x => x.First()));
|
|
}
|
|
}
|
|
} |