/// <summary>
 /// 转换简体中文和繁体中文字符。
 /// </summary>
 /// <param name="ch"></param>
 /// <param name="direction"></param>
 /// <returns></returns>
 public static char Convert(char ch, ChineseConversionDirection direction)
 {
     ChineseChar result;
     return ChineseChar.TryParse(ch, out result)
         ? (direction == ChineseConversionDirection.SimplifiedToTraditional) ? result.Traditional : result.Simplified
         : ch;
 }
示例#2
0
        public static string ConvertPhrase(string text, ChineseConversionDirection direction)
        {
            string result = string.Empty;

            if (direction == ChineseConversionDirection.SimplifiedToTraditional)
            {
                result = (string)text.Clone();
                foreach (var c in result)
                {
                    var w = $"{c}";
                    if (Tables.s2t.ContainsKey(w))
                    {
                        result = result.Replace(w, Tables.s2t[w]);
                    }
                }
            }
            else if (direction == ChineseConversionDirection.TraditionalToSimplified)
            {
                result = (string)text.Clone();
                foreach (var c in result)
                {
                    var w = $"{c}";
                    if (Tables.t2s.ContainsKey(w))
                    {
                        result = result.Replace(w, Tables.t2s[w]);
                    }
                }
            }
            return(result);
        }
        /// <summary>
        /// 转换简体中文和繁体中文字符。
        /// </summary>
        /// <param name="ch"></param>
        /// <param name="direction"></param>
        /// <returns></returns>
        public static char Convert(char ch, ChineseConversionDirection direction)
        {
            ChineseChar result;

            return(ChineseChar.TryParse(ch, out result)
                ? (direction == ChineseConversionDirection.SimplifiedToTraditional) ? result.Traditional : result.Simplified
                : ch);
        }
示例#4
0
        private void CustomPhrase_Remove(string k, string v, ChineseConversionDirection direction)
        {
            try
            {
                if (direction == ChineseConversionDirection.SimplifiedToTraditional)
                {
                    var items = TableS2T.Where(o => o.Key == k);
                    if (items.Count() > 0)
                    {
                        TableS2T.Remove(items.First());
                    }

                    var kn = k.ToTraditional(true);
                    if (Common.TongWen.Core.CustomPS2T.ContainsKey(kn))
                    {
                        Common.TongWen.Core.CustomPS2T.Remove(kn);
                    }

                    CustomPhraseBefore.Text = string.Empty;
                    CustomPhraseAfter.Text  = string.Empty;
                }
                else if (direction == ChineseConversionDirection.TraditionalToSimplified)
                {
                    if (lvT2S.SelectedItem != null)
                    {
                        var items = TableT2S.Where(o => o.Key == k);
                        if (items.Count() > 0)
                        {
                            TableT2S.Remove(items.First());
                        }

                        var kn = k.ToSimplified(true);
                        if (Common.TongWen.Core.CustomPT2S.ContainsKey(kn))
                        {
                            Common.TongWen.Core.CustomPT2S.Remove(kn);
                        }

                        CustomPhraseBefore.Text = string.Empty;
                        CustomPhraseAfter.Text  = string.Empty;
                    }
                }
                Common.TongWen.Core.SaveCustomPhrase();
            }
            catch (Exception ex)
            {
                ex.Message.ShowMessage("ERROR".T());
            }
        }
