///<inheritdoc/>
        public async ValueTask <IReadOnlyDictionary <string, string>?> TryLoadAsync(
            HttpHostedJsonLocalizationOptions options,
            Assembly assembly,
            string baseName,
            CultureInfo cultureInfo)
        {
            if (assembly == null)
            {
                throw new ArgumentNullException(nameof(assembly));
            }
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            var assemblyName = assembly.GetName().Name;

            var rootPath = (options.ApplicationAssembly == assembly)
                ? string.Empty
                : $"_content/{assemblyName}/";

            if (!string.IsNullOrEmpty(options.ResourcesPath))
            {
                rootPath = $"{rootPath}{options.ResourcesPath}/";
            }

            var basePath = $"{rootPath}{ResourcePathHelper.ComputeBasePath(assembly, baseName)}";

            return(await CultureInfoHelper.WalkThoughCultureInfoParentsAsync(cultureInfo,
                                                                             cultureName =>
            {
                var uri = string.IsNullOrEmpty(cultureName)
                    ? new Uri($"{basePath}.json", UriKind.Relative)
                    : new Uri($"{basePath}{options.CultureSeparator}{cultureName}.json", UriKind.Relative);

                this.logger.LogDebug($"Loading static assets data from {uri}");

                lock (Cache)
                {
                    if (!Cache.TryGetValue(uri, out var loadingTask))
                    {
                        loadingTask = TryLoadFromUriAsync(uri, options.JsonSerializerOptions);
                        Cache.Add(uri, loadingTask);
                    }
                    else if (loadingTask.IsFaulted)
                    {
                        loadingTask = TryLoadFromUriAsync(uri, options.JsonSerializerOptions);
                        Cache[uri] = loadingTask;
                    }
                    return loadingTask;
                }
            })
                   .ConfigureAwait(false));
        }
        public async Task ItShouldCallTheDataLoaderWithTheCultureInfoNameAsync()
        {
            var cultureName = "fr-FR";

            var cultureInfo = CultureInfo.GetCultureInfo(cultureName);

            var data = await CultureInfoHelper.WalkThoughCultureInfoParentsAsync <string>(
                cultureInfo,
                name =>
            {
                return(Task.FromResult <string?>(name));
            }).ConfigureAwait(false);

            Assert.Equal(cultureName, data);
        }
        public async Task ItShouldWalkThroughTheCultureInfoParentAsync()
        {
            var cultureName = "fr-FR";

            var cultureInfo = CultureInfo.GetCultureInfo(cultureName);

            var list = new List <string>();

            var data = await CultureInfoHelper.WalkThoughCultureInfoParentsAsync <string>(
                cultureInfo,
                name =>
            {
                list.Add(name);
                return(Task.FromResult <string?>(null));
            }).ConfigureAwait(false);

            Assert.Null(data);

            Assert.Equal(3, list.Count);
            Assert.Equal(cultureInfo.Name, list[0]);
            Assert.Equal(cultureInfo.Parent.Name, list[1]);
            Assert.Equal(cultureInfo.Parent.Parent.Name, list[2]);
        }
Example #4
0
        private async ValueTask <IReadOnlyDictionary <string, string>?> LoadStringMapAsync(
            IFileProvider fileProvider,
            string resourcesPath,
            string basePath,
            CultureInfo cultureInfo,
            JsonSerializerOptions?jsonSerializerOptions)
        {
            basePath = string.IsNullOrEmpty(resourcesPath)
                ? basePath
                : Path.Combine(resourcesPath, basePath);

            return(await CultureInfoHelper.WalkThoughCultureInfoParentsAsync(cultureInfo,
                                                                             cultureName =>
            {
                var path = string.IsNullOrEmpty(cultureName)
                        ? $"{basePath}.json"
                        : $"{basePath}-{cultureName}.json";

                this.logger.LogDebug($"Loading embedded data {path}");

                return TryLoadMapAsync(fileProvider, path, jsonSerializerOptions).AsTask();
            })
                   .ConfigureAwait(false));
        }