public static bool DoMerge(string newOriginalPath, string newOriginalFormat, string oldOriginalPath, string oldOriginalFormat,
                                   string oldTranslationPath, string oldTranslationFormat, string inputPath, string inputFormat, string outputPath, string outputFormat,
                                   string diffFilePath, string language, bool checkSpecialChar, bool ignoreSame, string checkFilePath)
        {
            SingleLanguageDB input       = new SingleLanguageDB(language);
            SingleLanguageDB newOriginal = new SingleLanguageDB(language);

            Localization.BatchImportToSingleLanguageDB(input, inputPath, inputFormat);
            Localization.BatchImportToSingleLanguageDB(newOriginal, newOriginalPath, newOriginalFormat);

            // optional
            SingleLanguageDB oldOriginal    = null;
            SingleLanguageDB oldTranslation = null;

            if (oldOriginalPath != null && oldOriginalFormat != null)
            {
                oldOriginal = new SingleLanguageDB(language);
                Localization.BatchImportToSingleLanguageDB(oldOriginal, oldOriginalPath, oldOriginalFormat);
            }
            if (oldTranslationPath != null && oldTranslationFormat != null)
            {
                oldTranslation = new SingleLanguageDB(language);
                Localization.BatchImportToSingleLanguageDB(oldTranslation, oldTranslationPath, oldTranslationFormat);
            }

            Dictionary <string, string> RemovedDiffChi = new Dictionary <string, string>();
            Dictionary <string, string> RemovedDiffEng = new Dictionary <string, string>();

            // 1. Remove diff from old translation (but allow them if they are in new original)
            if (oldOriginal != null && oldTranslation != null)
            {
                var diff = Localization.Compare(oldOriginal, newOriginal, false);
                foreach (var entry in diff)
                {
                    string chitext = oldTranslation.LookupText(entry.Tag, entry.Version);
                    string newtext = newOriginal.LookupText(entry.Tag, entry.Version);
                    if (chitext != null && newtext == null)
                    {
                        RemovedDiffChi[entry.Tag] = chitext;
                        RemovedDiffEng[entry.Tag] = entry.OldText;
                        oldTranslation.Remove(entry.Tag, entry.Version);
                    }
                }
            }

            // 2. Merge in old translation to input
            if (oldTranslation != null)
            {
                Localization.MergeIn(input, oldTranslation, LocalizationDB.ImportMode.kIgnore);
            }

            // 3. Find diff entires
            YMLSafeFile diffyml = new YMLSafeFile();

            diffyml.AppendLine(null, -1, GetLanguageTag(language) + ":", null);
            var missing = Localization.GetMissingEntries(newOriginal, input, false);

            foreach (var entry in missing)
            {
                string chi = null;
                if (RemovedDiffChi.ContainsKey(entry.Tag))
                {
                    chi = RemovedDiffChi[entry.Tag];
                }
                string oldeng = null;
                if (RemovedDiffChi.ContainsKey(entry.Tag))
                {
                    oldeng = RemovedDiffEng[entry.Tag];
                }
                diffyml.AppendLine(null, -1, "# Missing. Original: " + entry.NewText, null);
                if (oldeng != null)
                {
                    diffyml.AppendLine(null, -1, "#      old Original: " + oldeng, null);
                }
                diffyml.AppendLine(entry.Tag, entry.Version, chi != null ? chi : entry.OldText, null);
            }
            diffyml.Write(diffFilePath);

            // 3. Checks
            if (checkSpecialChar)
            {
                // check missing entries first as we will remove entries failed the check as well.

                var         check    = Localization.CheckTranslation(newOriginal, input, ignoreSame);
                YMLSafeFile checkyml = new YMLSafeFile();
                checkyml.AppendLine(null, -1, GetLanguageTag(language) + ":", null);

                foreach (var entry in check)
                {
                    if (entry.NewText != null)
                    {
                        checkyml.AppendLine(null, -1, "# Check Fail, Original: " + entry.NewText, null);
                        checkyml.AppendLine(entry.Tag, entry.Version, entry.OldText, null);
                    }
                    // Remove trnaslation, prepare for export
                    input.Remove(entry.Tag, entry.Version);
                }
                checkyml.Write(checkFilePath);
            }

            // 4. Export the merged translation
            return(Localization.BatchExportLocalization(input, newOriginalPath, newOriginalFormat, null, outputPath, outputFormat));
        }
