예제 #1
0
        private IEnumerable <PoUnit> ParseAllTokenStreams()
        {
            foreach (var path in po_paths)
            {
                var       unit    = new PoUnit();
                PoMessage message = null;

                using (var reader = new StreamReader(path)) {
                    var lexer = new PoLexer {
                        Path   = path,
                        Reader = reader
                    };

                    foreach (var token in lexer.Lex())
                    {
                        if (token is PoLexer.Token.Comment)
                        {
                            unit.Comments.Add((PoLexer.Token.Comment)token);
                        }
                        else if (token is PoLexer.Token.Identifier)
                        {
                            message = new PoMessage {
                                Identifier = (string)token
                            };
                            unit.Messages.Add(message);
                        }
                        else if (token is PoLexer.Token.String)
                        {
                            message.Value += (string)token;
                        }
                        else if (token is PoLexer.Token.EndOfUnit)
                        {
                            yield return(unit);

                            unit = new PoUnit();
                        }
                    }
                }

                yield return(unit);
            }
        }
예제 #2
0
        private LocalizedString ParsePoUnit(PoUnit unit)
        {
            var    developer_comments_builder  = new StringBuilder();
            var    translator_comments_builder = new StringBuilder();
            var    references_builder          = new StringBuilder();
            var    flags_builder               = new StringBuilder();
            var    translated_values           = new List <string> ();
            string untranslated_singular_value = null;
            string untranslated_plural_value   = null;

            foreach (var message in unit.Messages)
            {
                var match = Regex.Match(message.Identifier, @"^msg(id|id_plural|str|str\[(\d+)\]|ctx)$");
                if (!match.Success)
                {
                    continue;
                }

                switch (match.Groups[1].Value)
                {
                case "id": untranslated_singular_value = message.Value; break;

                case "id_plural": untranslated_plural_value = message.Value; break;

                case "str": translated_values.Insert(0, message.Value); break;

                case "ctx": break;

                default:
                    if (match.Groups.Count == 3)
                    {
                        translated_values.Insert(Int32.Parse(match.Groups[2].Value), message.Value);
                    }
                    break;
                }
            }

            foreach (var comment in unit.Comments)
            {
                switch (comment.Type)
                {
                case PoLexer.CommentType.Extracted:
                    developer_comments_builder.Append(comment.Value.Trim());
                    developer_comments_builder.Append('\n');
                    break;

                case PoLexer.CommentType.Translator:
                    translator_comments_builder.Append(comment.Value.Trim());
                    translator_comments_builder.Append('\n');
                    break;

                case PoLexer.CommentType.Reference:
                    references_builder.Append(comment.Value.Trim());
                    references_builder.Append(' ');
                    break;

                case PoLexer.CommentType.Flag:
                    flags_builder.Append(comment.Value.Trim());
                    flags_builder.Append(',');
                    break;
                }
            }

            var developer_comments  = developer_comments_builder.ToString().Trim();
            var translator_comments = translator_comments_builder.ToString().Trim();
            var references          = references_builder.ToString().Trim();
            var flags = flags_builder.ToString().Trim();

            var localized_string = new LocalizedString();

            if (!String.IsNullOrWhiteSpace(developer_comments))
            {
                localized_string.DeveloperComments = developer_comments;
            }

            if (!String.IsNullOrWhiteSpace(translator_comments))
            {
                localized_string.DeveloperComments = translator_comments;
            }

            if (!String.IsNullOrWhiteSpace(references))
            {
                localized_string.References = references.Split(' ');
            }

            if (!String.IsNullOrWhiteSpace(flags))
            {
                foreach (var flag in flags.Split(','))
                {
                    if (flag.EndsWith("-format"))
                    {
                        localized_string.StringFormatHint = flag;
                    }
                }
            }

            localized_string.UntranslatedSingularValue = untranslated_singular_value;
            localized_string.UntranslatedPluralValue   = untranslated_plural_value;

            if (translated_values.Count > 0)
            {
                localized_string.TranslatedValues = translated_values.ToArray();
            }

            return(localized_string);
        }