Exemple #1
0
        private static void AppendMissingKeyedTranslations(StringBuilder sb)
        {
            LoadedLanguage activeLanguage  = LanguageDatabase.activeLanguage;
            LoadedLanguage defaultLanguage = LanguageDatabase.defaultLanguage;

            if (activeLanguage == defaultLanguage)
            {
                return;
            }
            StringBuilder stringBuilder = new StringBuilder();
            int           num           = 0;

            foreach (KeyValuePair <string, LoadedLanguage.KeyedReplacement> keyedReplacement in defaultLanguage.keyedReplacements)
            {
                if (!activeLanguage.HaveTextForKey(keyedReplacement.Key))
                {
                    string text = keyedReplacement.Key + " '" + keyedReplacement.Value.value.Replace("\n", "\\n") + "' (English file: " + defaultLanguage.GetKeySourceFileAndLine(keyedReplacement.Key) + ")";
                    if (activeLanguage.HaveTextForKey(keyedReplacement.Key, allowPlaceholders: true))
                    {
                        text = text + " (placeholder exists in " + activeLanguage.GetKeySourceFileAndLine(keyedReplacement.Key) + ")";
                    }
                    num++;
                    stringBuilder.AppendLine(text);
                }
            }
            sb.AppendLine();
            sb.AppendLine("========== Missing keyed translations (" + num + ") ==========");
            sb.Append(stringBuilder);
        }
