Ejemplo n.º 1
0
        /// <summary>
        /// Add Localization messages from the specified TextReader
        /// (which hopefully points at a .po file)
        /// Doesn't initialize the collection, so it can be called from
        /// a loop if needed.
        /// </summary>
        /// <param name="reader"></param>
        public void LoadFromReader(TextReader reader)
        {
            Regex re = new Regex(@"""(.*)""", RegexOptions.Compiled);

            string  line;
            bool    parsingMsgID = true;
            Message message      = new Message();

            while (null != (line = reader.ReadLine()))
            {
                if (String.IsNullOrEmpty(line))
                {
                    // new message block
                    if (message.MsgID != "")
                    {
                        if (!Messages.ContainsKey(message.MsgID))
                        {
                            Messages.Add(message.MsgID, message);
                        }
                        if (!MessagesByAutoID.ContainsKey(message.AutoID))
                        {
                            MessagesByAutoID.Add(message.AutoID, message);
                        }
                    }
                    message = new Message();
                    continue;
                }
                if (line.StartsWith("#:") && LoadComments)
                {
                    // context
                    message.Contexts.Add(line.Replace("#: ", ""));
                }
                if (line.StartsWith("#") && LoadComments)
                {
                    // comment
                    message.Comments.Add(line);
                    continue;
                }
                if (line.StartsWith("msgid"))
                {
                    // text we find from here out is part of the MessageID
                    parsingMsgID = true;
                }
                else if (line.StartsWith("msgstr"))
                {
                    // text we find from here on out is part of the Message String
                    parsingMsgID = false;
                }

                Match m = re.Match(line);
                if (m.Success)
                {
                    string token = m.Groups[1].Value;

                    if (parsingMsgID)
                    {
                        message.MsgID += token;
                    }
                    else
                    {
                        if (!line.StartsWith("msgstr"))
                        {
                            message.MsgStr += Environment.NewLine;
                        }
                        message.MsgStr += token;
                    }
                }
            }

            // put away the last one
            if (message.MsgID != "")
            {
                Messages.Add(message.MsgID, message);
            }

            reader.Close();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Add Localization messages from the specified TextReader
        /// (which hopefully points at a .po file)
        /// Doesn't initialize the collection, so it can be called from
        /// a loop if needed.
        /// </summary>
        /// <param name="reader"></param>
        /// <created author="laurentiu.macovei" date="Tue, 29 Nov 2011 12:58:42 GMT"/>
        public void LoadFromReader(TextReader reader)
        {
            string   line;
            Message  message  = new Message();
            NodeType lastNode = NodeType.WhiteSpace;

            while (null != (line = reader.ReadLine()))
            {
                if (String.IsNullOrEmpty(line))
                {
                    // new message block
                    if (message.MsgID != "")
                    {
                        if (!Messages.ContainsKey(message.MsgID))
                        {
                            Messages.Add(message.MsgID, message);
                        }
                        if (!MessagesByAutoID.ContainsKey(message.AutoID))
                        {
                            MessagesByAutoID.Add(message.AutoID, message);
                        }
                    }
                    message = new Message();
                    continue;
                }
                NodeType node = GetNodeType(line);

                switch (node)
                {
                case NodeType.TranslatorComment:
                    if (LoadComments)
                    {
                        message.TranslatorComments.Add(line.Substring(1));
                    }
                    break;

                case NodeType.ExtractedComment:
                    if (LoadComments)
                    {
                        message.ExtractedComments.Add(line.Substring(2));
                    }
                    break;

                case NodeType.Reference:
                    if (LoadComments)
                    {
                        message.Contexts.Add(line.Substring(2).Trim());
                    }
                    break;

                case NodeType.Flag:
                    //flags are ignored for now
                    //if (LoadComments)
                    //    message.Comments.Add(line.Substring(2).Trim());
                    break;

                case NodeType.PreviousUntranslatedString:
                    //previous untranslated strings are ingored for now
                    break;

                case NodeType.MsgID:
                    message.MsgID = Unescape(line.Substring(6));
                    lastNode      = node;
                    break;

                case NodeType.MsgID_Plural:
                    message.MsgID_Plural = Unescape(line.Substring(13));
                    lastNode             = node;
                    break;

                case NodeType.MsgStr:
                    message.MsgStr = Unescape(line.Substring(7));
                    lastNode       = node;
                    break;

                case NodeType.MsgStrWithBrakets:
                    message.MsgStr = Unescape(line.Substring(10));
                    lastNode       = node;
                    break;

                case NodeType.MsgStr_Plural:
                    message.MsgStr_Plural = Unescape(line.Substring(10));
                    lastNode = node;
                    break;

                case NodeType.Continued:
                    //if (!string.IsNullOrEmpty(message.MsgID))
                    switch (lastNode)
                    {
                    case NodeType.MsgID:
                        message.MsgID += Unescape(line);
                        break;

                    case NodeType.MsgID_Plural:
                        message.MsgID_Plural += Unescape(line);
                        break;

                    case NodeType.MsgStr:
                    case NodeType.MsgStrWithBrakets:
                        message.MsgStr += Unescape(line);
                        break;

                    case NodeType.MsgStr_Plural:
                        message.MsgStr_Plural += Unescape(line);
                        break;
                    }
                    break;

                case NodeType.WhiteSpace:
                default:
                    break;
                }
            }
            // put away the last one
            if (message.MsgID != "")
            {
                Messages.Add(message.MsgID, message);
                MessagesByAutoID.Add(message.AutoID, message);
                message = new Message();
            }
            reader.Close();
        }