internal static void Initialize()
 {
     Instance = new WebAssemblyCultureProvider(
         DefaultWebAssemblyJSRuntime.Instance,
         initialCulture: CultureInfo.CurrentCulture,
         initialUICulture: CultureInfo.CurrentUICulture);
 }
Exemple #2
0
        public void GetCultures_ReturnsCultureClosure(string cultureName, string[] expected)
        {
            // Arrange
            var culture = new CultureInfo(cultureName);

            // Act
            var actual = WebAssemblyCultureProvider.GetCultures(culture);

            // Assert
            Assert.Equal(expected, actual);
        }
Exemple #3
0
        public static WebAssemblyHostBuilder CreateDefault(string[]?args = default)
        {
            // We don't use the args for anything right now, but we want to accept them
            // here so that it shows up this way in the project templates.
            args ??= Array.Empty <string>();
            var builder = new WebAssemblyHostBuilder(DefaultWebAssemblyJSRuntime.Instance);

            WebAssemblyCultureProvider.Initialize();

            // Right now we don't have conventions or behaviors that are specific to this method
            // however, making this the default for the template allows us to add things like that
            // in the future, while giving `new WebAssemblyHostBuilder` as an opt-out of opinionated
            // settings.
            return(builder);
        }
Exemple #4
0
        public async Task LoadCurrentCultureResourcesAsync_DoesNotReadAssembliesWhenThereAreNone()
        {
            // Arrange
            using var cultureReplacer = new CultureReplacer("en-GB");
            var invoker = new Mock <IJSUnmarshalledRuntime>();

            invoker.Setup(i => i.InvokeUnmarshalled <string[], object, object, Task <object> >(GetSatelliteAssemblies, new[] { "en-GB", "en" }, null, null))
            .Returns(Task.FromResult <object>(0))
            .Verifiable();

            var loader = new WebAssemblyCultureProvider(invoker.Object, CultureInfo.CurrentCulture, CultureInfo.CurrentUICulture);

            // Act
            await loader.LoadCurrentCultureResourcesAsync();

            // Assert
            invoker.Verify(i => i.InvokeUnmarshalled <object, object, object, object[]>(ReadSatelliteAssemblies, null, null, null), Times.Never());
        }
Exemple #5
0
        public void ThrowIfCultureChangeIsUnsupported_ThrowsIfCulturesAreDifferentAndICUShardingIsUsed()
        {
            // Arrange
            Environment.SetEnvironmentVariable("__BLAZOR_SHARDED_ICU", "1");
            try
            {
                // WebAssembly is initialized with en-US
                var cultureProvider = new WebAssemblyCultureProvider(DefaultWebAssemblyJSRuntime.Instance, new CultureInfo("en-US"), new CultureInfo("en-US"));

                // Culture is changed to fr-FR as part of the app
                using var cultureReplacer = new CultureReplacer("fr-FR");

                var ex = Assert.Throws <InvalidOperationException>(() => cultureProvider.ThrowIfCultureChangeIsUnsupported());
                Assert.Equal("Blazor detected a change in the application's culture that is not supported with the current project configuration. " +
                             "To change culture dynamically during startup, set <BlazorWebAssemblyLoadAllGlobalizationData>true</BlazorWebAssemblyLoadAllGlobalizationData> in the application's project file.",
                             ex.Message);
            }
            finally
            {
                Environment.SetEnvironmentVariable("__BLAZOR_SHARDED_ICU", null);
            }
        }
Exemple #6
0
        public async Task LoadCurrentCultureResourcesAsync_ReadsAssemblies()
        {
            // Arrange
            using var cultureReplacer = new CultureReplacer("en-GB");
            var invoker = new Mock <IJSUnmarshalledRuntime>();

            invoker.Setup(i => i.InvokeUnmarshalled <string[], object, object, Task <object> >(GetSatelliteAssemblies, new[] { "en-GB", "en" }, null, null))
            .Returns(Task.FromResult <object>(1))
            .Verifiable();

            invoker.Setup(i => i.InvokeUnmarshalled <object, object, object, object[]>(ReadSatelliteAssemblies, null, null, null))
            .Returns(new object[] { File.ReadAllBytes(GetType().Assembly.Location) })
            .Verifiable();

            var loader = new WebAssemblyCultureProvider(invoker.Object, CultureInfo.CurrentCulture, CultureInfo.CurrentUICulture);

            // Act
            await loader.LoadCurrentCultureResourcesAsync();

            // Assert
            invoker.Verify();
        }
        // This method returns void because currently the JS side is not listening to any result,
        // nor will it handle any exceptions. We handle all exceptions internally to this method.
        // In the future we may want Blazor.start to return something that exposes the possibly-async
        // entrypoint result to the JS caller. There's no requirement to do that today, and if we
        // do change this it will be non-breaking.
        public static async void InvokeEntrypoint(string assemblyName, string[] args)
        {
            WebAssemblyCultureProvider.Initialize();

            try
            {
                var assembly   = Assembly.Load(assemblyName);
                var entrypoint = FindUnderlyingEntrypoint(assembly);
                var @params    = entrypoint.GetParameters().Length == 1 ? new object[] { args ?? Array.Empty <string>() } : new object[] { };

                var result = entrypoint.Invoke(null, @params);
                if (result is Task resultTask)
                {
                    // In the default case, this Task is backed by the WebAssemblyHost.RunAsync that never completes.
                    // Awaiting it is allows catching any exception thrown by user code in MainAsync.
                    await resultTask;
                }
            }
            catch (Exception syncException)
            {
                HandleStartupException(syncException);
                return;
            }
        }