Exemplo n.º 1
0
        private IEnumerable <LocalizedString> GetResourceStringsFromCultureHierarchy(
            CultureInfo startingCulture, bool includeParentCultures)
        {
            foreach (var str in Resource.GetRootStrings())
            {
                yield return(new LocalizedString(str.Path, str.Value, true, Resource.ResourceName));
            }

            if (includeParentCultures)
            {
                var currentCulture = startingCulture;
                var parentCulture  = currentCulture.Parent;

                /*
                 * Example from ResourceManagerStringLocalizer:
                 * https://github.com/aspnet/Localization/blob/51549e8471c247f91d5ac57bd6f8f4c68508854b/src/Microsoft.Extensions.Localization/ResourceManagerStringLocalizer.cs#L236
                 */
                while (!parentCulture.Equals(currentCulture) && !currentCulture.Parent.Equals(CultureInfo.InvariantCulture))
                {
                    var resource = _resourceManager.GetResource(_baseName, _location, parentCulture);
                    foreach (var str in resource.GetRootStrings())
                    {
                        yield return(new LocalizedString(str.Path, str.Value, true, Resource.ResourceName));
                    }
                    currentCulture = parentCulture;
                    parentCulture  = currentCulture.Parent;
                }
            }
        }
        /// <inheritdoc />
        public IStringLocalizer Create(string baseName, string location)
        {
            //location is a prefix to a resource name
            var resource = _resourceManager.GetResource(baseName, location, CultureInfo.CurrentUICulture);

            if (resource != null)
            {
                return(new JsonFileStringLocalizer(_loggerFactory, _resourceManager, resource, baseName, location));
            }
            return(null);
        }
        /// <inheritdoc />
        public IObjectLocalizer Create(string baseName, string location)
        {
            var resource = _resourceManager.GetResource(baseName, location, CultureInfo.CurrentUICulture);

            if (resource != null)
            {
                return(new JsonFileObjectLocalizer(
                           _loggerFactory, _resourceManager, resource, baseName, location));
            }
            return(null);
        }
        /// <inheritdoc />
        public IObjectLocalizer WithCulture(CultureInfo culture)
        {
            var resource = _resourceManager.GetResource(_baseName, _location, culture);

            if (resource != null)
            {
                return(new JsonFileObjectLocalizer(
                           _loggerFactory, _resourceManager, resource,
                           _baseName, _location));
            }
            return(null);
        }