Exemple #2
0
        private static void AppendUnnecessaryKeyedTranslations(StringBuilder sb)
        {
            LoadedLanguage activeLanguage  = LanguageDatabase.activeLanguage;
            LoadedLanguage defaultLanguage = LanguageDatabase.defaultLanguage;

            if (activeLanguage == defaultLanguage)
            {
                return;
            }
            StringBuilder stringBuilder = new StringBuilder();
            int           num           = 0;

            foreach (KeyValuePair <string, LoadedLanguage.KeyedReplacement> keyValuePair in activeLanguage.keyedReplacements)
            {
                if (!defaultLanguage.HaveTextForKey(keyValuePair.Key, false))
                {
                    num++;
                    stringBuilder.AppendLine(string.Concat(new string[]
                    {
                        keyValuePair.Key,
                        " '",
                        keyValuePair.Value.value.Replace("\n", "\\n"),
                        "' (",
                        activeLanguage.GetKeySourceFileAndLine(keyValuePair.Key),
                        ")"
                    }));
                }
            }
            sb.AppendLine();
            sb.AppendLine("========== Unnecessary keyed translations (will never be used) (" + num + ") ==========");
            sb.Append(stringBuilder);
        }
        private static void AppendMissingKeyedTranslations(StringBuilder sb)
        {
            LoadedLanguage activeLanguage  = LanguageDatabase.activeLanguage;
            LoadedLanguage defaultLanguage = LanguageDatabase.defaultLanguage;
            StringBuilder  stringBuilder   = new StringBuilder();
            int            num             = 0;

            foreach (KeyValuePair <string, string> keyValuePair in defaultLanguage.keyedReplacements)
            {
                if (!activeLanguage.HaveTextForKey(keyValuePair.Key))
                {
                    num++;
                    stringBuilder.AppendLine(string.Concat(new string[]
                    {
                        keyValuePair.Key,
                        " '",
                        keyValuePair.Value.Replace("\n", "\\n"),
                        "' (",
                        defaultLanguage.GetKeySourceFileAndLine(keyValuePair.Key),
                        ")"
                    }));
                }
            }
            sb.AppendLine();
            sb.AppendLine("========== Missing keyed translations (" + num + ") ==========");
            sb.Append(stringBuilder);
        }
        public static void OutputTranslationReport()
        {
            LoadedLanguage activeLanguage  = LanguageDatabase.activeLanguage;
            LoadedLanguage defaultLanguage = LanguageDatabase.defaultLanguage;

            if (activeLanguage == defaultLanguage)
            {
                Messages.Message("Please activate a non-English language to scan.", MessageTypeDefOf.RejectInput);
            }
            else
            {
                activeLanguage.LoadData();
                defaultLanguage.LoadData();
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.AppendLine("Translation report for " + activeLanguage);
                stringBuilder.AppendLine();
                stringBuilder.AppendLine("========== Argument count mismatches =========");
                foreach (string item in defaultLanguage.keyedReplacements.Keys.Intersect(activeLanguage.keyedReplacements.Keys))
                {
                    int num  = LanguageReportGenerator.CountParametersInString(defaultLanguage.keyedReplacements[item]);
                    int num2 = LanguageReportGenerator.CountParametersInString(activeLanguage.keyedReplacements[item]);
                    if (num != num2)
                    {
                        stringBuilder.AppendLine(string.Format("{0} - '{1}' compared to '{2}'", item, defaultLanguage.keyedReplacements[item], activeLanguage.keyedReplacements[item]));
                    }
                }
                stringBuilder.AppendLine();
                stringBuilder.AppendLine("========== Missing keyed translations =========");
                foreach (KeyValuePair <string, string> keyedReplacement in defaultLanguage.keyedReplacements)
                {
                    if (!activeLanguage.HaveTextForKey(keyedReplacement.Key))
                    {
                        stringBuilder.AppendLine(keyedReplacement.Key + " - '" + keyedReplacement.Value + "'");
                    }
                }
                stringBuilder.AppendLine();
                stringBuilder.AppendLine("========== Unnecessary keyed translations (will never be used) =========");
                foreach (KeyValuePair <string, string> keyedReplacement2 in activeLanguage.keyedReplacements)
                {
                    if (!defaultLanguage.HaveTextForKey(keyedReplacement2.Key))
                    {
                        stringBuilder.AppendLine(keyedReplacement2.Key + " - '" + keyedReplacement2.Value + "'");
                    }
                }
                stringBuilder.AppendLine();
                stringBuilder.AppendLine("========== Def-injected translations missing =========");
                stringBuilder.AppendLine("Note: This does NOT return any kind of sub-fields. So if there's a list of strings, or a sub-member of the def with a string in it or something, they won't be reported here.");
                foreach (DefInjectionPackage defInjection in activeLanguage.defInjections)
                {
                    foreach (string item2 in defInjection.MissingInjections())
                    {
                        stringBuilder.AppendLine(defInjection.defType.Name + ": " + item2);
                    }
                }
                stringBuilder.AppendLine();
                stringBuilder.AppendLine("========== Backstory translations missing =========");
                foreach (string item3 in BackstoryTranslationUtility.MissingBackstoryTranslations(activeLanguage))
                {
                    stringBuilder.AppendLine(item3);
                }
                Log.Message(stringBuilder.ToString());
                Messages.Message("Translation report about " + activeLanguage.ToString() + " written to console. Hit ` to see it.", MessageTypeDefOf.NeutralEvent);
            }
        }
        private static void CleanupKeyedTranslations()
        {
            LoadedLanguage activeLanguage = LanguageDatabase.activeLanguage;
            LoadedLanguage english        = LanguageDatabase.defaultLanguage;
            string         activeLanguageCoreModFolderPath = TranslationFilesCleaner.GetActiveLanguageCoreModFolderPath();
            string         text          = Path.Combine(activeLanguageCoreModFolderPath, "CodeLinked");
            string         text2         = Path.Combine(activeLanguageCoreModFolderPath, "Keyed");
            DirectoryInfo  directoryInfo = new DirectoryInfo(text);

            if (directoryInfo.Exists)
            {
                if (!Directory.Exists(text2))
                {
                    Directory.Move(text, text2);
                    Thread.Sleep(1000);
                    directoryInfo = new DirectoryInfo(text2);
                }
            }
            else
            {
                directoryInfo = new DirectoryInfo(text2);
            }
            if (!directoryInfo.Exists)
            {
                Log.Error("Could not find keyed translations folder for the active language.", false);
                return;
            }
            DirectoryInfo directoryInfo2 = new DirectoryInfo(Path.Combine(TranslationFilesCleaner.GetEnglishLanguageCoreModFolderPath(), "Keyed"));

            if (!directoryInfo2.Exists)
            {
                Log.Error("English keyed translations folder doesn't exist.", false);
                return;
            }
            FileInfo[] files = directoryInfo.GetFiles("*.xml", SearchOption.AllDirectories);
            for (int i = 0; i < files.Length; i++)
            {
                FileInfo fileInfo = files[i];
                try
                {
                    fileInfo.Delete();
                }
                catch (Exception ex)
                {
                    Log.Error(string.Concat(new object[]
                    {
                        "Could not delete ",
                        fileInfo.Name,
                        ": ",
                        ex
                    }), false);
                }
            }
            FileInfo[] files2 = directoryInfo2.GetFiles("*.xml", SearchOption.AllDirectories);
            for (int j = 0; j < files2.Length; j++)
            {
                FileInfo fileInfo2 = files2[j];
                try
                {
                    string path  = new Uri(directoryInfo2.FullName + Path.DirectorySeparatorChar).MakeRelativeUri(new Uri(fileInfo2.FullName)).ToString();
                    string text3 = Path.Combine(directoryInfo.FullName, path);
                    Directory.CreateDirectory(Path.GetDirectoryName(text3));
                    fileInfo2.CopyTo(text3);
                }
                catch (Exception ex2)
                {
                    Log.Error(string.Concat(new object[]
                    {
                        "Could not copy ",
                        fileInfo2.Name,
                        ": ",
                        ex2
                    }), false);
                }
            }
            List <LoadedLanguage.KeyedReplacement> list = (from x in activeLanguage.keyedReplacements
                                                           where !x.Value.isPlaceholder && !english.HaveTextForKey(x.Key, false)
                                                           select x.Value).ToList <LoadedLanguage.KeyedReplacement>();
            HashSet <LoadedLanguage.KeyedReplacement> writtenUnusedKeyedTranslations = new HashSet <LoadedLanguage.KeyedReplacement>();

            FileInfo[] files3 = directoryInfo.GetFiles("*.xml", SearchOption.AllDirectories);
            for (int k = 0; k < files3.Length; k++)
            {
                FileInfo fileInfo3 = files3[k];
                try
                {
                    XDocument xDocument = XDocument.Load(fileInfo3.FullName, LoadOptions.PreserveWhitespace);
                    XElement  xElement  = xDocument.DescendantNodes().OfType <XElement>().FirstOrDefault <XElement>();
                    if (xElement != null)
                    {
                        try
                        {
                            foreach (XNode current in xElement.DescendantNodes())
                            {
                                XElement xElement2 = current as XElement;
                                if (xElement2 != null)
                                {
                                    foreach (XNode current2 in xElement2.DescendantNodes())
                                    {
                                        try
                                        {
                                            XText xText = current2 as XText;
                                            if (xText != null && !xText.Value.NullOrEmpty())
                                            {
                                                string value = " EN: " + xText.Value + " ";
                                                current.AddBeforeSelf(new XComment(value));
                                                current.AddBeforeSelf(Environment.NewLine);
                                                current.AddBeforeSelf("  ");
                                            }
                                        }
                                        catch (Exception ex3)
                                        {
                                            Log.Error(string.Concat(new object[]
                                            {
                                                "Could not add comment node in ",
                                                fileInfo3.Name,
                                                ": ",
                                                ex3
                                            }), false);
                                        }
                                        current2.Remove();
                                    }
                                    try
                                    {
                                        string text4;
                                        if (activeLanguage.TryGetTextFromKey(xElement2.Name.ToString(), out text4))
                                        {
                                            if (!text4.NullOrEmpty())
                                            {
                                                xElement2.Add(new XText(text4.Replace("\n", "\\n")));
                                            }
                                        }
                                        else
                                        {
                                            xElement2.Add(new XText("TODO"));
                                        }
                                    }
                                    catch (Exception ex4)
                                    {
                                        Log.Error(string.Concat(new object[]
                                        {
                                            "Could not add existing translation or placeholder in ",
                                            fileInfo3.Name,
                                            ": ",
                                            ex4
                                        }), false);
                                    }
                                }
                            }
                            bool flag = false;
                            foreach (LoadedLanguage.KeyedReplacement current3 in list)
                            {
                                if (new Uri(fileInfo3.FullName).Equals(new Uri(current3.fileSourceFullPath)))
                                {
                                    if (!flag)
                                    {
                                        xElement.Add("  ");
                                        xElement.Add(new XComment(" UNUSED "));
                                        xElement.Add(Environment.NewLine);
                                        flag = true;
                                    }
                                    XElement xElement3 = new XElement(current3.key);
                                    if (current3.isPlaceholder)
                                    {
                                        xElement3.Add(new XText("TODO"));
                                    }
                                    else if (!current3.value.NullOrEmpty())
                                    {
                                        xElement3.Add(new XText(current3.value.Replace("\n", "\\n")));
                                    }
                                    xElement.Add("  ");
                                    xElement.Add(xElement3);
                                    xElement.Add(Environment.NewLine);
                                    writtenUnusedKeyedTranslations.Add(current3);
                                }
                            }
                            if (flag)
                            {
                                xElement.Add(Environment.NewLine);
                            }
                        }
                        finally
                        {
                            TranslationFilesCleaner.SaveXMLDocumentWithProcessedNewlineTags(xDocument.Root, fileInfo3.FullName);
                        }
                    }
                }
                catch (Exception ex5)
                {
                    Log.Error(string.Concat(new object[]
                    {
                        "Could not process ",
                        fileInfo3.Name,
                        ": ",
                        ex5
                    }), false);
                }
            }
            foreach (IGrouping <string, LoadedLanguage.KeyedReplacement> current4 in from x in list
                     where !writtenUnusedKeyedTranslations.Contains(x)
                     group x by x.fileSourceFullPath)
            {
                try
                {
                    if (File.Exists(current4.Key))
                    {
                        Log.Error("Could not save unused keyed translations to " + current4.Key + " because this file already exists.", false);
                    }
                    else
                    {
                        object[] expr_676  = new object[1];
                        int      arg_6DB_1 = 0;
                        XName    arg_6D6_0 = "LanguageData";
                        object[] expr_688  = new object[4];
                        expr_688[0] = new XComment("NEWLINE");
                        expr_688[1] = new XComment(" UNUSED ");
                        expr_688[2] = current4.Select(delegate(LoadedLanguage.KeyedReplacement x)
                        {
                            string text5 = (!x.isPlaceholder) ? x.value : "TODO";
                            return(new XElement(x.key, new XText((!text5.NullOrEmpty()) ? text5.Replace("\n", "\\n") : string.Empty)));
                        });
                        expr_688[3]         = new XComment("NEWLINE");
                        expr_676[arg_6DB_1] = new XElement(arg_6D6_0, expr_688);
                        XDocument doc = new XDocument(expr_676);
                        TranslationFilesCleaner.SaveXMLDocumentWithProcessedNewlineTags(doc, current4.Key);
                    }
                }
                catch (Exception ex6)
                {
                    Log.Error(string.Concat(new object[]
                    {
                        "Could not save unused keyed translations to ",
                        current4.Key,
                        ": ",
                        ex6
                    }), false);
                }
            }
        }
        private static void CleanupKeyedTranslations()
        {
            LoadedLanguage activeLanguage = LanguageDatabase.activeLanguage;
            LoadedLanguage english        = LanguageDatabase.defaultLanguage;
            List <LoadedLanguage.KeyedReplacement> list = (from x in activeLanguage.keyedReplacements
                                                           where !x.Value.isPlaceholder && !english.HaveTextForKey(x.Key)
                                                           select x.Value).ToList();
            HashSet <LoadedLanguage.KeyedReplacement> writtenUnusedKeyedTranslations = new HashSet <LoadedLanguage.KeyedReplacement>();

            foreach (ModMetaData item in ModsConfig.ActiveModsInLoadOrder)
            {
                string        languageFolderPath = GetLanguageFolderPath(activeLanguage, item.RootDir.FullName);
                string        text          = Path.Combine(languageFolderPath, "CodeLinked");
                string        text2         = Path.Combine(languageFolderPath, "Keyed");
                DirectoryInfo directoryInfo = new DirectoryInfo(text);
                if (directoryInfo.Exists)
                {
                    if (!Directory.Exists(text2))
                    {
                        Directory.Move(text, text2);
                        Thread.Sleep(1000);
                        directoryInfo = new DirectoryInfo(text2);
                    }
                }
                else
                {
                    directoryInfo = new DirectoryInfo(text2);
                }
                DirectoryInfo directoryInfo2 = new DirectoryInfo(Path.Combine(GetLanguageFolderPath(english, item.RootDir.FullName), "Keyed"));
                if (!directoryInfo2.Exists)
                {
                    if (item.IsCoreMod)
                    {
                        Log.Error("English keyed translations folder doesn't exist.");
                    }
                    if (!directoryInfo.Exists)
                    {
                        continue;
                    }
                }
                if (!directoryInfo.Exists)
                {
                    directoryInfo.Create();
                }
                FileInfo[] files = directoryInfo.GetFiles("*.xml", SearchOption.AllDirectories);
                foreach (FileInfo fileInfo in files)
                {
                    try
                    {
                        fileInfo.Delete();
                    }
                    catch (Exception ex)
                    {
                        Log.Error("Could not delete " + fileInfo.Name + ": " + ex);
                    }
                }
                files = directoryInfo2.GetFiles("*.xml", SearchOption.AllDirectories);
                foreach (FileInfo fileInfo2 in files)
                {
                    try
                    {
                        string path  = new Uri(directoryInfo2.FullName + Path.DirectorySeparatorChar).MakeRelativeUri(new Uri(fileInfo2.FullName)).ToString();
                        string text3 = Path.Combine(directoryInfo.FullName, path);
                        Directory.CreateDirectory(Path.GetDirectoryName(text3));
                        fileInfo2.CopyTo(text3);
                    }
                    catch (Exception ex2)
                    {
                        Log.Error("Could not copy " + fileInfo2.Name + ": " + ex2);
                    }
                }
                files = directoryInfo.GetFiles("*.xml", SearchOption.AllDirectories);
                foreach (FileInfo fileInfo3 in files)
                {
                    try
                    {
                        XDocument xDocument = XDocument.Load(fileInfo3.FullName, LoadOptions.PreserveWhitespace);
                        XElement  xElement  = xDocument.DescendantNodes().OfType <XElement>().FirstOrDefault();
                        if (xElement == null)
                        {
                            continue;
                        }
                        try
                        {
                            XNode[] array = xElement.DescendantNodes().ToArray();
                            foreach (XNode xNode in array)
                            {
                                XElement xElement2 = xNode as XElement;
                                if (xElement2 == null)
                                {
                                    continue;
                                }
                                XNode[] array2 = xElement2.DescendantNodes().ToArray();
                                foreach (XNode xNode2 in array2)
                                {
                                    try
                                    {
                                        XText xText = xNode2 as XText;
                                        if (xText != null && !xText.Value.NullOrEmpty())
                                        {
                                            string comment = " EN: " + xText.Value + " ";
                                            xNode.AddBeforeSelf(new XComment(SanitizeXComment(comment)));
                                            xNode.AddBeforeSelf(Environment.NewLine);
                                            xNode.AddBeforeSelf("  ");
                                        }
                                    }
                                    catch (Exception ex3)
                                    {
                                        Log.Error("Could not add comment node in " + fileInfo3.Name + ": " + ex3);
                                    }
                                    xNode2.Remove();
                                }
                                try
                                {
                                    if (activeLanguage.TryGetTextFromKey(xElement2.Name.ToString(), out var translated))
                                    {
                                        if (!translated.NullOrEmpty())
                                        {
                                            xElement2.Add(new XText(translated.Replace("\n", "\\n").RawText));
                                        }
                                    }
                                    else
                                    {
                                        xElement2.Add(new XText("TODO"));
                                    }
                                }
                                catch (Exception ex4)
                                {
                                    Log.Error("Could not add existing translation or placeholder in " + fileInfo3.Name + ": " + ex4);
                                }
                            }
                            bool flag = false;
                            foreach (LoadedLanguage.KeyedReplacement item2 in list)
                            {
                                if (new Uri(fileInfo3.FullName).Equals(new Uri(item2.fileSourceFullPath)))
                                {
                                    if (!flag)
                                    {
                                        xElement.Add("  ");
                                        xElement.Add(new XComment(" UNUSED "));
                                        xElement.Add(Environment.NewLine);
                                        flag = true;
                                    }
                                    XElement xElement3 = new XElement(item2.key);
                                    if (item2.isPlaceholder)
                                    {
                                        xElement3.Add(new XText("TODO"));
                                    }
                                    else if (!item2.value.NullOrEmpty())
                                    {
                                        xElement3.Add(new XText(item2.value.Replace("\n", "\\n")));
                                    }
                                    xElement.Add("  ");
                                    xElement.Add(xElement3);
                                    xElement.Add(Environment.NewLine);
                                    writtenUnusedKeyedTranslations.Add(item2);
                                }
                            }
                            if (flag)
                            {
                                xElement.Add(Environment.NewLine);
                            }
                        }
                        finally
                        {
                            SaveXMLDocumentWithProcessedNewlineTags(xDocument.Root, fileInfo3.FullName);
                        }
                    }
                    catch (Exception ex5)
                    {
                        Log.Error("Could not process " + fileInfo3.Name + ": " + ex5);
                    }
                }
            }
            foreach (IGrouping <string, LoadedLanguage.KeyedReplacement> item3 in from x in list
                     where !writtenUnusedKeyedTranslations.Contains(x)
                     group x by x.fileSourceFullPath)
            {
                try
                {
                    if (File.Exists(item3.Key))
                    {
                        Log.Error("Could not save unused keyed translations to " + item3.Key + " because this file already exists.");
                        continue;
                    }
                    SaveXMLDocumentWithProcessedNewlineTags(new XDocument(new XElement("LanguageData", new XComment("NEWLINE"), new XComment(" UNUSED "), item3.Select(delegate(LoadedLanguage.KeyedReplacement x)
                    {
                        string text4 = (x.isPlaceholder ? "TODO" : x.value);
                        return(new XElement(x.key, new XText(text4.NullOrEmpty() ? "" : text4.Replace("\n", "\\n"))));
                    }), new XComment("NEWLINE"))), item3.Key);
                }
                catch (Exception ex6)
                {
                    Log.Error("Could not save unused keyed translations to " + item3.Key + ": " + ex6);
                }
            }
        }