Architecture FAQ for Localization and Globalization: Part II

Introduction

In case if you have not seen Part 1 of Localization and Globalization, click here

When we see around, architectures mainly discuss about loose coupling , scalability , performance etc etc. Many architecture forget one of the important aspects in software is making application Globalized. Depending on project some application would not really want to have multi-language based websites , but I am sure many will. So in this article we will go through a series of FAQ which will give you a quick start on making application multi-language based.

(B) What are satellite assemblies?

(A) How do we generate Satellite assemblies?

(A) What is AL.EXE and RESGEN.EXE?

In the previous question, you have seen how we can use resource files to store data according to the localized languages. However, when you actually go for deployment you will not like to also install the "resx" or "txt" files. It is definitely not a good deployment practice to install data, which can be easily modified. In short some how we should install this in a binary format so that no end user can change it. That is why Microsoft introduced satellite assemblies.

Satellite assemblies are assemblies, which do not contain source code. They only contain resource files. You can create a satellite assembly using rsgen.exe and al.exe. They are in binary DLL format, which makes it easier to ship it during deployment. So finally, during deployment you do not need to ship the resx files but only the compiled satellite DLL.

Figure 14.15: - resgen.exe and al.exe in action

The above diagram will give you a complete picture of how to generate Satellite assembly. You can see from the above figure we need two exe resgen.exe and al.exe. Once you made your resx file or text file, you should first convert in to a ".resource" files. This is done by using the resgen.exe. Below is the command snippet for resgen.exe where LoginScreen.aspx.el.resx is the resx file and output is Greek. Resources file. If you do not provide the output file name it will generate "LoginScreen.resources".

Resgen LoginScreen.aspx.el.resx Greek.Resources

You can also generate resx files from txt file using resgen.exe below is the code snippet for the same:-

Resgen MyLanguage.txt MyLanguage.resx

The above command snippet will generate a MyLanguage.resx using MyLanguag.txt file. You can make a DLL using resource files and not resx so you should make this conversion.

Now once the resource file is generated its time make the compiled assembly of the same so that it can be shipped during deployment. This is accomplished by using the assembly linker tool al.exe provided by Microsoft. Below is the command code snippet for the same.

al.exe /out: el.dll /c: de /embed: greek.resources

In the /out switch you need to provide the output DLL name. /c you need to specify the culture of the resource file. /embed you need to specify all the resources which are present in the resource file. As said previously other than strings you can also put image files like GIF, BMP etc. So those physical resources you can specify in the /embed switch. You can specify more than one resource use "," as a separator to specify more than one resource files.

(I) What is the use of resource manager class?

Resource Manager Class helps us to read the resource files and get the values using key.

First, you need to create the object of resource manager. You need to specify the resource name and the assembly in the constructor.

  1. privateResourceManagerobjResourceManager = new ResourceManager("Globalization.resource", System.Reflection.Assembly.GetExecutingAssembly());  
Once the resource manager is populated with details, you can then use the Get String function to get by key. For instance in the below code snippet we are using the "cmdAddNew" key to get the value for button "cmdAddNew".
  1. cmdAddNew.Text = objResourceManager.GetString("cmdAddNew");  
(A) What precautions do we need to take while deploying satellite assemblies?

When we deploy the assembly, the folder structure has to very organized. Below table shows how the folder structure should be organized. Main Folder is the main application folder. All satellite assemblies should be deployed in the Main application folder with in there own respective folder. The respective folder is denoted by the culture code.

Figure 14.16: - Satellite Assembly folder structure

You can see from the above figure Hindi satellite assembly is deployed in hi folder, Greek satellite assembly is deployed in el folder and so on. If the program does not find resource file for a culture it uses the invariant culture satellite assembly. The above folder structure is a strict requirement when we deploy the satellite assembly. Any mismatch in the folder structure will lead to in appropriate results.

(A) Can we get a strongly typed resource class rather than using resource manager?

In the previous question, we had seen how resource manager class could be used to read the string from the resource file. However, there has been considerable improvement in VS.Net 2005 for resource support. You no more need to load using resource manager class. However, Microsoft has still kept it for backward compatibility. You can now get strongly types classes in your VS.NET intelligence as shown in the figure below.

Figure 14.17: - Strongly typed resource class

All belong to Resources namespace. Let us do a small sample and see how the strongly typed classes work in VS.NET 2005 and the simplicity, which they bring while implementing globalization in projects. Below is the screen shot of the project? It is a simple login screen with user id and password text boxes. User has options to select the language. Currently only two languages are provided English and Greek. Depending on the languages selected, the user id and password label values will be displayed.

Figure 14.18: - Strongly typed project

Note: - In the globalization project you can get the project sample in "LoginScreenUsingStrongType.aspx".

Below is the code snippet, which describes the various important sections of the code. First thing are the resource files. We have generated two resource files one for Greece with el and second is the general resource file which will be used when the regional code does not match.

Figure 14.19: - Walkthrough for using the resources namespace

There are three important steps in the code:-

  • First is set the culture information for the current thread with the new culture info object. Structure has the language code, which is currently selected in the drop down.
    1. Thread.CurrentThread.CurrentCulture = new CultureInfo(structure);  
  • We set the same culture to the Resource class.
    1. Resources.Resource.Culture = Thread.CurrentThread.CurrentCulture;
  • Now we are all set to use the value.
    1. lblUserId.Text = Resources.Resource.lblUserIdResource1.ToString ();  
    2.   
    3. blPassword.Text = Resources.Resource.lblPasswordResource1.ToString ();

