Internationalization (i18n) in C# (.NET): A Complete Guide

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Internationalization (i18n) in C# (.NET)

Internationalization (abbreviated as i18n — from the English "internationalization", where 18 is the number of letters between the first "i" and the last "n") is the process of designing software so that it can be adapted to various languages, regions, and cultural differences without requiring changes to the source code. In the .NET platform (C#), i18n is implemented through localization and globalization mechanisms built into the core of the framework.



Key Concepts of i18n in .NET

  • Globalization — designing an application to support multiple cultures (e.g., formatting dates, numbers, currencies).
  • Localization — adapting the interface to a specific culture (translating texts, images, sounds).
  • Culture — a combination of language and regional settings, denoted by codes like ru-RU, en-US, de-DE.
  • Resource files (.resx) — XML files containing key-value pairs for each locale.


How i18n Works in C# (.NET)

In .NET, resource files are used for localization. The main file (e.g., Strings.resx) contains strings in the default language (usually English). For each additional language, a separate file is created with the culture code suffix: Strings.ru.resx (Russian), Strings.de.resx (German). During compilation, these files are transformed into satellite assemblies that are loaded depending on the current thread culture.



// Example of a resource file (Strings.resx)// Key: HelloMessage, Value: "Hello, World!"// Strings.ru.resx// Key: HelloMessage, Value: "Привет, мир!"


Step-by-Step Localization Implementation in a Console Application

  1. Create a resource file in the project: right-click on the project → Add → New Item → Resources File. Name it Strings.resx.
  2. Add strings: insert the key Greeting and the value Hello, user!.
  3. Create a file for the Russian language: Strings.ru.resx with the same key and the value Привет, пользователь!.
  4. Write code to set the culture and retrieve the localized string:


using System.Globalization;using System.Resources;using System.Threading;

class Program{ static void Main() { // Set the Russian culture Thread.CurrentThread.CurrentUICulture = new CultureInfo("ru-RU"); Thread.CurrentThread.CurrentCulture = new CultureInfo("ru-RU");

// Get the resource manager ResourceManager rm = new ResourceManager("YourNamespace.Strings", typeof(Program).Assembly); string greeting = rm.GetString("Greeting");

Console.WriteLine(greeting); // Outputs: "Привет, пользователь!" }}


Localization of ASP.NET Core Web Applications

In ASP.NET Core, i18n is implemented through middleware and built-in services. Let's look at an example of configuration for MVC or Razor Pages.



1. Configuration in Program.cs

using Microsoft.AspNetCore.Localization;using System.Globalization;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");builder.Services.AddControllersWithViews() .AddViewLocalization() .AddDataAnnotationsLocalization();

var app = builder.Build();

var supportedCultures = new[] { new CultureInfo("en-US"), new CultureInfo("ru-RU") };app.UseRequestLocalization(new RequestLocalizationOptions{ DefaultRequestCulture = new RequestCulture("en-US"), SupportedCultures = supportedCultures, SupportedUICultures = supportedCul

Recommendations