Exemplo n.º 1
0
        public JObject GetTranslationDataForLanguage(string lang = null)
        {
            //read fallback localization json object first
            var fallbackCacheKey = CacheKey.With(GetType(), "FallbackJson");
            var result           = _memoryCache.GetOrCreateExclusive(fallbackCacheKey, cacheEntry =>
            {
                //Add cache  expiration token
                cacheEntry.AddExpirationToken(LocalizationCacheRegion.CreateChangeToken());
                return(InnerGetTranslationData(_options.FallbackLanguage));
            });

            if (lang != null && !lang.EqualsInvariant(_options.FallbackLanguage))
            {
                var cacheKey = CacheKey.With(GetType(), "RequestedLanguageJson", lang);
                result = _memoryCache.GetOrCreateExclusive(cacheKey, cacheEntry =>
                {
                    //Add cache  expiration token
                    cacheEntry.AddExpirationToken(LocalizationCacheRegion.CreateChangeToken());
                    var langJson = InnerGetTranslationData(lang);
                    // Make another instance of fallback language to avoid corruption of it in the memory cache
                    result = (JObject)result.DeepClone();
                    // Need merge default and requested localization json to resulting object
                    result.Merge(langJson, new JsonMergeSettings {
                        MergeArrayHandling = MergeArrayHandling.Merge
                    });
                    return(result);
                });
            }
            return(result);
        }
Exemplo n.º 2
0
        public string[] GetListOfInstalledLanguages()
        {
            var cachekey = CacheKey.With(GetType(), nameof(GetListOfInstalledLanguages));
            var result   = _memoryCache.GetOrCreateExclusive(cachekey, cacheEntry =>
            {
                cacheEntry.AddExpirationToken(LocalizationCacheRegion.CreateChangeToken());
                return(_providers.SelectMany(x => x.GetListOfInstalledLanguages()).Distinct().ToArray());
            });

            return(result);
        }