Beispiel #1
0
        public virtual ConcurrentDictionary <string, LanguageTag> GetAppLanguages()
        {
            ConcurrentDictionary <string, LanguageTag> AppLanguages = (ConcurrentDictionary <string, LanguageTag>)HttpRuntime.Cache["i18n.AppLanguages"];

            if (AppLanguages != null)
            {
                return(AppLanguages);
            }
            lock (Sync)
            {
                AppLanguages = (ConcurrentDictionary <string, LanguageTag>)HttpRuntime.Cache["i18n.AppLanguages"];
                if (AppLanguages != null)
                {
                    return(AppLanguages);
                }
                AppLanguages = new ConcurrentDictionary <string, LanguageTag>();

                // Insert into cache.
                // NB: we do this before actually populating the collection. This is so that any changes to the
                // folders before we finish populating the collection will cause the cache item to be invalidated
                // and hence reloaded on next request, and so will not be missed.
                HttpRuntime.Cache.Insert("i18n.AppLanguages", AppLanguages, _translationRepository.GetCacheDependencyForAllLanguages());

                // Populate the collection.
                List <string> languages = _translationRepository.GetAvailableLanguages().Select(x => x.LanguageShortTag).ToList();

                // Ensure default language is included in AppLanguages where appropriate.
                if (LocalizedApplication.Current.MessageKeyIsValueInDefaultLanguage &&
                    !languages.Any(x => LocalizedApplication.Current.DefaultLanguageTag.Equals(x)))
                {
                    languages.Add(LocalizedApplication.Current.DefaultLanguageTag.ToString());
                }

                foreach (var langtag in languages)
                {
                    if (IsLanguageValid(langtag))
                    {
                        AppLanguages[langtag] = LanguageTag.GetCachedInstance(langtag);
                    }
                }

                // Done.
                return(AppLanguages);
            }
        }