Best way to implement a multilingual in ASP.NET ap

2019-02-23 12:24发布

问题:

The following things/behaviors are expected in multi lingual application.

  1. To pick up the correct resources for the logged in user’s language
  2. The string sort should happen according to user language (For example: in case of Swedish users, the Swedish alphabets should come after z in Order).
  3. The date time format validation should happen according to the user’s language. (For example: fr-FR users can enter dd/mm/yyyy and en-US users can enter mm/dd/yyyy)
  4. The decimal validation again should happen according to user’s language (For example: 12,5 is valid in French whereas 12.5 is invalid)

Now the question is what is the best way to handle this.

Approach 1:

  1. Set the page culture and page UICulture – so that the App_GlobalResources are picked up and decimal values in grid are correctly formatted.
  2. Write custom methods to fire validation for the following
    • Datetime
    • Decimal
  3. Write custom method to handle string sorting.

Approach 2:

  1. Set the current thread’s culture and let this handle the following { Does each user of the web app have their culture correctly set in this case?}

Approach 3:

  1. Initialize the globalization attribute in web.config and letting the application pick culture based on the user’s browser culture

    <globalization uiculture="auto" culture="auto" enableClientBasedCulture =”true”/>
    

Please let me know if there are better alternatives and how should we approach solving such problems or the pros and cons of the above approaches.

回答1:

Overload the InitializeCulture() page event. Use a session var based on the autheticated user, which stores the currentliy selected culture and reset both Thread.Culture and UICulture in the event handler.

As far as I know, these conversion issues or ToString() format issues should no exist. The current thread culture will handle all for you.

Some resources:
Walkthrough: Using Resources for Localization with ASP.NET
How to: Set the Culture and UI Culture for ASP.NET Web Page Globalization