Search results

Localization in Blazor Chart component

28 Jul 2021 / 3 minutes to read

We have used Resource file (.resx) to translate the static text of the Chart.

The Resource file is an XML file which contains the strings(key and value pairs) that you want to translate into different language. You can also refer Localization link to know more about how to configure and use localization in the ASP.Net Core application framework.

  • Add .resx file to Resources folder and enter the key value (Locale Keywords) in the Name column and the translated string in the Value.

Blazor Server Side

Following examples demonstrate how to enable Localization for chart in server side Blazor samples.

  • Open the Startup.cs file and add the below configuration in the ConfigureServices function as follows.
Copied to clipboard
using Syncfusion.Blazor;
using System.Globalization;
using Microsoft.AspNetCore.Localization;

namespace BlazorApplication
{
public class Startup
{
    ....
    ....
    public void ConfigureServices(IServiceCollection services)
    {
        ....
        ....
        services.AddSyncfusionBlazor();
        services.AddLocalization(options => options.ResourcesPath = "Resources");
        services.Configure<RequestLocalizationOptions>(options =>
        {
            // define the list of cultures your app will support
            var supportedCultures = new List<CultureInfo>()
            {
                new CultureInfo("de")
            };
            // set the default culture
            options.DefaultRequestCulture = new RequestCulture("de");
            options.SupportedCultures = supportedCultures;
            options.SupportedUICultures = supportedCultures;
            options.RequestCultureProviders = new List<IRequestCultureProvider>() {
             new QueryStringRequestCultureProvider() // Here, You can also use other localization provider
            };
        });
        services.AddSingleton(typeof(ISyncfusionStringLocalizer), typeof(SampleLocalizer));
    }
}
}

Add UseRequestLocalization() middle-ware in Configure method in Startup.cs file to get browser Culture Information.

  • Then, write a class by inheriting ISyncfusionStringLocalizer interface and override the Manager property to get the resource file details from the application end.
Copied to clipboard
using Syncfusion.Blazor;

namespace BlazorServer
{
public class SampleLocalizer : ISyncfusionStringLocalizer
{

    public string GetText(string key)
    {
        return this.ResourceManager.GetString(key);
    }

    public System.Resources.ResourceManager ResourceManager
    {
        get
        {
            return BlazorServer.Resources.SfResources.ResourceManager;
        }
    }
}
}

Blazor WebAssembly

Following examples demonstrate how to enable Localization for chart in Client side Blazor samples.

  • Open the Program.cs file and add the below configuration in the Main function as follows.
Copied to clipboard
using Syncfusion.Blazor;
using System.Globalization;

namespace ClientApplication
{
public class Program
{
    public static async Task Main(string[] args)
    {
        var builder = WebAssemblyHostBuilder.CreateDefault(args);
        builder.RootComponents.Add<App>("app");

        builder.Services.AddTransient(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
        builder.Services.AddSyncfusionBlazor();


        // Register the Syncfusion locale service to customize the  SyncfusionBlazor component locale culture
        builder.Services.AddSingleton(typeof(ISyncfusionStringLocalizer), typeof(SyncfusionLocalizer));

        // Set the default culture of the application
        CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("de");
        CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("de");

        await builder.Build().RunAsync();
    }
}
}
  • Then, create a ~/Shared/SyncfusionLocalizer.cs file and implement ISyncfusionStringLocalizer interface to the class and override the ResourceManager property to get the resource file details from the application end.
Copied to clipboard
using Syncfusion.Blazor;

public class SyncfusionLocalizer : ISyncfusionStringLocalizer
{
// To get the locale key from mapped resources file
public string GetText(string key)
{
    return this.ResourceManager.GetString(key);
}  

// To access the resource file and get the exact value for locale key

public System.Resources.ResourceManager ResourceManager
{
    get
    {
        // Replace the ApplicationNamespace with your application name.
        return ClientApplication.Resources.SfResources.ResourceManager;
    }
}
}

You can refer more details about localization here.

 Note: You can refer to our Blazor Charts feature tour page for its groundbreaking feature representations. You can also explore our Blazor Chart example to knows various chart types and how to represent time-dependent data, showing trends in data at equal intervals.

See Also