示例#1
0
        /// <summary>
        ///   Adds all items in a list of <see cref="LocalText.Entry"/> elements
        ///   to local text dictionaries.</summary>
        /// <remarks>
        ///   If there are already translations in local text dictionaries with
        ///   same keys, their values are overriden, no exception occurs.</remarks>
        /// <param name="list">
        ///   List of local texts to be added to local text dictionaries.</param>
        /// <param name="pending">
        ///   True if this is a pending approval entry.</param>
        public static void Add(IList <Entry> list, bool pending)
        {
            if (list.Count > 0)
            {
                var newDictionary = new Dictionary <LanguageIDTextKey, string>(pending ? _pendingTexts : _publishedTexts);

                LanguageIDTextKey k = new LanguageIDTextKey(DefaultLanguageID, "");
                foreach (Entry e in list)
                {
                    k._languageID    = e.LanguageID;
                    k._textKey       = e.TextKey;
                    newDictionary[k] = e.LocalText;
                }

                if (pending)
                {
                    _pendingTexts = newDictionary;
                }
                else
                {
                    _publishedTexts = newDictionary;
                }
            }
        }
示例#2
0
        /// <summary>
        ///   Converts <paramref name="textKey">the local text key</paramref>, to its representation in
        ///   <paramref name="languageID">requested language</paramref>.</summary>
        /// <param name="languageID"/>
        ///   Language ID.
        /// <param name="textKey">
        ///   Local text key (can be null).</param>
        /// <param name="pending">
        ///   If pending approval texts to be used, true.</param>
        /// <returns>
        ///   Local text, looked up from requested language, its parents, and default language in order. If not found
        ///   in any, null is returned</returns>
        public static string TryGet(Int64 languageID, string textKey, bool pending)
        {
            // create a key to lookup by language and text key pair
            LanguageIDTextKey k = new LanguageIDTextKey(languageID, textKey);

            string s;

            if (pending)
            {
                // first search in pending texts
                if (_pendingTexts.TryGetValue(k, out s))
                {
                    // pending text is available, return it
                    if (s != null)
                    {
                        return(s);
                    }

                    // pending text is null, it means marked for deletion,
                    // if this is default language, return key itself
                    if (languageID == DefaultLanguageID)
                    {
                        return(null);
                    }
                }
                else if (_publishedTexts.TryGetValue(k, out s))
                {
                    // published is available, return it
                    return(s);
                }
                else if (languageID == DefaultLanguageID)
                {
                    // if language is default language, return text key
                    return(null);
                }

                int circularCheck1 = 0;
                while (true)
                {
                    if (!_languageParents.TryGetValue(languageID, out languageID))
                    {
                        languageID = DefaultLanguageID;
                    }

                    // search in parent or default language
                    k._languageID = languageID;
                    if (_pendingTexts.TryGetValue(k, out s))
                    {
                        // text available in pending parent language
                        if (s != null)
                        {
                            return(s);
                        }

                        // if marked for deletion in default language, return key itself
                        if (languageID == DefaultLanguageID)
                        {
                            return(null);
                        }
                    }
                    else if (_publishedTexts.TryGetValue(k, out s))
                    {
                        // text available in published default language
                        return(s);
                    }
                    else if (languageID == DefaultLanguageID)
                    {
                        // not in pending nor published, or circular reference, return key itself
                        return(null);
                    }
                    // check for possible circular parents
                    if (circularCheck1++ >= 10)
                    {
                        return(null);
                    }
                    // try again for parent language...
                }
            }
            else if (!_publishedTexts.TryGetValue(k, out s))
            {
                // couldn't find, if requested language is not DefaultLanguageID, search in it too
                if (languageID != DefaultLanguageID)
                {
                    int circularCheck2 = 0;
                    while (true)
                    {
                        if (!_languageParents.TryGetValue(languageID, out languageID))
                        {
                            languageID = DefaultLanguageID;
                        }

                        // search in parent or default language
                        k._languageID = languageID;

                        // search again
                        if (_publishedTexts.TryGetValue(k, out s))
                        {
                            return(s);
                        }

                        if (languageID == DefaultLanguageID ||
                            circularCheck2++ >= 10)
                        {
                            return(null);
                        }
                    }
                }

                // couldn't find in requested language or its parents, return key itself
                return(null);
            }
            else
            {
                // found in requested language, return it
                return(s);
            }
        }