Home / Blog / Internationalization (i18n) in .NET MVC (resx)

Internationalization (i18n) in .NET MVC (resx)

Developer resources
Helmut Juskewycz
CEO & Founder of LingoHub

Last updated

4/23/2013

Read time

5 min

Best for

Developers

You create - Lingohub localizes
selection 022

How i18n works in .NET/MVC: If you are creating a .NET/MVC application with multilingual users, you would certainly want to localize your content. This overview explains how that can be achieved while developing in .NET.

.NET provides components to support i18n out of the box with the following features:

  • It can display content in different languages.

  • It autodetects the language from the user’s browser.

  • It allows the user to override the language of their browser.

Internationalization consists of Globalization, which is supporting multiple cultures, and Localization, which is customizing your application for a given culture.

ASP.NET keeps track of two culture values: Culture and UICulture. The Culture value determines the results of culture-dependent functions such as date, number, and currency formatting. The UICulture determines which resources are loaded for the page by the ResourceManager.

Localized content in .NET is stored in resource files (.resx extension), which are XML-based files containing keys and values. <data> tags represent keys, and values are stored inside <value> tags. Microsoft Visual Studio provides a visual editor for these files in a Key = Value format. The key is what the application uses to retrieve localized values.

This is one example of a resource file entry in its binary form:

Resource file gist example


Setup i18n in .NET/MVC

First, we need to determine Culture and UICulture. .NET can automatically detect this from the user’s browser via a web.config setting:

web.config culture configuration gist

Alternatively, you can store and manage user-selected Culture and UICulture via cookies.

Next, create resource files. Each file should clearly indicate its culture using this naming convention:

<resource-filename>.<culture>-<country_code>.resx

Example:

Messages.en-US.resx

This file would contain application messages translated into US English.

Set the access modifier of the resource file to Public if the resources should be accessible across multiple projects. Otherwise, they can remain private within the project.


Using i18n in .NET/MVC

Resource files are mostly used in views. Below is an example of referencing resource values in a view:

.NET MVC resource usage gist

Resource files can either:

  • Live inside the same project, or

  • Be placed in a separate shared project and referenced by multiple projects.


Using i18n with interpolation

Often you need variables inside localized strings, such as:

“You have signed in as…”

This can be done using placeholders in resource strings:

SignedInText = "You have signed in as {0}"

Then use String.Format to inject values:

.NET interpolation with resources gist


Using i18n with pluralization

.NET resource files do not include built-in pluralization support. Therefore, pluralization logic must be implemented manually by the developer (for example, using conditional logic based on numeric values).

Related articles