Beispiel #1
0
        private static void ParseBody(TextReader fs, string line, StringBuilder sb, I18NMessage message, Regex quoted)
        {
            if (!string.IsNullOrEmpty(line))
            {
                if (line.StartsWith("msgid"))
                {
                    var msgid = quoted.Match(line).Value;
                    sb.Append(msgid.Substring(1, msgid.Length - 2));

                    while ((line = fs.ReadLine()) != null && !line.StartsWith("msgstr") && !string.IsNullOrWhiteSpace(msgid = quoted.Match(line).Value))
                    {
                        sb.Append(msgid.Substring(1, msgid.Length - 2));
                    }

                    message.MsgId = sb.ToString();
                }

                sb.Clear();
                if (!string.IsNullOrEmpty(line) && line.StartsWith("msgstr"))
                {
                    //var msgstr = quoted.Match(line).Value;
                    //sb.Append(msgstr.Substring(1, msgstr.Length - 2));
                    int firstIndex = line.IndexOf('\"');
                    int lastIndex  = line.LastIndexOf('\"');
                    var msgstr     = line.Substring(firstIndex + 1, lastIndex - firstIndex - 1);
                    sb.Append(msgstr);
                    while ((line = fs.ReadLine()) != null && !string.IsNullOrEmpty(msgstr = quoted.Match(line).Value))
                    {
                        sb.Append(msgstr.Substring(1, msgstr.Length - 2));
                    }

                    message.MsgStr = sb.ToString();
                }
            }
        }
Beispiel #2
0
        private static void ParseBody(TextReader fs, string line, StringBuilder sb, I18NMessage message)
        {
            if (!string.IsNullOrEmpty(line))
            {
                if (line.StartsWith("msgid"))
                {
                    var msgid = line.Unquote();
                    sb.Append(msgid);

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

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

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

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

                    message.MsgStr = sb.ToString().Unescape();
                }
            }
        }
Beispiel #3
0
        public static void LoadFromDiskAndCache(string culture, string path)
        {
            //If the msgstr is 1 word length, e.g. msgstr \"a\", it does not worked
            var quoted = new Regex("(?:\"(?:[^\"]+.)*\")", RegexOptions.Compiled);

            lock (Sync)
            {
                using (var fs = File.OpenText(path))
                {
                    // http://www.gnu.org/s/hello/manual/gettext/PO-Files.html

                    var    messages = new List <I18NMessage>(0);
                    string line;
                    while ((line = fs.ReadLine()) != null)
                    {
                        if (line.StartsWith("#~") || line.StartsWith("#,"))
                        {
                            continue;
                        }

                        var message = new I18NMessage();
                        var sb      = new StringBuilder();

                        if (line.StartsWith("#"))
                        {
                            sb.Append(CleanCommentLine(line));
                            while ((line = fs.ReadLine()) != null && line.StartsWith("#"))
                            {
                                sb.Append(CleanCommentLine(line));
                            }
                            message.Comment = sb.ToString();

                            sb.Clear();
                            ParseBody(fs, line, sb, message, quoted);
                            messages.Add(message);
                        }
                        else if (line.StartsWith("msgid"))
                        {
                            ParseBody(fs, line, sb, message, quoted);
                        }
                    }

                    lock (Sync)
                    {
                        // If the file changes we want to be able to rebuild the index without recompiling
                        HttpRuntime.Cache.Insert(string.Format("po:{0}", culture), messages, new CacheDependency(path));
                    }
                }
            }
        }
Beispiel #4
0
        private static string GetTextOrDefault(string culture, string key)
        {
            // Note that there is no need to serialize access to HttpRuntime.Cache when just reading from it.
            //
            var         messages = (Dictionary <string, I18NMessage>)HttpRuntime.Cache[GetCacheKey(culture)];
            I18NMessage message  = null;

            if (messages == null || !messages.TryGetValue(key, out message))
            {
                return(key);
            }

            return(message.MsgStr);
            // We check this for null/empty before adding to collection.
        }
Beispiel #5
0
        private static void ParseBody(TextReader fs, string line, StringBuilder sb, I18NMessage message, Regex quoted)
        {
            if(!string.IsNullOrEmpty(line))
            {
                if(line.StartsWith("msgid"))
                {
                    var msgid = quoted.Match(line).Value;
                    sb.Append(msgid.Substring(1, msgid.Length - 2));

                    while ((line = fs.ReadLine()) != null && !line.StartsWith("msgstr") && !string.IsNullOrWhiteSpace(msgid = quoted.Match(line).Value))
                    {
                        sb.Append(msgid.Substring(1, msgid.Length - 2));
                    }

                    message.MsgId = sb.ToString();
                }

                sb.Clear();
                if(!string.IsNullOrEmpty(line) && line.StartsWith("msgstr"))
                {
                    var msgstr = quoted.Match(line).Value;
                    sb.Append(msgstr.Substring(1, msgstr.Length - 2));

                    while ((line = fs.ReadLine()) != null && !string.IsNullOrEmpty(msgstr = quoted.Match(line).Value))
                    {
                        sb.Append(msgstr.Substring(1, msgstr.Length - 2));
                    }

                    message.MsgStr = sb.ToString();
                }
            }
        }
