コード例 #1
0
        /// <summary>
        /// Find missing entries in target that are in src.
        /// Can be used to detect missing translation.
        /// </summary>
        /// <param name="src"></param>
        /// <param name="target"></param>
        /// <returns>tag, ver, missing text, potential text</returns>
        static public List <DiffResultEntry> GetMissingEntries(SingleLanguageDB src, SingleLanguageDB target, bool onlyLatest)
        {
            List <DiffResultEntry> result = new List <DiffResultEntry>();

            foreach (var entry in src)
            {
                string tag = entry.Key;
                if (onlyLatest)
                {
                    int    ver  = entry.Value.Last().Key;
                    string text = target.LookupText(tag, ver);

                    if (text == null)
                    {
                        string refText   = entry.Value.Last().Value;
                        var    potential = target.LookupLatestText(tag);
                        if (potential != null)
                        {
                            text = potential.Item2;
                        }
                        result.Add(new DiffResultEntry(tag, ver, refText, text));
                    }
                }
                else
                {
                    foreach (var kv in entry.Value)
                    {
                        int    ver  = kv.Key;
                        string text = target.LookupText(tag, ver);
                        if (text == null)
                        {
                            string refText   = entry.Value.Last().Value;
                            var    potential = target.LookupLatestText(tag);
                            if (potential != null)
                            {
                                text = potential.Item2;
                            }
                            result.Add(new DiffResultEntry(tag, ver, refText, text));
                        }
                    }
                }
            }
            return(result);
        }
コード例 #2
0
        public static bool DoDiff(string newOriginalPath, string newOriginalFormat, string oldOriginalPath, string oldOriginalFormat,
                                  string oldTranslationPath, string oldTranslationFormat, string outputPath, string outputFormat, string diffFilePath, string language)
        {
            SingleLanguageDB oldOriginal    = new SingleLanguageDB(language);
            SingleLanguageDB newOriginal    = new SingleLanguageDB(language);
            SingleLanguageDB oldTranslation = new SingleLanguageDB(language);

            Localization.BatchImportToSingleLanguageDB(newOriginal, newOriginalPath, newOriginalFormat);
            Localization.BatchImportToSingleLanguageDB(oldOriginal, oldOriginalPath, oldOriginalFormat);
            Localization.BatchImportToSingleLanguageDB(oldTranslation, oldTranslationPath, oldTranslationFormat);

            var     diff    = Localization.Compare(oldOriginal, newOriginal, false);
            YMLFile diffyml = new YMLFile();

            diffyml.AppendLine(null, -1, GetLanguageTag(language) + ":", null);
            foreach (var entry in diff)
            {
                string chitext = oldTranslation.LookupText(entry.Tag, entry.Version);
                diffyml.AppendLine(null, -1, "# new: " + entry.NewText, null);
                diffyml.AppendLine(null, -1, "# old: " + entry.OldText, null);

                if (chitext != null)
                {
                    diffyml.AppendLine(entry.Tag, entry.Version, chitext, null);
                }
                else
                {
                    var lastestText = oldTranslation.LookupLatestText(entry.Tag);
                    if (lastestText != null)
                    {
                        diffyml.AppendLine(entry.Tag, entry.Version, lastestText.Item2, null);
                    }
                    else
                    {
                        diffyml.AppendLine(entry.Tag, entry.Version, entry.NewText, null);
                    }
                }

                // Remove trnaslation, prepare for export
                if (chitext != null)
                {
                    oldTranslation.Remove(entry.Tag, entry.Version);
                }
            }

            diffyml.Write(diffFilePath);

            if (outputPath != null && outputFormat != null)
            {
                return(Localization.BatchExportLocalization(oldTranslation, newOriginalPath, newOriginalFormat, null, outputPath, outputFormat));
            }

            return(true);
        }
コード例 #3
0
        /// <summary>
        /// Check if newdb has anything new, will ignore extra entries in old db.
        /// Can be used to detect difference between versions.
        /// </summary>
        /// <param name="oldDb"></param>
        /// <param name="newDb"></param>
        /// <returns>tag, ver, new string, old text</returns>
        static public List <DiffResultEntry> Compare(SingleLanguageDB oldDb, SingleLanguageDB newDb, bool lowercase = false)
        {
            List <DiffResultEntry> result = new List <DiffResultEntry>();

            foreach (var entry in newDb)
            {
                string tag = entry.Key;
                foreach (var kv in entry.Value)
                {
                    int    ver     = kv.Key;
                    string refText = kv.Value;
                    string text    = oldDb.LookupText(tag, ver);
                    if (text == null)
                    {
                        var potential = oldDb.LookupLatestText(tag);
                        if (potential != null)
                        {
                            text = potential.Item2;
                        }
                        result.Add(new DiffResultEntry(tag, ver, refText, text));
                    }
                    else
                    {
                        // check if string are same
                        if (lowercase)
                        {
                            if (refText.ToLower() != text.ToLower())
                            {
                                result.Add(new DiffResultEntry(tag, ver, refText, text));
                            }
                        }
                        else
                        {
                            if (refText != text)
                            {
                                result.Add(new DiffResultEntry(tag, ver, refText, text));
                            }
                        }
                    }
                }
            }
            return(result);
        }
コード例 #4
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);
        }