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. In this post we will give an overview of how that can be achieved while developing in .NET.

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

  • It can display contents 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, the Culture and UICulture. The culture value determines the results of culture-dependent functions, such as the date, number, and currency formatting. The UICulture determines which resources are to be loaded for the page by the ResourceManager.

Localized content in .NET is kept in files called resource files (.resx extension) consisting of XML entries, which specify objects and strings inside XML tags. tags represent keys and values are kept inside them in tags. Most popular IDE when working in .NET, Microsoft Visual Studio, provides an editor for such files to be edited in a more readable format Key = Value. Key is what will be used in the application to retrieve the localized value.

This is one example of a resource file key in it's binary form:

\[gist id=30aa00ffb55e6167adf2\]

Setup i18n in .NET/MVC

First of all we need to determine Culture and UICulture. .NET can do that automatically from the users browser by a web.config setting

[gist id=d5bed2444ff105e9d2f4]

or you can keep track of the user selected Culture and UICulture via cookies for example.

Now we can continue and create resource files. Each file should clearly indicate to what culture it belongs to using this naming convention .-<country_code>.resx for example Messages.en-US.resx indicating this file contains application messages translated to US English.

Set the access modifier of the resource file to Public if resources should be visible in more projects. Here is an example of a resource being edited from Visual Studio:

Using i18n in .NET/MVC

It is in the views where resource files will mostly be used. This is an example of referencing a resource file value in the view by referencing the project called MyResources where resource files are kept, a file called Resources and it's keys LogOn and LogOff. Resource files if public can be in a separate project and referenced in other projects or private inside a project that is being localized.

[gist id=900b1b25e6864576e6b1]

Using i18n with interpolation

Often there is a need for for variables inside our resources, for example a user logged in welcome message "You have signed in as..." where we want to pass the name of the user that has signed in. It can be achieved by adding placeholders to localization strings as such SignedInText="You have signed in as {0}" and then using it inside a familiar String.Format method.

[gist id=28e824b8d1484d63a874]

Using i18n with pluralization

.NET and resource files do not have pluralization support included so pluralization logic should be added by the developer.

Try lingohub 14 days for free. No credit card. No catch. Cancel anytime