示例#5
0
        /// <summary>轉換簡體中文和繁體中文字串。</summary>
        /// <param name="text">需要轉換的字串。</param>
        /// <param name="direction">轉換方式。</param>
        /// <returns>轉換的字串。</returns>
        /// <exception cref="T:System.ArgumentNullException">文本是null。</exception>
        /// <remarks>
        ///   請參閱<see cref="T:Microsoft.International.Converters.TraditionalChineseToSimplifiedConverter.ChineseConverter" />來查看使用中文轉換的例子。
        /// </remarks>
        public static string Convert(string text, ChineseConversionDirection direction)
        {
            if (text == null)
            {
                throw new ArgumentNullException(nameof(text));
            }
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                //OfficeConversionEngine conversionEngine = OfficeConversionEngine.Create();
                //if (conversionEngine != null)
                //    return conversionEngine.TCSCConvert(text, direction);
                uint   dwMapFlags = direction == ChineseConversionDirection.TraditionalToSimplified ? 33554432U : 67108864U;
                int    num1       = text.Length * 2 + 2;
                IntPtr num2       = Marshal.AllocHGlobal(num1);
                NativeMethods.LCMapString(2052, dwMapFlags, text, -1, num2, num1);
                var stringUni = Marshal.PtrToStringUni(num2);
                Marshal.FreeHGlobal(num2);
                return(stringUni);
            }
            switch (direction)
            {
            //内容来自于 https://github.com/BYVoid/OpenCC
            case ChineseConversionDirection.SimplifiedToTraditional:
                if (STCharacters == null)
                {
                    STCharacters = Encoding.UTF8.GetString(Properties.Resources.STCharacters)
                                   .Replace("\r", "").Split('\n').Select(c => c.Split('\t'))
                                   .Where(c => c.Length == 2).ToDictionary(k => k[0], v => v[1].Split(' ')[0]);
                }
                return(FindandReplace(text, STCharacters));

            case ChineseConversionDirection.TraditionalToSimplified:
                if (TSCharacters == null)
                {
                    TSCharacters = Encoding.UTF8.GetString(Properties.Resources.TSCharacters)
                                   .Replace("\r", "").Split('\n').Select(c => c.Split('\t'))
                                   .Where(c => c.Length == 2).ToDictionary(k => k[0], v => v[1].Split(' ')[0]);
                }
                return(FindandReplace(text, TSCharacters));

            default:
                return(null);
            }
        }
        /// <summary>
        /// 转换简体中文和繁体中文字符串。
        /// </summary>
        /// <param name="text"></param>
        /// <param name="direction"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException">参数 <paramref name="text"/> 为 null。</exception>
        public static string Convert(string text, ChineseConversionDirection direction)
        {
            // 本方法代码反编译自 ChineseConverter.dll。

            if (text == null)
            {
                throw new ArgumentNullException("text");
            }
            OfficeConversionEngine engine = OfficeConversionEngine.Create();
            if (engine != null)
            {
                return engine.TCSCConvert(text, direction);
            }
            uint dwMapFlags = (direction == ChineseConversionDirection.TraditionalToSimplified) ? (uint)0x2000000 : (uint)0x4000000;
            int cb = (text.Length * 2) + 2;
            IntPtr lpDestStr = Marshal.AllocHGlobal(cb);
            NativeMethods.LCMapString(0x804, dwMapFlags, text, -1, lpDestStr, cb);
            string str = Marshal.PtrToStringUni(lpDestStr);
            Marshal.FreeHGlobal(lpDestStr);
            return str;
        }
        internal string TCSCConvert(string input, ChineseConversionDirection direction)
        {
            string str2;
            IntPtr zero   = IntPtr.Zero;
            IntPtr handle = IntPtr.Zero;

            try
            {
                IntPtr ptr3;
                int    num;
                zero   = NativeMethods.LoadLibrary(MSOPath);
                handle = NativeMethods.LoadLibrary(Mstr2tscPath);
                if (!NativeMethods.TCSCInitialize())
                {
                    return(null);
                }
                string str = null;
                if (NativeMethods.TCSCConvertText(input, input.Length, out ptr3, out num, direction, false, true))
                {
                    str = Marshal.PtrToStringUni(ptr3);
                    NativeMethods.TCSCFreeConvertedText(ptr3);
                }
                NativeMethods.TCSCUninitialize();
                str2 = str;
            }
            finally
            {
                if (handle != IntPtr.Zero)
                {
                    NativeMethods.FreeLibrary(new HandleRef(this, handle));
                }
                if (zero != IntPtr.Zero)
                {
                    NativeMethods.FreeLibrary(new HandleRef(this, zero));
                }
            }
            return(str2);
        }
        /// <summary>
        /// 转换简体中文和繁体中文字符串。
        /// </summary>
        /// <param name="text"></param>
        /// <param name="direction"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException">参数 <paramref name="text"/> 为 null。</exception>
        public static string Convert(string text, ChineseConversionDirection direction)
        {
            // 本方法代码反编译自 ChineseConverter.dll。

            if (text == null)
            {
                throw new ArgumentNullException("text");
            }
            OfficeConversionEngine engine = OfficeConversionEngine.Create();

            if (engine != null)
            {
                return(engine.TCSCConvert(text, direction));
            }
            uint   dwMapFlags = (direction == ChineseConversionDirection.TraditionalToSimplified) ? (uint)0x2000000 : (uint)0x4000000;
            int    cb         = (text.Length * 2) + 2;
            IntPtr lpDestStr  = Marshal.AllocHGlobal(cb);

            NativeMethods.LCMapString(0x804, dwMapFlags, text, -1, lpDestStr, cb);
            string str = Marshal.PtrToStringUni(lpDestStr);

            Marshal.FreeHGlobal(lpDestStr);
            return(str);
        }
