Exemple #1
0
        void GenerateOriginCsvFile(bool isClear)//isClear == false
        {
            Debug.Log("start generate origin csv file...");
            var originCsvFilePath = this.serializedObject.FindProperty("originCsvFilePath").stringValue;

            // 检测原始文件是否已经存在,记录起来做增量判断
            List <string> oldKeys = null;

            if (!isClear)
            {
                using (StreamReader sr = new StreamReader(originCsvFilePath, Encoding.UTF8))
                {
                    if (sr != null)
                    {
                        var content = sr.ReadToEnd();
                        if (content != "") // 空也当做没有
                        {
                            using (CsvReader cr = new CsvReader(new StringReader(content)))
                            {
                                oldKeys = new List <string>();
                                //cr.ReadHeader();
                                while (cr.Read())
                                {
                                    oldKeys.Add(cr[0]);
                                }
                            }
                        }
                    }
                }
            }

            // 解析脚本和ui
            List <string> keys = new List <string>();

            if (!this.GenerateScriptKeys(ref keys))
            {
                return;
            }
            if (!this.GenerateUIKeys(ref keys))
            {
                return;
            }

            using (ProgressIndicator progress = new ProgressIndicator("genreate csv", 2))
            {
                if (oldKeys != null)
                {
                    // 筛选出无用key和增量k
                    progress.Show("remove old keys and add new keys");
                    for (int i = oldKeys.Count - 1; i >= 0; i--)
                    //foreach(var k in oldKeys)
                    {
                        var  oldK   = oldKeys[i];
                        bool isFind = false;
                        foreach (var newK in keys)
                        {
                            if (newK == oldK)
                            {
                                isFind = true;
                                keys.Remove(newK);
                                break;
                            }
                        }

                        // 已经用不到的key就删掉
                        if (!isFind && !oldK.StartsWith("record"))
                        {
                            oldKeys.Remove(oldK);
                        }
                    }
                    progress.Next();
                }

                if (keys.Count > 0)
                {
                    progress.Show("generate csv file");
                    using (CsvWriter cw = new CsvWriter(new StreamWriter(originCsvFilePath, false, Encoding.UTF8)))
                    {
                        cw.WriteHeader(typeof(CsvData));
                        if (oldKeys != null)
                        {
                            // 旧key
                            foreach (var key in oldKeys)
                            {
                                cw.WriteField(key);
                                cw.WriteField("");
                                cw.NextRecord();
                            }
                        }

                        // 新key
                        foreach (var key in keys)
                        {
                            cw.WriteField(key);
                            cw.WriteField("");
                            cw.NextRecord();
                        }

                        cw.WriteField("record" + DateTime.Now.ToString("yyyyMMdd-HHmmss"));
                        cw.WriteField("");
                        cw.NextRecord();
                    }
                    progress.Next();
                }
            }

            AssetDatabase.Refresh(ImportAssetOptions.Default);

            Debug.Log("finished");
        }
Exemple #2
0
        bool TranslatePrefabText()
        {
            // fonts
            var strRefFontRegex = "/Fonts+|/TTF+";
            var guids           = AssetDatabase.FindAssets("t:prefab", new string[] { "Assets/Game/UIs" });
            var strRegex        = this.uiTextRegex.stringValue;

            using (ProgressIndicator progress = new ProgressIndicator("...", guids.Length))
            {
                foreach (var guid in guids)
                {
                    var path   = AssetDatabase.GUIDToAssetPath(guid);
                    var prefab = AssetDatabase.LoadAssetAtPath <GameObject>(path);
                    var texts  = prefab.GetComponentsInChildren <Text>(true);

                    if (progress.Show("translating prefab " + path))
                    {
                        return(false);
                    }

                    bool isAnyChange = false;
                    foreach (var text in texts)
                    {
                        // text regex
                        if (text.text != "" && Regex.IsMatch(text.text, strRegex))
                        {
                            var translateText = I18NConfig.Instance.TranslateText(text.text);
                            if (string.IsNullOrEmpty(translateText))
                            {
                                Debug.LogError("translate text is null or empty, origin text = " + text.text);
                            }
                            else
                            {
                                text.text   = translateText;
                                isAnyChange = true;
                            }
                        }

                        var assetPath = AssetDatabase.GetAssetPath(
                            text.font.GetInstanceID());

                        // font reference
                        if (Regex.IsMatch(assetPath, strRefFontRegex))
                        {
                            var translateAssetPath = assetPath.Replace("Fonts", "Fonts_I18N/zh_cht");
                            translateAssetPath = translateAssetPath.Replace("TTF", "TTF_I18N/zh_cht");
                            var font = AssetDatabase.LoadAssetAtPath <Font>(translateAssetPath);
                            if (font == null)
                            {
                                Debug.LogWarning("not found " + translateAssetPath);
                            }
                            else
                            {
                                text.font   = font;
                                isAnyChange = true;
                            }
                        }
                    }

                    if (isAnyChange)
                    {
                        EditorUtility.SetDirty(prefab);
                    }

                    progress.Next();
                }
            }

            //
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
            Debug.Log("translate i18n prefab finish.");

            return(true);
        }
Exemple #3
0
        bool GenerateScriptKeys(ref List <string> keys)//todo
        {
            var rootPath = this.serializedObject.FindProperty("scriptFileRootPath").stringValue;

            if (rootPath == "" | rootPath.StartsWith("#"))
            {
                return(true);
            }

            var strRegex = this.scriptRegex.stringValue;//todo
            var files    = Directory.GetFiles(
                rootPath, "*.lua", SearchOption.AllDirectories);

            using (ProgressIndicator progress = new ProgressIndicator("generate script keys", files.Length))
            {
                foreach (var file in files)
                {
                    if (file.Contains("i18n_translate.lua"))
                    {
                        continue;
                    }

                    if (progress.Show("generating script:{0}", file))
                    {
                        return(false);
                    }

                    using (StreamReader sr = new StreamReader(file, Encoding.UTF8))
                    {
                        //var content = sr.ReadToEnd();
                        //var matches = Regex.Matches(content, strRegex);
                        //for (int i=0; i<matches.Count; i++)
                        //{
                        //    var match = matches[i];
                        //    if (match.Value != "" && !keys.Contains(match.Value))
                        //        keys.Add(match.Value);
                        //}

                        string line = sr.ReadLine();
                        while (line != null)
                        {
                            var matches = Regex.Matches(line, strRegex);
                            for (int i = 0; i < matches.Count; i++)
                            {
                                var match = matches[i];

                                string fixMatch = match.Value;
                                fixMatch = fixMatch.Remove(0, 1);
                                fixMatch = fixMatch.Substring(0, fixMatch.Length - 1);
                                //Debug.Log(fixMatch);
                                fixMatch = fixMatch.Replace("\\r\\n", "\r\n");
                                fixMatch = fixMatch.Replace("\\n", "\n");
                                if (fixMatch != "" && !keys.Contains(fixMatch))
                                {
                                    keys.Add(fixMatch);
                                }
                            }

                            line = sr.ReadLine();
                        }
                    }

                    progress.Next();
                }
            }

            return(true);
        }