Exemplo n.º 1
0
 internal static void Initialize()
 {
     Instance = new WebAssemblyCultureProvider(
         DefaultWebAssemblyJSRuntime.Instance,
         initialCulture: CultureInfo.CurrentCulture,
         initialUICulture: CultureInfo.CurrentUICulture);
 }
Exemplo n.º 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);
    }
Exemplo n.º 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.
        var jsRuntime = DefaultWebAssemblyJSRuntime.Instance;
        var builder   = new WebAssemblyHostBuilder(
            jsRuntime,
            jsRuntime.ReadJsonSerializerOptions());

        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);
    }
    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());
    }
Exemplo n.º 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);
        }
    }
    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();
    }