示例#9
0
        public static string Convert(string text, ChineseConversionDirection direction)
        {
            string result = string.Empty;

            if (direction == ChineseConversionDirection.SimplifiedToTraditional)
            {
                result = (string)text.Clone();
                if (result.Length < Tables.s2t.Count / 2.0)
                {
                    foreach (var c in result)
                    {
                        var w = $"{c}";
                        if (Tables.s2t.ContainsKey(w))
                        {
                            result = result.Replace(w, Tables.s2t[w]);
                        }
                    }
                }
                else
                {
                    foreach (var kv in Tables.s2t)
                    {
                        result = result.Replace(kv.Key, kv.Value);
                    }
                }
                foreach (var kv in CustomPS2T)
                {
                    result = result.Replace(kv.Key, kv.Value);
                }
                foreach (var kv in Tables.s2t_custom)
                {
                    result = result.Replace(kv.Key, kv.Value);
                }
                foreach (var kv in Tables.s2t_phrase)
                {
                    result = result.Replace(kv.Key, kv.Value);
                }
                foreach (var kv in Tables.s2t_symbol)
                {
                    result = result.Replace(kv.Key, kv.Value);
                }
            }
            else if (direction == ChineseConversionDirection.TraditionalToSimplified)
            {
                result = (string)text.Clone();
                if (result.Length < Tables.t2s.Count / 2.0)
                {
                    foreach (var c in result)
                    {
                        var w = $"{c}";
                        if (Tables.t2s.ContainsKey(w))
                        {
                            result = result.Replace(w, Tables.t2s[w]);
                        }
                    }
                }
                else
                {
                    foreach (var kv in Tables.t2s)
                    {
                        result = result.Replace(kv.Key, kv.Value);
                    }
                }
                foreach (var kv in CustomPT2S)
                {
                    result = result.Replace(kv.Key, kv.Value);
                }
                foreach (var kv in Tables.t2s_custom)
                {
                    result = result.Replace(kv.Key, kv.Value);
                }
                foreach (var kv in Tables.t2s_phrase)
                {
                    result = result.Replace(kv.Key, kv.Value);
                }
                foreach (var kv in Tables.t2s_symbol)
                {
                    result = result.Replace(kv.Key, kv.Value);
                }
            }
            return(result);
        }
示例#10
0
        private async void CustomPhrase_Add(string k, string v, ChineseConversionDirection direction, bool confirm = false)
        {
            try
            {
                var kv = new KeyValuePair <string, string>(k, v);

                if (direction == ChineseConversionDirection.SimplifiedToTraditional)
                {
                    var kn = kv.Key.ToTraditional(true);
                    if (Common.TongWen.Core.CustomPS2T.ContainsKey(kn))
                    {
                        var result = DialogResult.YES;
                        if (confirm)
                        {
                            result = await "PhraseExistsConfirm".T().ShowConfirm("CONFIRM".T());
                        }
                        IEnumerable <KeyValuePair <string, string> > items = TableS2T.Where(o => o.Key == k);
                        if (items.Count() > 0)
                        {
                            var idx = TableS2T.IndexOf(items.First());
                            lvS2T.SelectedIndex = idx;
                            lvS2T.ScrollIntoView(items.First());

                            if (result == DialogResult.YES)
                            {
                                TableS2T.Insert(lvS2T.SelectedIndex, kv);
                                TableS2T.RemoveAt(lvS2T.SelectedIndex);
                                Common.TongWen.Core.CustomPS2T[kn] = v;
                            }
                        }
                    }
                    else
                    {
                        TableS2T.Add(kv);
                        Common.TongWen.Core.CustomPS2T.Add(kn, kv.Value);
                    }
                }
                else if (direction == ChineseConversionDirection.TraditionalToSimplified)
                {
                    var kn = kv.Key.ToSimplified(true);
                    if (Common.TongWen.Core.CustomPT2S.ContainsKey(kn))
                    {
                        var result = DialogResult.YES;
                        if (confirm)
                        {
                            result = await "PhraseExistsConfirm".T().ShowConfirm("CONFIRM".T());
                        }
                        IEnumerable <KeyValuePair <string, string> > items = TableT2S.Where(o => o.Key == k);
                        if (items.Count() > 0)
                        {
                            var idx = TableT2S.IndexOf(items.First());
                            lvT2S.SelectedIndex = idx;
                            lvT2S.ScrollIntoView(items.First());

                            if (result == DialogResult.YES)
                            {
                                TableT2S.Insert(lvT2S.SelectedIndex, kv);
                                TableT2S.RemoveAt(lvT2S.SelectedIndex);
                                Common.TongWen.Core.CustomPT2S[kn] = v;
                            }
                        }
                    }
                    else
                    {
                        TableT2S.Add(kv);
                        Common.TongWen.Core.CustomPT2S.Add(kn, kv.Value);
                    }
                }
                Common.TongWen.Core.SaveCustomPhrase();
            }
            catch (Exception ex)
            {
                ex.Message.ShowMessage("ERROR".T());
            }
        }
