public string Translate(string content) { if (_settings.ShouldUsePinyin) { var result = _cache[content] as string; if (result == null) { if (WordsHelper.HasChinese(content)) { result = WordsHelper.GetFirstPinyin(content); } else { result = content; } var policy = new CacheItemPolicy(); policy.SlidingExpiration = new TimeSpan(12, 0, 0); _cache.Set(content, result, policy); } return(result); } return(content); }
public string Translate(string content) { if (_settings.ShouldUsePinyin) { if (!_pinyinCache.ContainsKey(content)) { if (WordsHelper.HasChinese(content)) { var result = WordsHelper.GetPinyin(content, ";"); result = GetFirstPinyinChar(result) + result.Replace(";", ""); _pinyinCache[content] = result; return(result); } else { return(content); } } else { return(_pinyinCache[content]); } } else { return(content); } }
public string Translate(string content) { if (_settings.ShouldUsePinyin) { if (!_pinyinCache.ContainsKey(content)) { if (WordsHelper.HasChinese(content)) { var resultList = WordsHelper.GetPinyinList(content); StringBuilder resultBuilder = new StringBuilder(); for (int i = 0; i < resultList.Length; i++) { if (content[i] >= 0x3400 && content[i] <= 0x9FD5) { resultBuilder.Append(resultList[i].First()); } } resultBuilder.Append(' '); bool pre = false; for (int i = 0; i < resultList.Length; i++) { if (content[i] >= 0x3400 && content[i] <= 0x9FD5) { resultBuilder.Append(' '); resultBuilder.Append(resultList[i]); pre = true; } else { if (pre) { pre = false; resultBuilder.Append(' '); } resultBuilder.Append(resultList[i]); } } return(_pinyinCache[content] = resultBuilder.ToString()); } else { return(content); } } else { return(_pinyinCache[content]); } } else { return(content); } }
public void HasChinese() { var b = WordsHelper.HasChinese("xhdsf"); Assert.AreEqual(false, b); var c = WordsHelper.HasChinese("我爱中国"); Assert.AreEqual(true, c); var d = WordsHelper.HasChinese("I爱中国"); Assert.AreEqual(true, d); }
/// <summary> /// 结果管道 /// </summary> /// <param name="result"></param> /// <returns></returns> public string ResultPipe(string result) { if (result.IsNullOrWhiteSpace()) { return(result); } if (Reverse == "all") { result = ReverseStr(result); } else if (Reverse == "row") { result = ReverseStr(result, true); } if (WordsHelper.HasChinese(result)) { if (ToTraditionalChinese) { result = WordsHelper.ToTraditionalChinese(result); } else if (ToSimplifiedChinese) { result = WordsHelper.ToSimplifiedChinese(result); } else if (ToPinyin != null) { result = WordsHelper.GetPinyin(result, ToPinyin.Equals("tone", StringComparison.OrdinalIgnoreCase)); } else if (ToFirstPinyin) { result = WordsHelper.GetFirstPinyin(result)?.ToLower(); } } if (Lower) { result = result?.ToLower(); } else if (Upper) { result = result?.ToUpper(); } if (SpeakWithWhiteSpace) { result = ToSpeakWithWhiteSpace(result); } return(result); }
/// <summary> /// 获取字符串首个字 的 首个字符 /// </summary> /// <param name="source"></param> /// <returns></returns> public static char GetFirstChar(string source, bool isGroupBy = false) { if (string.IsNullOrEmpty(source)) { return isGroupBy ? GROUPBY_CHAR : SORT_CHAR; } var t = source.Substring(0, 1); if (WordsHelper.HasChinese(t)) { return char.Parse((WordsHelper.GetFirstPinYin(t).Substring(0, 1))); } else if (WordsHelper.HasEnglish(t)) return Char.Parse(t.ToUpper()); else return isGroupBy ? GROUPBY_CHAR : SORT_CHAR; }