/// <summary>
        /// collect all message ids of strings that should not be translated
        /// </summary>
        /// <param name="ADoNotTranslatePath"></param>
        /// <returns></returns>
        private static StringCollection GetDoNotTranslateStrings(string ADoNotTranslatePath)
        {
            StringCollection result        = new StringCollection();
            StringCollection OriginalLines = new StringCollection();

            StreamReader sr   = new StreamReader(ADoNotTranslatePath);
            string       line = sr.ReadLine();

            while (line != null)
            {
                if (line.StartsWith("msgid"))
                {
                    string messageId = TPoFileParser.ParsePoLine(sr, ref line, out OriginalLines);

                    result.Add(messageId);
                }
                else
                {
                    line = sr.ReadLine();
                }
            }

            sr.Close();

            return(result);
        }
        /// <summary>
        /// remove all strings from po file that are listed in the "Do Not Translate" file
        /// </summary>
        /// <param name="ADoNotTranslatePath"></param>
        /// <param name="ATranslationFile"></param>
        public static void RemoveUnwantedStringsFromTranslation(string ADoNotTranslatePath, string ATranslationFile)
        {
            StringCollection DoNotTranslate = GetDoNotTranslateStrings(ADoNotTranslatePath);
            StreamReader     sr             = new StreamReader(ATranslationFile);
            Encoding         enc            = new UTF8Encoding(false);
            StreamWriter     sw             = new StreamWriter(ATranslationFile + ".new", false, enc);
            //create a template in which all the source links are contained
            StreamWriter sw_all = new StreamWriter(ATranslationFile + ".withallsources", false, enc);

            string line    = sr.ReadLine();
            int    counter = 0;

            while (line != null)
            {
                counter++;

                if (!line.StartsWith("msgid"))
                {
                    sw_all.WriteLine(line);
                    sw.WriteLine(line);
                    line = sr.ReadLine(); //get the empty line

                    if (line != null)
                    {
                        if (line.Contains("todoComment"))
                        {
                            //Console.WriteLine("here");
                        }

                        while (line.StartsWith("#.") && !line.Contains("todoComment")) //take over the comments(if they exist)
                        {
                            string line_part1 = AdaptString(line, "_ ");
                            string line_part2 = AdaptString(line_part1, "<summary>");
                            string line_part3 = AdaptString(line_part2, "</summary>");

                            sw_all.WriteLine(line_part3);
                            sw.WriteLine(line_part3);
                            line = sr.ReadLine();
                        }

                        if (line.StartsWith("#:")) //take over the first source code line (if it exists)
                        {
                            sw_all.WriteLine(line);

                            if (line.Contains("GenerateI18N.CollectedGettext.cs"))
                            {
                                sw.WriteLine("#: - This item was created automatically from a designer file");
                                line = sr.ReadLine();
                            }
                            else
                            {
                                string currentLine = line;
                                line = sr.ReadLine();

                                if (line.StartsWith("#:"))
                                {
                                    sw.WriteLine(
                                        currentLine +
                                        " (first of several occurrences - the whole list can be found in i8n/template.pot.withallsources)");
                                }
                                else
                                {
                                    sw.WriteLine(currentLine);
                                }
                            }
                        }

                        while (line.StartsWith("#:") || line.StartsWith("#,")) //ignore all other source code lines
                        {
                            sw_all.WriteLine(line);
                            line = sr.ReadLine();
                        }

                        if (line.Contains("todoComment"))
                        {
                            line = sr.ReadLine(); //ignore todoComment
                        }
                    }
                }
                else if ((line != null) && line.StartsWith("msgid"))
                {
                    if (line.Contains("Maintain Month Names for Different Languages"))
                    {
                        // Console.WriteLine("here");
                    }

                    StringCollection OriginalLines;
                    string           messageId = TPoFileParser.ParsePoLine(sr, ref line, out OriginalLines);

                    if (DoNotTranslate.Contains(messageId))
                    {
                        if (!line.StartsWith("msgstr"))
                        {
                            throw new Exception("did expect msgstr in the line");
                        }

                        TPoFileParser.ParsePoLine(sr, ref line, out OriginalLines);
                    }
                    else
                    {
                        foreach (string s in OriginalLines)
                        {
                            sw_all.WriteLine(s);
                            sw.WriteLine(s);
                        }
                    }
                }
            }

            sr.Close();
            sw_all.Close();
            sw.Close();

            TTextFile.UpdateFile(ATranslationFile);

            ReviewTemplateFile(ATranslationFile);
        }
Exemple #3
0
        /// <summary>
        /// add new translations to the po file
        /// </summary>
        public static void WriteUpdatedPoFile(string APoFilePath, SortedList <string, string> ANewTranslations)
        {
            List <string> pofile = new List <string>();

            if (ANewTranslations.Keys.Count > 0)
            {
                TLogging.Log("updating " + APoFilePath);

                // parse the whole po file
                StreamReader sr  = new StreamReader(APoFilePath);
                Encoding     enc = new UTF8Encoding(false);
                StreamWriter sw  = new StreamWriter(APoFilePath + ".new", false, enc);

                string line = sr.ReadLine();

                while (line != null)
                {
                    if (line.StartsWith("msgid \""))
                    {
                        StringCollection OriginalLines;
                        string           messageId = TPoFileParser.ParsePoLine(sr, ref line, out OriginalLines);

                        if (pofile.Contains(messageId))
                        {
                            // ignore this instance
                            TPoFileParser.ParsePoLine(sr, ref line, out OriginalLines);

                            TLogging.Log("duplicate messageid: " + messageId);
                        }
                        else
                        {
                            pofile.Add(messageId);
                        }

                        foreach (string s in OriginalLines)
                        {
                            sw.WriteLine(s);
                        }

                        if (ANewTranslations.ContainsKey(messageId))
                        {
                            // skip msgstr line
                            TPoFileParser.ParsePoLine(sr, ref line, out OriginalLines);

                            sw.WriteLine(String.Format("msgstr \"{0}\"", ANewTranslations[messageId]));

                            ANewTranslations.Remove(messageId);
                        }
                    }
                    else
                    {
                        sw.WriteLine(line);
                        line = sr.ReadLine();
                    }
                }

                sr.Close();
                sw.Close();

                TTextFile.UpdateFile(APoFilePath);

                foreach (string key in ANewTranslations.Keys)
                {
                    TLogging.Log("Warning: cannot find in po file: " + key);
                }
            }
        }