Пример #1
0
        public void Translater(TranslateMapper transMap)
        {
            if (transMap.FilePath.Contains("Editor"))
            {
                return;
            }

            Regex rx = new Regex("[\u4e00-\u9fa5]+");

            string[] lines = File.ReadAllLines(transMap.FilePath);

            Regex reg = new Regex("\"[^\"]*\"");

            for (int i = 0; i < lines.Length; i++)
            {
                if (isFilter(lines[i]))
                {
                    continue;
                }

                MatchCollection mc = reg.Matches(lines[i]);
                foreach (Match m in mc)
                {
                    if (rx.IsMatch(m.Value))
                    {
                        //翻译替换
                        string format = m.Value.Substring(1, m.Value.Length - 2);
                        lines[i] = lines[i].Replace(format, transMap.Translate(format));
                    }
                }
            }

            //保存文件
            File.WriteAllLines(transMap.FilePath, lines);
        }
Пример #2
0
        public TranslateMapper Export(string filePath)
        {
            Regex           rx     = new Regex("[\u4e00-\u9fa5]+");
            TranslateMapper trsMap = new TranslateMapper(filePath);

            string assetPath = filePath.Substring(filePath.IndexOf("Assets/"));

            GameObject prefab   = AssetDatabase.LoadAssetAtPath(assetPath, typeof(GameObject)) as GameObject;
            GameObject instance = GameObject.Instantiate(prefab) as GameObject;

#if NGUI
            UILabel[] labels = instance.GetComponentsInChildren <UILabel>(true);
            for (int q = 0; q < labels.Length; q++)
            {
                string labText = labels[q].text.Trim();
                if (string.IsNullOrEmpty(labText))
                {
                    continue;
                }

                labText = labText.Replace("\n", @"\n").Replace("\r", "");
                if (rx.IsMatch(labText) && !trsMap.ContainWorld(labText))
                {
                    trsMap.AddWorld(labText);
                }
            }

            // UIInput中的默认文本
            UIInput[] inputs = instance.GetComponentsInChildren <UIInput>(true);
            for (int q = 0; q < inputs.Length; q++)
            {
                string text = inputs[q].value.Trim();
                if (string.IsNullOrEmpty(text))
                {
                    continue;
                }

                text = text.Replace("\n", @"\n").Replace("\r", "");
                if (rx.IsMatch(text) && !trsMap.ContainWorld(text))
                {
                    trsMap.AddWorld(text);
                }
            }
#endif
            GameObject.DestroyImmediate(instance);

            return(trsMap);
        }
Пример #3
0
        public void Translater(TranslateMapper transMap)
        {
            Regex rx = new Regex("[\u4e00-\u9fa5]+");

            GameObject prefab   = AssetDatabase.LoadAssetAtPath(transMap.FilePath, typeof(GameObject)) as GameObject;
            GameObject instance = GameObject.Instantiate(prefab) as GameObject;

#if NGUI
            UILabel[] labels = instance.GetComponentsInChildren <UILabel>(true);
            for (int q = 0; q < labels.Length; q++)
            {
                string labText = labels[q].text.Trim();
                if (string.IsNullOrEmpty(labText))
                {
                    continue;
                }

                labText = labText.Replace("\n", @"\n").Replace("\r", "");
                if (rx.IsMatch(labText))    //翻译替换
                {
                    labels[q].text = transMap.Translate(labText);
                }
            }

            // UIInput中的默认文本
            UIInput[] inputs = instance.GetComponentsInChildren <UIInput>(true);
            for (int q = 0; q < inputs.Length; q++)
            {
                string text = inputs[q].value.Trim();
                if (string.IsNullOrEmpty(text))
                {
                    continue;
                }

                text = text.Replace("\n", @"\n").Replace("\r", "");
                if (rx.IsMatch(text))
                {
                    inputs[q].value = transMap.Translate(text);
                }
            }

            PrefabUtility.ReplacePrefab(instance, prefab);
#endif
            GameObject.DestroyImmediate(instance);
        }
Пример #4
0
        public TranslateMapper Export(string filePath)
        {
            TranslateMapper trsMap = new TranslateMapper(filePath);

            if (isFilterFolder(filePath))
            {
                return(trsMap);
            }

            Regex  rx        = new Regex("[\u4e00-\u9fa5]+");
            string assetPath = filePath.Substring(filePath.IndexOf("Assets/"));

            string[] lines = File.ReadAllLines(assetPath);
            //用正则表达式把代码里面两种字符串中间的字符串提取出来。
            Regex reg  = new Regex("\"[^\"]*\"");
            Regex reg2 = new Regex("'[^\']*\'"); //Lua单引号字符

            for (int i = 0; i < lines.Length; i++)
            {
                if (isFilter(lines[i]))
                {
                    continue;
                }

                MatchCollection mc = reg.Matches(lines[i]);
                if (mc.Count == 0)
                {
                    mc = reg2.Matches(lines[i]);
                }
                foreach (Match m in mc)
                {
                    if (rx.IsMatch(m.Value))
                    {
                        string format = m.Value.Substring(1, m.Value.Length - 2);
                        if (!trsMap.ContainWorld(format))
                        {
                            trsMap.AddWorld(format);
                        }
                    }
                }
            }

            return(trsMap);
        }
Пример #5
0
        /// <summary>
        /// 翻译资源
        /// </summary>
        private void onTranlateAssets()
        {
            string textPath = string.Format(OutPath, outArr[(int)targetLanguage]);

            if (!System.IO.File.Exists(textPath))
            {
                EditorUtility.DisplayDialog("错误", "找不到对应的翻译文件,目标语言:" + languageStrArr[(int)targetLanguage], "确定");
                return;
            }

            Dictionary <string, TranslateMapper> transMaps = new Dictionary <string, TranslateMapper>();

            //读取已翻译的内容
            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();
                    tm = new TranslateMapper(filePath);
                    transMaps[tm.FilePath] = tm;
                }
                else
                {
                    string[] worlds = lines[i].Trim().Split('=');
                    if (worlds.Length > 1)
                    {
                        tm.SetTranslate(worlds[0].Trim(), worlds[1].Trim());
                    }
                }
            }

            //翻译转换资源
            List <string> files = searchSelectFiles();

            for (int i = 0; i < files.Count; i++)
            {
                string      file       = files[i];
                ITranslater translater = getTranslater(file);
                if (translater == null)
                {
                    continue;
                }

                string filePath = file.Replace("\\", "/");
                if (!transMaps.ContainsKey(filePath))
                {
                    transMaps[filePath] = new TranslateMapper(filePath);
                }
                translater.Translater(transMaps[filePath]);

                EditorUtility.DisplayProgressBar("翻译", "正在翻译文件...", i / (float)files.Count);
            }
            EditorUtility.ClearProgressBar();

            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();

            EditorUtility.DisplayDialog("提示", "已经完成翻译转换", "确定");
        }
Пример #6
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], "确定");
        }