Beispiel #6
0
        private static void LoadFromDiskAndCache(string culture, string path)
        {
            var quoted = new Regex("(?:\"(?:[^\"]+.)*\")", RegexOptions.Compiled);

            lock (_sync)
            {
                using (var fs = File.OpenText(path))
                {
                    // http://www.gnu.org/s/hello/manual/gettext/PO-Files.html

                    var messages = new List<I18NMessage>(0);
                    string line;
                    while ((line = fs.ReadLine()) != null)
                    {
                        if (line.StartsWith("#~"))
                        {
                            continue;
                        }

                        var message = new I18NMessage();
                        var sb = new StringBuilder();

                        if (line.StartsWith("#"))
                        {
                            sb.Append(CleanCommentLine(line));
                            while((line = fs.ReadLine()) != null && line.StartsWith("#"))
                            {
                                sb.Append(CleanCommentLine(line));
                            }
                            message.Comment = sb.ToString();

                            sb.Clear();
                            ParseBody(fs, line, sb, message, quoted);
                            messages.Add(message);
                        }
                        else if (line.StartsWith("msgid"))
                        {
                            ParseBody(fs, line, sb, message, quoted);
                        }
                    }

                    lock (_sync)
                    {
                        // If the file changes we want to be able to rebuild the index without recompiling
                        HttpRuntime.Cache.Insert(string.Format("po:{0}", culture), messages, new CacheDependency(path));
                    }
                }
            }
        }
Beispiel #7
0
        private static void ParseBody(TextReader fs, string line, StringBuilder sb, I18NMessage message)
        {
            if(!string.IsNullOrEmpty(line))
            {
                if(line.StartsWith("msgid"))
                {
                    var msgid = line.Unquote();
                    sb.Append(msgid);

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

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

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

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

                    message.MsgStr = sb.ToString().Unescape();
                }
            }
        }
Beispiel #8
0
        private static void LoadFromDiskAndCache(string culture, string path)
        {
            lock (Sync)
            {
                // It is possible for multiple threads to race to this method. The first to
                // enter the above lock will insert the messages into the cache.
                // If we lost the race...no need to duplicate the work of the winning thread.
                if (HttpRuntime.Cache[GetCacheKey(culture)] != null) {
                    return; }

                using (var fs = File.OpenText(path))
                {
                    // http://www.gnu.org/s/hello/manual/gettext/PO-Files.html

                    var messages = new Dictionary<string, I18NMessage>(0);
                    string line;
                    while ((line = fs.ReadLine()) != null)
                    {
                        if (line.StartsWith("#~"))
                        {
                            continue;
                        }

                        var message = new I18NMessage();
                        var sb = new StringBuilder();

                        if (line.StartsWith("#"))
                        {
                            sb.Append(CleanCommentLine(line));
                            while((line = fs.ReadLine()) != null && line.StartsWith("#"))
                            {
                                sb.Append(CleanCommentLine(line));
                            }
                            message.Comment = sb.ToString();

                            sb.Clear();
                            ParseBody(fs, line, sb, message);

                            // Only if a msgstr (translation) is provided for this entry do we add an entry to the cache.
                            // This conditions facilitates more useful operation of the GetLanguageIfAvailable method,
                            // which prior to this condition was indicating a language was available when in fact there
                            // were zero translation in the PO file (it having been autogenerated during gettext merge).
                            if (!string.IsNullOrWhiteSpace(message.MsgStr))
                            {
                                if (!messages.ContainsKey(message.MsgId))
                                {
                                    messages.Add(message.MsgId, message);
                                }
                            }
                        }
                        else if (line.StartsWith("msgid"))
                        {
                            ParseBody(fs, line, sb, message);
                        }
                    }

                    //lock (Sync)
                    {
                        // If the file changes we want to be able to rebuild the index without recompiling
                        HttpRuntime.Cache.Insert(GetCacheKey(culture), messages, new CacheDependency(path));
                    }
                }
            }
        }
Beispiel #9
0
        private static void LoadFromDiskAndCache(string culture, string path)
        {
            lock (Sync)
            {
                // It is possible for multiple threads to race to this method. The first to
                // enter the above lock will insert the messages into the cache.
                // If we lost the race...no need to duplicate the work of the winning thread.
                if (HttpRuntime.Cache[GetCacheKey(culture)] != null)
                {
                    return;
                }

                using (var fs = File.OpenText(path))
                {
                    // http://www.gnu.org/s/hello/manual/gettext/PO-Files.html

                    var    messages = new Dictionary <string, I18NMessage>(0);
                    string line;
                    while ((line = fs.ReadLine()) != null)
                    {
                        if (line.StartsWith("#~"))
                        {
                            continue;
                        }

                        var message = new I18NMessage();
                        var sb      = new StringBuilder();

                        if (line.StartsWith("#"))
                        {
                            sb.Append(CleanCommentLine(line));
                            while ((line = fs.ReadLine()) != null && line.StartsWith("#"))
                            {
                                sb.Append(CleanCommentLine(line));
                            }
                            message.Comment = sb.ToString();

                            sb.Clear();
                            ParseBody(fs, line, sb, message);

                            // Only if a msgstr (translation) is provided for this entry do we add an entry to the cache.
                            // This conditions facilitates more useful operation of the GetLanguageIfAvailable method,
                            // which prior to this condition was indicating a language was available when in fact there
                            // were zero translation in the PO file (it having been autogenerated during gettext merge).
                            if (!string.IsNullOrWhiteSpace(message.MsgStr))
                            {
                                if (!messages.ContainsKey(message.MsgId))
                                {
                                    messages.Add(message.MsgId, message);
                                }
                            }
                        }
                        else if (line.StartsWith("msgid"))
                        {
                            ParseBody(fs, line, sb, message);
                        }
                    }

                    //lock (Sync)
                    {
                        // If the file changes we want to be able to rebuild the index without recompiling
                        HttpRuntime.Cache.Insert(GetCacheKey(culture), messages, new CacheDependency(path));
                    }
                }
            }
        }