示例#1
0
        private void AddNewTemplateItem(
            string fileName,
            ReferenceContext referenceContext,
            Nugget nugget,
            ConcurrentDictionary <string, TemplateItem> templateItems)
        {
            string msgid = nugget.MsgId.Replace("\r\n", "\n").Replace("\r", "\\n");
            // NB: In memory msgids are normalized so that LFs are converted to "\n" char sequence.
            string        key = TemplateItem.KeyFromMsgidAndComment(msgid, nugget.Comment, _settings.MessageContextEnabledFromComment);
            List <string> tmpList;

            //
            templateItems.AddOrUpdate(
                key,
                // Add routine.
                k => {
                TemplateItem item = new TemplateItem();
                item.MsgKey       = key;
                item.MsgId        = msgid;
                item.FileName     = fileName;

                item.References = new List <ReferenceContext> {
                    referenceContext
                };

                if (nugget.Comment.IsSet())
                {
                    tmpList = new List <string>();
                    tmpList.Add(nugget.Comment);
                    item.Comments = tmpList;
                }

                return(item);
            },
                // Update routine.
                (k, v) =>
            {
                if (!_settings.DisableReferences)
                {
                    var newReferences = new List <ReferenceContext>(v.References.ToList());
                    newReferences.Add(referenceContext);
                    v.References = newReferences;
                }

                if (nugget.Comment.IsSet())
                {
                    tmpList = v.Comments != null ? v.Comments.ToList() : new List <string>();
                    if (!_settings.DisableReferences || !tmpList.Contains(nugget.Comment))
                    {
                        tmpList.Add(nugget.Comment);
                    }
                    v.Comments = tmpList;
                }

                return(v);
            });
        }
示例#2
0
        private void AddNewTemplateItem(
            string filePath,
            int lineNumber,
            Nugget nugget,
            ConcurrentDictionary <string, TemplateItem> templateItems)
        {
            string reference = filePath + ":" + lineNumber.ToString();
            string msgid     = nugget.MsgId.Replace("\r\n", "\n").Replace("\r", "\\n");
            // NB: In memory msgids are normalized so that LFs are converted to "\n" char sequence.
            string        key = TemplateItem.KeyFromMsgidAndComment(msgid, nugget.Comment, _settings.MessageContextEnabledFromComment);
            List <string> tmpList;

            //
            templateItems.AddOrUpdate(
                key,
                // Add routine.
                k => {
                TemplateItem item = new TemplateItem();
                item.MsgKey       = key;
                item.MsgId        = msgid;

                tmpList = new List <string>();
                tmpList.Add(reference);
                item.References = tmpList;

                if (nugget.Comment.IsSet())
                {
                    tmpList = new List <string>();
                    tmpList.Add(nugget.Comment);
                    item.Comments = tmpList;
                }

                return(item);
            },
                // Update routine.
                (k, v) => {
                tmpList = v.References.ToList();
                tmpList.Add(reference);
                v.References = tmpList;

                if (nugget.Comment.IsSet())
                {
                    tmpList = v.Comments != null ? v.Comments.ToList() : new List <string>();
                    tmpList.Add(nugget.Comment);
                    v.Comments = tmpList;
                }

                return(v);
            });
        }
示例#3
0
        public virtual string GetText(string msgid, string msgcomment, LanguageItem[] languages, out LanguageTag o_langtag, int maxPasses = -1)
        {
            // Validate arguments.
            if (maxPasses > (int)LanguageTag.MatchGrade._MaxMatch + 1)
            {
                maxPasses = (int)LanguageTag.MatchGrade._MaxMatch + 1;
            }
            // Init.
            bool fallbackOnDefault = maxPasses == (int)LanguageTag.MatchGrade._MaxMatch + 1 ||
                                     maxPasses == -1;
            // Determine the key for the msg lookup. This may be either msgid or msgid+msgcomment, depending on the prevalent
            // MessageContextEnabledFromComment setting.
            string msgkey = msgid == null ?
                            msgid:
                            TemplateItem.KeyFromMsgidAndComment(msgid, msgcomment, _settings.MessageContextEnabledFromComment);
            // Perform language matching based on UserLanguages, AppLanguages, and presence of
            // resource under msgid for any particular AppLanguage.
            string text;

            o_langtag = LanguageMatching.MatchLists(
                languages,
                GetAppLanguages().Values,
                msgkey,
                TryGetTextFor,
                out text,
                Math.Min(maxPasses, (int)LanguageTag.MatchGrade._MaxMatch));
            // If match was successfull
            if (text != null)
            {
                // If the msgkey was returned...don't output that but rather the msgid as the msgkey
                // may be msgid+msgcomment.
                if (text == msgkey)
                {
                    return(msgid);
                }
                return(text);
            }
            // Optionally try default language.
            if (fallbackOnDefault)
            {
                o_langtag = LocalizedApplication.Current.DefaultLanguageTag;
                return(msgid);
            }
            //
            return(null);
        }
示例#4
0
        /// <summary>
        /// Parses the body of a PO file item. That is to say the message id and the message itself.
        /// Reason for why it must be on second line (textreader) is so that you can read until you have read to far without peek previously for meta data.
        /// </summary>
        /// <param name="fs">A textreader that must be on the second line of a message body</param>
        /// <param name="line">The first line of the message body.</param>
        /// <returns>Returns a TranslationItem with only key, id and message set</returns>
        private TranslationItem ParseBody(TextReader fs, string line, List <string> extractedComments)
        {
            string originalLine = line;

            if (string.IsNullOrEmpty(line))
            {
                return(null);
            }

            TranslationItem message = new TranslationItem {
                MsgKey = ""
            };
            StringBuilder sb = new StringBuilder();

            string msgctxt = null;

            line = RemoveCommentIfHistorical(line);             //so that we read in removed historical records too
            if (line.StartsWith("msgctxt"))
            {
                msgctxt = Unquote(line);
                line    = fs.ReadLine();
            }

            line = RemoveCommentIfHistorical(line);             //so that we read in removed historical records too
            if (line.StartsWith("msgid"))
            {
                var msgid = Unquote(line);
                sb.Append(msgid);

                while ((line = fs.ReadLine()) != null)
                {
                    line = RemoveCommentIfHistorical(line);
                    if (String.IsNullOrEmpty(line))
                    {
                        DebugHelpers.WriteLine("ERROR - line is empty. Original line: " + originalLine);
                        continue;
                    }
                    if (!line.StartsWith("msgstr") && (msgid = Unquote(line)) != null)
                    {
                        sb.Append(msgid);
                    }
                    else
                    {
                        break;
                    }
                }

                message.MsgId = Unescape(sb.ToString());

                // If no msgctxt is set then msgkey is the msgid; otherwise it is msgid+msgctxt.
                message.MsgKey = string.IsNullOrEmpty(msgctxt) ?
                                 message.MsgId:
                                 TemplateItem.KeyFromMsgidAndComment(message.MsgId, msgctxt, true);
            }

            sb.Clear();
            line = RemoveCommentIfHistorical(line);
            if (!string.IsNullOrEmpty(line) && line.StartsWith("msgstr"))
            {
                var msgstr = Unquote(line);
                sb.Append(msgstr);

                while ((line = fs.ReadLine()) != null && (msgstr = Unquote(line)) != null)
                {
                    line = RemoveCommentIfHistorical(line);
                    sb.Append(msgstr);
                }

                message.Message = Unescape(sb.ToString());
            }
            return(message);
        }