在asp.net中创建一个多语言的站点。

时间:2022-03-25 04:11:45

I am creating a website that will be multilingual in nature. I am providing a functionality whereby the user can select desired language by selecting it from the drop down.

我正在创建一个多语种的网站。我提供了一个功能,用户可以通过从下拉菜单中选择所需的语言。

Now my problem starts here (when a language is selected from the drop down). I am currently implementing 2 languages English and Arabic.

现在我的问题从这里开始(从下拉菜单中选择语言)。我目前正在使用英语和阿拉伯语两种语言。

Problem is when the user select Arabic from the drop down on the login page the page is refreshed and the browser loads all the content in Arabic.

问题是,当用户从登录页面的下拉菜单中选择阿拉伯文时,页面将被刷新,浏览器将以阿拉伯文装载所有内容。

But...

但是…

When i select English back again, the page refreshes but the language content does not change !!

当我再次选择英语回来,页面刷新,但语言内容没有改变!!

i have check the code and the values (culture name value) are being applied correctly!!

我已经检查了代码和值(文化名值)正在被正确地应用!

Any clues as to what is wrong and where...

关于什么是错的以及哪里……

Here is my code...

这是我的代码…

protected override void InitializeCulture()
{
    String selectedLanguage = string.Empty;

    if (Request.Form["ddlLanguage"] != null)
    {
        selectedLanguage = Request.Form["ddlLanguage"];

        CultureInfo ci = new CultureInfo(selectedLanguage);

        Thread.CurrentThread.CurrentCulture = ci;
        Thread.CurrentThread.CurrentUICulture = ci;
    }

    base.InitializeCulture();
}

Any help will be great !!

任何帮助都将是伟大的!!

Thanks

谢谢

2 个解决方案

#1


2  

The Culture settings must set on each request. It's not enough to set the Thread cultures once when the selection changes.

区域性设置必须针对每个请求进行设置。当选择发生变化时,仅仅设置一次线程区域性是不够的。

In order to set the culture according to the user selection on each request, there are several possible ways.

为了根据用户对每个请求的选择来设置区域性,有几种可能的方法。

  1. Set the culture in (for instance) the Page_Init event of the master page.
  2. 在主页面的Page_Init事件中设置文化(例如)。
  3. Create custom base page (maybe MyBasePage) for the content pages and override the InitializeCulture method of that class. Then make all the content pages derive from the class instead of directly from Page.
  4. 为内容页面创建自定义基页(可能是MyBasePage),并覆盖该类的InitializeCulture方法。然后使所有的内容页面从类派生而不是直接从页面派生。
  5. Add code for setting the culture in a suitable event in Global.asax.
  6. 在Global.asax中添加设置文化的代码。
  7. Probably there are several other possible ways to handle this as well.
  8. 可能还有其他几种可能的方法来处理这个问题。

Regardless of which method you will use, the culture that the user has chosen must be available to the code that is going to set the culture on the thread. So when the user changes his/her selection, you must save that selection in a place where you can access it in the coming requests. This can also be solved in several possible ways. Here are some options:

无论您使用哪种方法,用户所选择的文化必须对将要在线程上设置文化的代码可用。因此,当用户更改他/她的选择时,您必须将该选择保存在一个您可以在即将到来的请求中访问它的位置。这也可以通过几种可能的方式来解决。这里有一些选项:

  1. If you are using a ProfileProvider, you could save the selection to the user's profile.
  2. 如果您正在使用ProfileProvider,您可以将选择保存到用户的配置文件中。
  3. Alternatively you can store it in a cookie.
  4. 或者你可以将它存储在一个cookie中。
  5. You could possibly store it in the Session although that would mean that the user must re-select it whenever the Session has been reset.
  6. 您可以将它存储在会话中,尽管这意味着用户必须在会话被重置时重新选择它。
  7. You could use some other custom way to store the selection (database, some file on disk etc).
  8. 您可以使用其他自定义方式来存储选择(数据库、磁盘上的文件等)。

For a more detailed exmple of how this could be done using Global.asax and a cookie, have a look over here.

有关如何使用Global实现此功能的更详细的示例。asax和饼干,看这里。

#2


0  

Check this article - How to create ASP.NET MVC multilingual web application ? We will see mainly two approaches-

检查这篇文章——如何创建ASP。NET MVC多语言web应用程序?我们将主要看到两种方法

Approach 1: Using Static Pages

方法1:使用静态页面

Approach 2: Using Dynamic page with localized data at runtime

方法2:在运行时使用带有本地化数据的动态页面

#1


2  

The Culture settings must set on each request. It's not enough to set the Thread cultures once when the selection changes.

区域性设置必须针对每个请求进行设置。当选择发生变化时,仅仅设置一次线程区域性是不够的。

In order to set the culture according to the user selection on each request, there are several possible ways.

为了根据用户对每个请求的选择来设置区域性,有几种可能的方法。

  1. Set the culture in (for instance) the Page_Init event of the master page.
  2. 在主页面的Page_Init事件中设置文化(例如)。
  3. Create custom base page (maybe MyBasePage) for the content pages and override the InitializeCulture method of that class. Then make all the content pages derive from the class instead of directly from Page.
  4. 为内容页面创建自定义基页(可能是MyBasePage),并覆盖该类的InitializeCulture方法。然后使所有的内容页面从类派生而不是直接从页面派生。
  5. Add code for setting the culture in a suitable event in Global.asax.
  6. 在Global.asax中添加设置文化的代码。
  7. Probably there are several other possible ways to handle this as well.
  8. 可能还有其他几种可能的方法来处理这个问题。

Regardless of which method you will use, the culture that the user has chosen must be available to the code that is going to set the culture on the thread. So when the user changes his/her selection, you must save that selection in a place where you can access it in the coming requests. This can also be solved in several possible ways. Here are some options:

无论您使用哪种方法,用户所选择的文化必须对将要在线程上设置文化的代码可用。因此,当用户更改他/她的选择时,您必须将该选择保存在一个您可以在即将到来的请求中访问它的位置。这也可以通过几种可能的方式来解决。这里有一些选项:

  1. If you are using a ProfileProvider, you could save the selection to the user's profile.
  2. 如果您正在使用ProfileProvider,您可以将选择保存到用户的配置文件中。
  3. Alternatively you can store it in a cookie.
  4. 或者你可以将它存储在一个cookie中。
  5. You could possibly store it in the Session although that would mean that the user must re-select it whenever the Session has been reset.
  6. 您可以将它存储在会话中,尽管这意味着用户必须在会话被重置时重新选择它。
  7. You could use some other custom way to store the selection (database, some file on disk etc).
  8. 您可以使用其他自定义方式来存储选择(数据库、磁盘上的文件等)。

For a more detailed exmple of how this could be done using Global.asax and a cookie, have a look over here.

有关如何使用Global实现此功能的更详细的示例。asax和饼干,看这里。

#2


0  

Check this article - How to create ASP.NET MVC multilingual web application ? We will see mainly two approaches-

检查这篇文章——如何创建ASP。NET MVC多语言web应用程序?我们将主要看到两种方法

Approach 1: Using Static Pages

方法1:使用静态页面

Approach 2: Using Dynamic page with localized data at runtime

方法2:在运行时使用带有本地化数据的动态页面