Skip to main content Skip to footer

How to Use Localization Features in Your .NET C# Application

Localization in .NET Applications

When discussing localization, Microsoft’s own guidelines (Localization - .NET ) mention that an application should be split into two pieces: a block containing the user interface portions and a block containing the code to be executed. .NET uses a common language runtime that supports satellite assembly resource models that distinguishes an application’s resources from its code.

For every language you want your application to support, you’ll need a new satellite assembly containing the localized user interface block for the application. The satellite assemblies are often split into different resource files, also known as .resx files. These are XML files that are translated before the code is executed by the consuming application, allowing the application to translate its contents at runtime.  

Ready to Try It Out? Download ComponentOne Today!

ComponentOne Controls have Built-In Localization

With ComponentOne libraries, dozens of fully translated localization files are distributed with each release, allowing you to quickly change the language of your software utilizing ComponentOne controls with a single line of code.

In this example, we’ll change the text shown within our FlexPivotPanel from the default English to German. Before we initialize the component in our application, we’ll want to ensure we use this single line of code from the System.Threading library:

Thread.CurrentThread.CurrentUICulture = new CultureInfo("de");

Now when we run the application, we notice that the ComponentOne controls are displaying their built-in text in German instead of English:

.NET C# Localization

And that’s it! All it takes to apply localization to ComponentOne controls is one line of code. If you’d like to learn more about localizing the rest of your application that isn’t using ComponentOne controls, continue reading below.

How to Extend Localization to the Rest of Your Application

While localizing ComponentOne controls to German or any other language of choice is simple, configuring the rest of your .NET application may prove more challenging. First, you’ll need to ensure your application has the proper resource files required for the language being imported.

If these don’t exist already, you will have to make your own and save it as a .resx file. For example, the following could be the English .resx  (XML) file you intend to have in your application:

<?xml version="1.0" encoding="utf-8"?>

<root>
  <data name="WelcomeMessage" xml:space="preserve">
    <value>Welcome to GrapeCity!</value>
  </data>
</root>

Here is the German .resx file you would also include with your application as a resource if you were planning on potentially deploying the same application into German:

<?xml version="1.0" encoding="utf-8"?>

<root>
  <data name="WelcomeMessage" xml:space="preserve">
    <value>Willkommen bei GrapeCity!</value>
  </data>
</root>

This resource file will automatically be resolved, but if your project file name is different from the root namespace of the project, the assembly name may differ. In this scenario, you will have to specify the RootNamespace for the localization file.

For example, if the project is a translator.csproj (which would create a translator.dll and translator.exe), but the localization was to occur within a different namespace, titled “Real.Localization” you would use the following line of code:

[assembly: RootNamespace("Real.Localization")]

Next, we’ll have to register the localization services by utilizing one of the AddLocalization extension methods, which will allow dependency injection of both  Microsoft.Extensions.Localization.IStringLocalizer<T> and Microsoft.Extensions.Localization.IStringLocalizerFactory.

using IHost testHost = Host.CreateDefaultBuilder(args)
    .ConfigureServices(services =>
    {
        services.AddLocalization(options =>
        {
            options.ResourcesPath = "Resources";
        });
    });

After successfully registering the localization services, we can now use either of the two types labeled above to successfully apply the localization to our application, as shown in the example code below:

using System.Diagnostics.CodeAnalysis;
using Microsoft.Extensions.Localization;

namespace Real.Localization;

public sealed class TranslastionService
{
    private readonly IStringLocalizer<TranslationService> _localize = null!; 
    public MessageService(IStringLocalizer<TranslationService> localize) =>
        _localize = localize;
 
    [return: NotNullIfNotNull(nameof(_localize))]
    public string? GetWelcomeMessage()
    {
        LocalizedString translatedString = _localize["WelcomeMessage"];
        return translatedString;
    }
}

As you can see, applying localization throughout your application is not as simple as the ComponentOne controls’ approach of applying one line of code.

If this is your first time hearing about ComponentOne controls, you can explore the full suite’s capabilities and functionality by downloading our free 30-day trial.

Happy coding!

comments powered by Disqus