Note: - You can get the same from globalization folder in "LoginScreenUsingStrongType.aspx". Try to add a new language and most of the fundamentals will be clear.

(A) Can you explain the fundamentals of "GetGlobalResourceObject" and "GetLocalResourceObject" functions?

These two functions belong to the Http Context object. Using it you can get the object reference of the resource object. For instance you can see from the below code snippet we have reference to the Global resource object and we are trying to get the value for "lblUserIdresource1" key.

  1. lblUserId.Text = HttpContext.GetGlobalResourceObject("Resource""lblUserIdResource1").ToString();  
Note:- In the same globalization folder there is "LoginScreenUsingGetGlobal.aspx" which demonstrates how "GetGlobalResource" works.

One short note because "GetGlobalResourceObject" and "GetLocalResourceObject" operate from with in current HttpContext. It uses what the regional settings are sent from the browser end.

(A) Can we sign a satellite assembly?

Yes you can sign the satellite assembly using the /key file switch which takes ".snk" file as the input parameter.

al /res:MyLanguage.resources /c:de /keyfile:MyLang.snk out:MyLanguages.resources.dll

(I) Can you explain collation sequence in SQL server?

First, let us define collation.

Collation sequences are set of rules, which determine how the data is sorted and compared. Sorting rules can be defined with options with case-sensitivity, accent marks, kana character types, and character width.

Case sensitivity

If A and a, B and b, etc. are treated in the same way then it is case-insensitive. A computer treats A and a differently because it uses ASCII code to differentiate the input. The ASCII value of A is 65, while a is 97. The ASCII value of B is 66 and b is 98.

Accent sensitivity

If a and , o and are treated in the same way, then it is accent-insensitive. A computer treats a and a differently because it uses ASCII code for differentiating the input. The ASCII value of a is 97 and is 225. The ASCII value of o is 111 and is 243.

Kana Sensitivity

When Japanese kana characters Hiragana and Katakana are treated differently, it is called Kana sensitive.

Width sensitivity

When a single-byte character (half-width) and the same character when represented as a double-byte character (full-width) are treated differently then it is width sensitive.

(A)How do we define collation sequence for database and tables?

You can create a database with language specific collation sequence. For instance in the below create statement tblCustomer is created by Latin language collation sequence.

Create database tblCustomer collate Latin1_General_BIN

You can also create tables with particular collation sequence. Below is the create table syntax for the same.

  1. Create table tblCustomer   
  2. (  
  3. [CustomerCode] char(10) COLLATE Albanian_CI_AI_KS_WS NULL,  
  4. [EntryDate] [char] (8) COLLATE Korean_Wansung_Unicode_CS_AS_KS NOT NULL ,  
  5. [CustAbbrev] [char] (2) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL   
  6. )  
(A)Can we change the order in a select query with a specified collation sequence?

Yes we can specify a collate sequence in the order by clause. That will change the sort according to the collation defined in the order by clause.

  1. ORDER BY   
  2. {  
  3. order_by_expression   
  4. COLLATE collation_name ]   
  5. ASC | DESC ]  
  6. } [ ,...n ] ]  
(A) Can you list best practices for globalization and localization?

Below are the best practices while developing international language support software:-

  • Do not hardcode strings or user interface resources.
  • Make sure your application depends on Unicode.
  • Whenever you read or write data from various encoding make, sure you use the System. Text namespace. Many programmers assume ASCII data.
  • While testing test it with actual international data and environments.
  • Whenever we manipulate data, for instance numbers, dates or currency make sure that you are using culture-aware classes defined in System. Globalization namespace. Below is the table, which specifies in more detail about the functionality and the classes to be used to achieve the same.


    Figure 14.20: - Functionality and classes used
  • If a security decision is based on the result of a string comparison or case change operation, perform a culture-insensitive operation by explicitly specifying the CultureInfo.InvariantCulture property. This practice ensures that the result is not affected by the value of CultureInfo.CurrentCulture.
  • Move all your localizable resources to separate DLL's.
  • Avoid using images and icons that contain text in your application. They are expensive to localize.
  • Allow plenty of room for the length of strings to expand in the user interface. In some languages, phrases can require 50-75 percent more space.
  • Use the System.Resources.ResourceManager class to retrieve resources based on culture.
  • Explicitly set the CurrentUICulture and Current Culture properties in your application. Do not rely on defaults.
  • Be aware that you can specify the following three types of encodings in ASP.NET:
  • Request Encoding specifies the encoding received from the client's browser.
  • Response Encoding specifies the encoding to send to the client browser. In most situations, this should be the same as request Encoding.
  • File Encoding specifies the default encoding for .aspx, .asmx, and .asax file parsing.
(A) Why is the culture set to the current thread?

First, let me explain this question. If you look at the code snippet of how to set the culture info.
  1. Thread.CurrentThread.CurrentCulture = new CultureInfo(strCulture);  
It uses the current thread to set it. What does that mean? Let us drill down a bit, of how IIS handles request to understand this concept. When any user requests a resource from IIS like an ASPX page or any other resource. IIS services that request in his own thread. That means if 100 users have requested some resource from IIS he will serve every request in its own thread. In short, IIS will spawn 100 threads to service the 100 request. It is very much practically possible that you can have different locale in different threads. Therefore, when we set a culture we cannot set it for the whole application, as it will affect all the requests. So when we set a culture we set it to a particular thread rather to the whole application.


Similar Articles