如何通过编程找到客户端机器的选定语言?

时间:2021-10-29 01:54:11

I have set arabic as as client's machine language.But in C# program while am using

我已经将阿拉伯语设置为客户端的机器语言。但是在使用C#程序时

var test = Thread.CurrentThread.CurrentCulture;

It shows the language as En-US.?
How i get the selected language of the machine?.

它将语言显示为En-US。我如何获得机器的选定语言?

2 个解决方案

#1


1  

This will depend on how the client browser is configured. If the client browser's default language is configured to be en-US you will never be able to get the actual language on the server. For example in Google Chrome there's a setting where you could specify the preferred languages sent to the server:

这取决于客户端浏览器的配置方式。如果客户端浏览器的默认语言配置为en-US,您将永远无法获得服务器上的实际语言。例如,在Google Chrome中,您可以指定发送到服务器的首选语言的设置:

如何通过编程找到客户端机器的选定语言?

So once you have configured the preferred language of your web browser to be something else, the browser will send this language as Accept-Language HTTP request header and you will be able to retrieve it on the server. In this case ASP.NET will automatically assign it to the current thread's culture assuming in your web.config you have not changed it in the <globalization> element but left the default value.

因此,一旦您将Web浏览器的首选语言配置为其他语言,浏览器就会将此语言作为Accept-Language HTTP请求标头发送,您将能够在服务器上检索它。在这种情况下,ASP.NET将自动将其分配给当前线程的文化,假设在您的web.config中,您没有在 元素中更改它,但保留了默认值。

#2


0  

Request.UserLanguages is the property you're looking for.
Just keep in mind that this array may contain arbitrary (even non-exsitent) languages as set by request headers.

Request.UserLanguages是您正在寻找的房产。请记住,此数组可能包含由请求标头设置的任意(甚至非exsitent)语言。

Example:

例:

var lobUserLanguages = Request.UserLanguages;
CultureInfo ci;
if (lobUserLanguages.Count > 0)
{
    try
    {
        ci = new CultureInfo(lobUserLanguages[0]);
    }
    catch(CultureNotFoundException)
    {
         ci = CultureInfo.InvariantCulture;
    }
}
else
{
    ci = CultureInfo.InvariantCulture;
}

#1


1  

This will depend on how the client browser is configured. If the client browser's default language is configured to be en-US you will never be able to get the actual language on the server. For example in Google Chrome there's a setting where you could specify the preferred languages sent to the server:

这取决于客户端浏览器的配置方式。如果客户端浏览器的默认语言配置为en-US,您将永远无法获得服务器上的实际语言。例如,在Google Chrome中,您可以指定发送到服务器的首选语言的设置:

如何通过编程找到客户端机器的选定语言?

So once you have configured the preferred language of your web browser to be something else, the browser will send this language as Accept-Language HTTP request header and you will be able to retrieve it on the server. In this case ASP.NET will automatically assign it to the current thread's culture assuming in your web.config you have not changed it in the <globalization> element but left the default value.

因此,一旦您将Web浏览器的首选语言配置为其他语言,浏览器就会将此语言作为Accept-Language HTTP请求标头发送,您将能够在服务器上检索它。在这种情况下,ASP.NET将自动将其分配给当前线程的文化,假设在您的web.config中,您没有在 元素中更改它,但保留了默认值。

#2


0  

Request.UserLanguages is the property you're looking for.
Just keep in mind that this array may contain arbitrary (even non-exsitent) languages as set by request headers.

Request.UserLanguages是您正在寻找的房产。请记住,此数组可能包含由请求标头设置的任意(甚至非exsitent)语言。

Example:

例:

var lobUserLanguages = Request.UserLanguages;
CultureInfo ci;
if (lobUserLanguages.Count > 0)
{
    try
    {
        ci = new CultureInfo(lobUserLanguages[0]);
    }
    catch(CultureNotFoundException)
    {
         ci = CultureInfo.InvariantCulture;
    }
}
else
{
    ci = CultureInfo.InvariantCulture;
}