Saturday, April 16, 2011

I18n and L10n of the .NET application

Today I was very happy to see the orange sunset once again after a very long time. It is a scientific fact that the color of the visible light spectrum which is reflected the most is the one that is seen most prominently through the transparent surface. The sun never changes its appearance but it is the light travelling from it and reaching our eyes through the atmospheric particles during the entire daytime makes it to appear in different colors at the different times. This is the most common example where the external appearance of the entity is dependent on the external factors. This is quite analogous to the process of developing and deploying the large scale multi-lingual applications in the production environment. In such scenarios the content to be displayed is isolated from the executable code.

Internationalization is the process of designing a software application so that it can be adapted to various languages and regions without engineering changes. Localization is the process of adapting internationalized software for a specific region or language by adding specific components and translating text (ref: Wikipedia)

The products which are targeted for the users all across the globe may need to be designed in a way they can be used by the target audiences from different geographical locations. This may bring up different challenges for a developer and a content writer working on the multilingual applications. For a developer he may need to make his code generic enough so as to fit the text data written in the different languages. For a content writer it is probably the text encoding and other parameters that matters the most. From the developers perspective the well externalized application is the master key to achieve the internationalization of any product. Let’s have a look at .NET-way of doing it.

The .NET application can be divided into two parts:

1) Executable code: The code which gets compiled in the form of binaries.

2) Content: The literal or static text which is separated into different unit of deployment which is often known as satellite assemblies. The satellite assemblies are often deployed separately and independently from the product feature release.

Project structure:

1) Code files: This chiefly consists of C# code files which compiles in the form of binaries

2) Resource files: This contains the content to be rendered based on the culture set by the user. The naming convention should be followed precisely so as to load it for a particular culture e.g. the content written in US English should contain en-US in the name of its resource file. The satellite assemblies that are created after the successful build are placed inside the respective folders with the same name.

The hub and spoke model of deployment

While writing such kind of applications it becomes mandatory for a developer/content writer to identify all the user interface texts which needs to be externalized. Essentially it means that the developer cannot hardcode the literal string value anywhere inside the code but instead he needs a placeholder to refer all those values from outside the executable code. The code sample is illustrated as below and very simple to understand.

System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-GB");
ResourceManager resourceManager = new ResourceManager("Core.Content", Assembly.GetExecutingAssembly());
var userName = resourceManager.GetString("UserName");

The above code can be made to work for all the available cultures by setting the CultureInfo from the http request or taking it from the regional settings for the current user and creating the resource files for each of them. The satellite assemblies can also be created separately and copied to the \bin folder of the application so that it is picked by the application upon the next user request.

This is the another example of the beauty of the .NET framework which abstracts all the internal implementation from the developer and gives a nice and wide range of framework classes to work with the multi-lingual applications.

Find what varies and encapsulate it!

No comments: