Best way to localize a .NET web application (both server side and client side)

In the last few days, I have been implementing localization for my web application written in MVC. There are three main tasks that inhibit the problem of "Localization", which I could indicate:

  • Storage: Saving a pair of keys and values ​​in several languages ​​that will be used in your application.
  • Access to the server side: Access to the value according to the specified key on your side of the Script server (for example, views, controllers, web forms, etc.)
  • Client access: Access to the value according to this key on the client side Script (for example, Java-script)
  • create a culture switching mechanism.

There are several methods for accomplishing each task that I have studied, and I am struggling with a choice between them. I am interested in two questions:

  • I would like to know what would be the best practice for localization? from your point of view. Please write down the Advantages \ Disadvantages for one method compared to another.
  • Are there other methods or improvements 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, for example. myString.resx, myStrings.he-IL.resex, etc.

XML files:

  • Create a structured XML file that will contain a parent node for each key and child nodes for each specific culture value, for example.

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

2. Server access

  • With two storage options, it would be fairly easy to access from the server side.
    • Using resources is a little easier in this case, because there are classes that are automatically generated to facilitate access to values. Thus, you use resources as regular classes with static properties to access each string value according to the key.
    • With XML files, you have to write the access level code yourself, which is not necessarily bad, because you get much more control in this way (only if you need it)

3. Client access

Here it becomes interesting, and sometimes difficult. Where there are two options that I studied here, and I'm not sure how to proceed.

Server-generated Script:

  • In your View \ Web form, add a Script server rendering block inside the <script>
  • From this point, dynamically create an array of keys or static variables in java Script and put the desired string in them. Here is an example of using the Razor engine.

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

Custom HTTP Handler:

I found this idea on a blog here: Localize Text in JavaScript Files in ASP.NET Here

  • First of all, you write an HTTP handler that processes any ".js.axd" request that comes to the server.
  • "ScriptTranslatorHandler" will read the javascrip file and replace any instance of the predefined token, for example. Translate (SomeStringKey) in java Script to the correct line, taken by any of the methods described above.
+4
source share
2 answers

Resource Files:

Use resx format. Translation tools will not be able to process the format that you suggested, as it contains several languages ​​in one file. A regular translation tool will take the source file and generate the corresponding file for each language after the translation. I would avoid the format that you offered at all costs.

If you want to use XML, use one file for each language. Also, in whatever format you choose, be sure to include contextual comments for each segment to help translators.

Server access

Use resx and save time to define a layer

Client Access

The server-side script generated is simpler and more consistent with side-side access, in my opinion.

Culture switching mechanism

I assume that you are asking about logic, not about the user interface. The most common method is to configure the user's browser language. If the language is set to a language that does not exist in your application, return to the default language. You also want to set a cookie with a choice of users (in case they change the language manually), so that next time they will be offered their choice of language.

+1
source

maybe this will help if its webapp https://developers.google.com/chrome/web-store/docs/i18n

try it !!!

-1
source

Source: https://habr.com/ru/post/1437976/


All Articles