Пример #2
0
        static bool DoRefine(Dictionary <string, string> options)
        {
            string outputPath           = null;
            string outputFormat         = null;
            string oldOriginPath        = null;
            string oldTranslationPath   = null;
            string oldOriginFormat      = null;
            string oldTranslationFormat = null;


            if (!options.TryGetValue("output-path", out outputPath))
            {
                throw new ArgumentException("Missing output-path.");
            }
            if (!options.TryGetValue("output-format", out outputFormat))
            {
                throw new ArgumentException("Missing output-format.");
            }

            if (!options.TryGetValue("old-original-path", out oldOriginPath))
            {
                throw new ArgumentException("Missing old-original-path.");
            }
            if (!options.TryGetValue("old-original-format", out oldOriginFormat))
            {
                throw new ArgumentException("Missing old-original-format.");
            }

            if (!options.TryGetValue("old-translation-path", out oldTranslationPath))
            {
                throw new ArgumentException("Missing old-translation-path.");
            }
            if (!options.TryGetValue("old-translation-format", out oldTranslationFormat))
            {
                throw new ArgumentException("Missing old-translation-format.");
            }


            // all dummy english for now..
            SingleLanguageDB oldEng       = new SingleLanguageDB("english");
            SingleLanguageDB oldTranslate = new SingleLanguageDB("english");

            Localization.BatchImportToSingleLanguageDB(oldEng, oldOriginPath, oldOriginFormat);
            Localization.BatchImportToSingleLanguageDB(oldTranslate, oldTranslationPath, oldTranslationFormat);

            Dictionary <string, string> dict1 = new Dictionary <string, string>();
            Dictionary <string, string> dict2 = new Dictionary <string, string>();

            foreach (var kv in oldEng)
            {
                string tag = kv.Key;
                if ((tag.Length > 2 && tag[1] == '_') || tag.StartsWith("PROV"))
                {
                    continue;
                }

                string value = kv.Value.Last().Value;

                if (tag.ToLowerInvariant().Replace("_", "").Contains(value.ToLowerInvariant().Replace(" ", "")))
                {
                    dict1[tag] = value;
                }
                else
                {
                    TextInfo ti = CultureInfo.CurrentCulture.TextInfo;
                    if (value == ti.ToTitleCase(value))
                    {
                        dict2[tag] = value;
                    }
                }
            }

            YMLSafeFile dict1file = new YMLSafeFile();
            YMLSafeFile dict2file = new YMLSafeFile();

            dict1file.AppendLine(null, -1, "l_english:", null);
            dict2file.AppendLine(null, -1, "l_english:", null);


            foreach (var entry in dict1)
            {
                dict1file.AppendLine(null, -1, "# Potential Term, origin: " + entry.Value, null);
                string trans = oldTranslate.LookupLatestText(entry.Key)?.Item2;
                dict1file.AppendLine(entry.Key, 0, trans != null? trans : entry.Value, null);
            }
            foreach (var entry in dict2)
            {
                dict2file.AppendLine(null, -1, "# Potential Term, origin: " + entry.Value, null);
                string trans = oldTranslate.LookupLatestText(entry.Key)?.Item2;
                dict2file.AppendLine(entry.Key, 0, trans != null ? trans : entry.Value, null);
            }
            dict1file.Write(outputPath + "\\dict1." + dict1file.DefaultExtension());
            dict2file.Write(outputPath + "\\dict2." + dict1file.DefaultExtension());
            return(true);
        }