/// <summary>
        /// 添加文件中的原始字符
        /// </summary>
        /// <param name="world"></param>
        public void AddWorld(string world)
        {
            if (ContainWorld(world))
            {
                return;
            }

            TranslatorWorld tw = new TranslatorWorld();

            tw.Source = world;

            worlds.Add(tw);
        }
        /// <summary>
        /// 设置翻译词汇映射
        /// </summary>
        /// <param name="srcWorld">原单词</param>
        /// <param name="translateWorld">翻译后的单词</param>
        public void SetTranslate(string srcWorld, string translateWorld)
        {
            for (int i = 0, max = worlds.Count; i < max; i++)
            {
                if (worlds[i].Source.Equals(srcWorld))
                {
                    worlds[i].Dest = translateWorld;
                    return;
                }
            }

            TranslatorWorld tWorld = new TranslatorWorld();

            tWorld.Source = srcWorld.Trim();
            tWorld.Dest   = translateWorld.Trim();
            worlds.Add(tWorld);
        }
Exemple #3
0
        /// <summary>
        /// 导出源语言
        /// </summary>
        private void exportSourcesLanguage()
        {
            if (curLanguage == targetLanguage || targetLanguage == Language.SimpleChinese)
            {
                return;
            }

            List <TranslateMapper> transMaps = new List <TranslateMapper>();
            List <string>          files     = searchSelectFiles();

            for (int i = 0; i < files.Count; i++)
            {
                string file = files[i];

                ITranslater translater = getTranslater(file);
                if (translater == null)
                {
                    continue;
                }

                transMaps.Add(translater.Export(file.Replace("\\", "/")));

                EditorUtility.DisplayProgressBar("匹配", "正在匹配中文...", i / (float)files.Count);
            }
            EditorUtility.ClearProgressBar();

            //最终把提取的中文生成出来
            string textPath = string.Format(OutPath, outArr[(int)targetLanguage]);

            if (System.IO.File.Exists(textPath))
            {
                //读取已翻译的内容
                string[]        lines     = File.ReadAllLines(textPath);
                string          fileStart = "-------";
                TranslateMapper tm        = null;
                for (int i = 0; i < lines.Length; i++)
                {
                    if (lines[i].StartsWith(fileStart))
                    {
                        string          filePath = lines[i].Replace("-", "").Trim();
                        TranslateMapper hasTm    = null;
                        for (int j = 0; j < transMaps.Count; j++)
                        {
                            if (transMaps[j].FilePath.Equals(filePath))
                            {
                                hasTm = transMaps[j];
                                tm    = hasTm;
                                break;
                            }
                        }

                        if (hasTm == null)
                        {
                            //没找到文件,保留原翻译记录
                            tm = new TranslateMapper(filePath);
                            transMaps.Add(tm);
                        }
                    }
                    else
                    {
                        string[] worlds = lines[i].Trim().Split('=');
                        if (worlds.Length > 1)
                        {
                            tm.SetTranslate(worlds[0].Trim(), worlds[1].Trim());
                        }
                    }
                }
            }

            // 去掉多文件重复
            HashSet <string> map             = new HashSet <string>();
            float            totalWorldCount = 0;

            for (int i = 0, count = transMaps.Count; i < count; i++)
            {
                TranslateMapper tm = transMaps[i];
                for (int j = tm.Worlds.Count - 1; j >= 0; j--)
                {
                    TranslatorWorld transWorld = tm.Worlds[j];
                    if (map.Contains(transWorld.Source))
                    {
                        tm.Worlds.RemoveAt(j);
                    }
                    else
                    {
                        totalWorldCount += 1;
                        map.Add(transWorld.Source);
                    }
                }
            }

            StringBuilder buf             = new StringBuilder();
            int           totalWorldIndex = 0;

            for (int i = 0, count = transMaps.Count; i < count; i++)
            {
                TranslateMapper tm = transMaps[i];
                if (tm.Worlds.Count == 0)
                {
                    continue;
                }

                buf.AppendLine(string.Format("--------------------{0}", tm.FilePath));
                for (int j = 0, worldCount = tm.Worlds.Count; j < worldCount; j++)
                {
                    TranslatorWorld transWorld = tm.Worlds[j];

                    buf.AppendLine(string.Format("{0} = {1}", transWorld.Source, transWorld.Dest));

                    totalWorldIndex++;
                    EditorUtility.DisplayProgressBar("翻译", "正在导出文件...", totalWorldIndex / totalWorldCount);
                }
            }

            using (StreamWriter writer = new StreamWriter(textPath, false, System.Text.Encoding.UTF8))
            {
                writer.Write(buf.ToString());
            }
            EditorUtility.ClearProgressBar();

            string assetFilePath = textPath.Replace(Application.dataPath, "Assets");

            AssetDatabase.ImportAsset(assetFilePath);

            Selection.activeObject = AssetDatabase.LoadAssetAtPath <UnityEngine.Object>(assetFilePath);
            EditorUtility.DisplayDialog("提示", "已经成功导出为:" + languageStrArr[(int)targetLanguage], "确定");
        }