使用 Request.UserLanguages 根据浏览器语言呈现页面的最有效方法是什么?

发布于 2024-11-25 17:55:48 字数 953 浏览 0 评论 0原文

我正在制作一个页面,通过 Request.UserLanguages...从用户的浏览器中提取他们的首选语言......它返回两个字母代码(例如“en”)或详细代码(例如“en-GB”)。

我基本上获取用户语言的字符串(它们按偏好顺序排列)并将它们存储在字符串数组中。然后,我使用循环来检查字符串数组第一个位置的语言代码是否是某种语言的任何代码(另一个硬编码的字符串数组)。

有更好的方法吗?我注意到加载时间增加,并且担心其他语言会进一步减慢页面加载速度...

 if (!IsPostBack)

    {   //Holds possible user languages preferences to check client machine against
        String[] compJapaneseLang = { "ja-jp","ja","jp","jpn","euc","shift-jis" };
    }

 //Get client machines langugage preferences                
        String[] userLang = Request.UserLanguages;

//Loop through variation of preferences from possible user langugaes

        for (int i = 0; i < compJapaneseLang.Length; i++)
        { 
                //IF JAPANESE
            if (userLang.GetValue(0).ToString().ToLowerInvariant().Equals(compJapaneseLang.GetValue(i).ToString().ToLowerInvariant()))


                cc.JapeneseObject();
        }

谢谢!

I am making a page which pulls from the user's browser their preferred language, via the Request.UserLanguages....which returns a two letter code (ex. "en") or detailed code (ex. "en-GB") .

I basically get the string of user languages (they are in order of preference) and store them in a string array. Then I use a loop to check if the language code in the first position of the string array is any of the codes for a certain language (another string array hard coded in).

Is there a better way to do this? I'm noticing increased load time and am worried additional languages will further slow the page load...

 if (!IsPostBack)

    {   //Holds possible user languages preferences to check client machine against
        String[] compJapaneseLang = { "ja-jp","ja","jp","jpn","euc","shift-jis" };
    }

 //Get client machines langugage preferences                
        String[] userLang = Request.UserLanguages;

//Loop through variation of preferences from possible user langugaes

        for (int i = 0; i < compJapaneseLang.Length; i++)
        { 
                //IF JAPANESE
            if (userLang.GetValue(0).ToString().ToLowerInvariant().Equals(compJapaneseLang.GetValue(i).ToString().ToLowerInvariant()))


                cc.JapeneseObject();
        }

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

懒猫 2024-12-02 17:55:49

将它们存储在一个列表中效果最好,除此之外我们无能为力......

Storing them in a list turned out best, not really much else one can do....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文