示例#1
0
        /// <summary>
        /// Make sure all asynchronous localizer loading is completed.
        /// </summary>
        /// <param name="localizer">Localizer to load.</param>
        /// <param name="loadParentCulture">If true (false by default) the LoadAsync method will load the parent culture.</param>
        /// <returns>The value task to await.</returns>
        /// <remarks>The parent culture is actually used as fall back if a key is not found.</remarks>
        public static ValueTask LoadAsync(this IStringLocalizer localizer, bool loadParentCulture = false)
        {
            if (localizer == null)
            {
                throw new ArgumentNullException(nameof(localizer));
            }

            return(JsonStringLocalizerAsync.LoadAsync(localizer, loadParentCulture));
        }
示例#2
0
        public async Task ItShoultLoadLocalizerDataAsync()
        {
            var key = "TestKey";
            var txt = "Test txt value.";

            var cultureInfo = CultureInfo.GetCultureInfo("fr-FR");

            using var waitHandler = new ManualResetEvent(false);

            async Task <IReadOnlyDictionary <string, string> > loadAsync()
            {
                // Make sure the task is running asynchronously.
                await Task.Delay(1).ConfigureAwait(false);

                // Wait until signal.
                waitHandler.WaitOne();

                // Return data
                return(new Dictionary <string, string>()
                {
                    [key] = txt
                });
            };

            var localizerFactoryMock = new Mock <IJsonStringLocalizerFactoryInternal>();

            var loadingLocalizerMock = new Mock <IStringLocalizer>();

            loadingLocalizerMock.SetupGet(x => x[key]).Returns(new LocalizedString(key, "..."));

            // Setup the async localizer with the loadAsync task.
            var localizer = new JsonStringLocalizerAsync(
                loadAsync(),
                cultureInfo,
                localizerFactoryMock.Object,
                loadingLocalizerMock.Object);

            // Make sure the localized text is loading...
            var loading = localizer[key];

            Assert.Equal("...", loading.Value);

            // Signal the async task.
            waitHandler.Set();

            // Make the localized text is loaded.
            await JsonStringLocalizerAsync.LoadAsync(localizer, false).ConfigureAwait(false);

            // Make sure the localized text is the one expected.
            var value = localizer[key];

            Assert.Equal(txt, value.Value);
        }