示例#11
0
        private void CustomPhrase_Edit(string k, string v, ChineseConversionDirection direction)
        {
            try
            {
                if (direction == ChineseConversionDirection.SimplifiedToTraditional)
                {
                    var kv = new KeyValuePair <string, string>(k, v);

                    var items = TableS2T.Where(o => o.Key == k);
                    if (items.Count() > 0)
                    {
                        var idx = TableS2T.IndexOf(items.First());
                        TableS2T.Insert(idx, kv);
                        TableS2T.RemoveAt(idx);
                    }

                    var ko = TableS2T[lvS2T.SelectedIndex].Key.ToTraditional(true);
                    var kn = k.ToTraditional(true);


                    if (Common.TongWen.Core.CustomPS2T.ContainsKey(kn))
                    {
                        Common.TongWen.Core.CustomPS2T[kn] = v;
                    }
                    else
                    {
                        if (Common.TongWen.Core.CustomPS2T.ContainsKey(ko))
                        {
                            Common.TongWen.Core.CustomPS2T.Remove(ko);
                        }
                        Common.TongWen.Core.CustomPS2T.TryAdd(kn, v);
                    }
                }
                else if (direction == ChineseConversionDirection.TraditionalToSimplified)
                {
                    if (lvT2S.SelectedItem != null)
                    {
                        var kv = new KeyValuePair <string, string>(k, v);

                        var items = TableT2S.Where(o => o.Key == k);
                        if (items.Count() > 0)
                        {
                            var idx = TableT2S.IndexOf(items.First());
                            TableT2S.Insert(idx, kv);
                            TableT2S.RemoveAt(idx);
                        }

                        var ko = TableT2S[lvT2S.SelectedIndex].Key.ToSimplified(true);
                        var kn = k.ToSimplified(true);

                        if (Common.TongWen.Core.CustomPT2S.ContainsKey(kn))
                        {
                            Common.TongWen.Core.CustomPT2S[kn] = v;
                        }
                        else
                        {
                            if (Common.TongWen.Core.CustomPT2S.ContainsKey(ko))
                            {
                                Common.TongWen.Core.CustomPT2S.Remove(ko);
                            }
                            Common.TongWen.Core.CustomPT2S.TryAdd(kn, v);
                        }
                    }
                }
                Common.TongWen.Core.SaveCustomPhrase();
            }
            catch (Exception ex)
            {
                ex.Message.ShowMessage("ERROR".T());
            }
        }
示例#12
0
 internal static extern bool TCSCConvertText([MarshalAs(UnmanagedType.LPTStr)] string pwszInput, int cchInput, out IntPtr ppwszOutput, out int pcchOutput, ChineseConversionDirection dwDirection, [MarshalAs(UnmanagedType.Bool)] bool fCharBase, [MarshalAs(UnmanagedType.Bool)] bool fLocalTerm);
 internal static extern bool TCSCConvertText([MarshalAs(UnmanagedType.LPTStr)] string pwszInput, int cchInput, out IntPtr ppwszOutput, out int pcchOutput, ChineseConversionDirection dwDirection, [MarshalAs(UnmanagedType.Bool)] bool fCharBase, [MarshalAs(UnmanagedType.Bool)] bool fLocalTerm);
示例#14
0
 public static string ConverterChinese(string text, ChineseConversionDirection direction)
 {
     return(ChineseConverter.Convert(text, direction));
 }