Exemple #1
0
        /// <summary>
        /// Effectivly find the value for the requested Key,Category,Culture
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        private async Task <string> GetLocalizedStringAsync(GetLocalizedParam param)
        {
            LocalizationTree tree = await GetLocalizationTreeAsync(param.CultureInfo).ConfigureAwait(false);

            if (tree.LocalizedCategories.TryGetValue(param.Category.ToLowerInvariant(), out LocalizationCategory category) &&
                category.LocalizedValues.TryGetValue(param.Key, out string value))
            {
                return(value);
            }
            return(null);
        }
Exemple #2
0
        /// <summary>
        /// Combine all possible sources of localized string for a given (culture)
        /// into a single Tree containing the most relevent localizations.
        ///
        /// This The localization a found in Resx files
        /// The Possible sources are resx files configured in most relevent file first
        /// <example>
        /// ComposerConfiguration.ResxLocalizationRepositoryConfiguration.PatternsForPossibleSources = {
        ///     "{category}_Custom.{cultureName}.resx",
        ///     "{category}_Custom.{twoLetterISOLanguageName}.resx",
        ///     "{category}_Custom.resx",
        ///     "{category}.{cultureName}.resx",
        ///     "{category}.{twoLetterISOLanguageName}.resx",
        ///     "{category}.resx",
        /// };
        /// </example>
        ///
        /// <example>
        /// string value = await tree.LocalizedCategories[category].LocalizedValues[key].ConfigureAwait(false);
        /// </example>
        /// </summary>
        /// <param name="culture"></param>
        /// <returns>Tree of most relevant localized values</returns>
        public Task <LocalizationTree> GetLocalizationTreeAsync(CultureInfo culture)
        {
            if (culture == null)
            {
                throw new ArgumentNullException("culture");
            }

            CacheKey localizationTreeCacheKey = new CacheKey(CacheConfigurationCategoryNames.LocalizationTree)
            {
                CultureInfo = culture,
            };

            var value = CacheProvider.GetOrAddAsync(localizationTreeCacheKey, async() =>
            {
                string folderPhysicalPath = "";
                LocalizationTree tree     = new LocalizationTree(culture);

                foreach (var categoryName in LocalizationRepository.GetAllCategories())
                {
                    LocalizationCategory category = new LocalizationCategory(categoryName);

                    foreach (var source in LocalizationRepository.GetPrioritizedSources(categoryName, culture))
                    {
                        Dictionary <string, string> values =
                            await LocalizationRepository.GetValuesAsync(source).ConfigureAwait(false);
                        foreach (var kvp in values)
                        {
                            if (!category.LocalizedValues.ContainsKey(kvp.Key))
                            {
                                category.LocalizedValues.Add(kvp.Key, kvp.Value);
                            }
                        }

                        Regex meinReg = new Regex("^[a-zA-Z]{1}:\\.*");
                        if (string.IsNullOrEmpty(folderPhysicalPath) && !meinReg.IsMatch(source.VirtualPath))
                        {
                            folderPhysicalPath = HostingEnvironment.MapPath(source.VirtualPath.Substring(0, source.VirtualPath.LastIndexOf(source.Name)));
                        }
                    }

                    tree.LocalizedCategories.Add(categoryName.ToLowerInvariant(), category);
                }

                MonitorLocalizationFiles(localizationTreeCacheKey, folderPhysicalPath);
                return(tree);
            });

            return(value);
        }