Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to Localize .NET Web Application (Both Server-Side and Client-Side)

In the past few days i have be implementing Localization for my web-application written in MVC. There are 3 main task that brake-down the Lozalization "Problem" that I could point out:

  1. Storage: Store Key-Value pair of strings in multiple languages to be used in your application
  2. Server-Side Access: Access the Value according to a given Key in your Server Side Script (e.g. Views, Controllers, Web-forms etc...)
  3. Client-Side Access: Access the Value according to a given Key in your client Side Script (e.g. Java-script)
  4. create a culture switching mechanism.

There are several methods to accomplish each task that I have explored and I'm struggling with choosing between them. There are two questions that I'm interested in:

  1. I would like to know what would be the best practice for localization? from your point of view. Please write Advantages\ Disadvantages for one method vs. the other.
  2. Are there other methods or enhancements that you could suggest.

1. Storage

Resource Files:

  • Store Key-Value pairs of localized strings in a resource file
  • Create a resource file with the same keys for each culture, e.g. myString.resx, myStrings.he-IL.resex etc...

XML Files:

  • Create a structured XML file that will contain a parent node for each key and childe nodes for each culture specific value e.g.

    <string key="myAppName">
        <heIL>some Hebrew value</heIL>
        <enUS>some English value</enUS>
    </string>
    

2. Server Side Access

  • With both Storage options it would be fairly easy to get access to from the server side.
    • Using resource files a bit easier in that case becuse there are classess that are auto-generated to facilitate access to the values. So you use resources as regular clasess with static properties to access each string value according to a key.
    • With XML files you will have to write down the Access Layer code yourself, which is not necessarily a bad thing - because you get much more control that way (only if you need it)

3. Client Side Access

Here is where it gets interesting and sometimes complicated. There where two options that i have explored here and I'm not sure how to proceed.

Server-Side Generated Script:

  • In your View\ Web-Form add a server script render block inside a <script> tag
  • From this point dynamically create an Array of keys, or static variables in java script and place the required string inside them. Here is an example using Razor engine.

       <script type="text/javascript">
                k_CultureInfo = "@CultureInfo.CurrentCulture.Name";
                K_ApplicationName = "@MyStrings.SomeStringKey";
                ....
       </script>
    

Custom HTTP Handler:

I found the idea in a blog pos here: Localize text in JavaScript files in ASP.NET Here

  • Bestially your write an HTTP handler that handles any ".js.axd" request comes to the server.
  • The "ScriptTranslatorHandler" will read the javascrip file and wil replace any instance of a predefined token e.g. Translate(SomeStringKey) in your java script to the correct string taken by any of the methods described above.
like image 406
Mortalus Avatar asked Nov 27 '22 10:11

Mortalus


1 Answers

Resource files:

Use the resx format. Translation tools will not be able to process the format you proposed since it contains multiple languages in the same file. A typical translation tool would take a source file and generate a matching file for each language after translation. I would avoid the format that you suggested at all costs.

If you want to use an XML, use a single file per language. Also, whatever format you choose, make sure you include context comments for each segment to help translators.

Server Side Access

Use the resx and save yourself the time to define the layer

Client Side Access

Server side generated script is simpler and more consistent with the sever side access in my opinion.

Culture switching mechanism

I assume you are asking about the logic and not the UI. The most common method is to get the user's browser language setting. If the language is set to a language that does not exist in your application, fall back to your default language. You also want to set a cookie with the users selection (in case they change the language manually) so that they are presented with their language of choice next time.

like image 97
eakkas Avatar answered Dec 04 '22 04:12

eakkas