Example #1
0
        public static LanguageTag ChooseAppLanguage(this HttpContextBase context, IEnumerable <LanguageTag> AppLanguages)
        {
            string text;

            return(LanguageMatching.MatchLists(
                       context.GetRequestUserLanguages(),
                       AppLanguages,
                       null,
                       null,
                       out text));
        }
Example #2
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);
        }