简体   繁体   English

根据浏览器语言切换 div 标签的可见性

[英]Toggle visibility of div tags based on Browser Language

First off, I'm a PM and the dev team is already mad at me.首先,我是 PM,开发团队已经生我的气了。 So I'd like to take a shot creating the solution to the question below.所以我想尝试创建以下问题的解决方案。

We have a static maintenance mode html page that displays "Please check back soon" in 24 languages.我们有一个静态维护模式 html 页面,以 24 种语言显示“请尽快回来”。 Right now we are displaying all 24 translations at once and the page is pretty ugly.现在我们一次显示所有 24 个翻译,页面非常难看。

I've wrapped each translation into it's own div tag like this:我已经将每个翻译包装到它自己的 div 标签中,如下所示:

<div id="en">
  <p>Please check back soon</p>
</div>
<div id="es">
  <p>Compruebe por favor más adelante</p>
</div>
...

I'm looking to use javascript to detect the browser language and set visibility:visible;我希望使用 javascript 来检测浏览器语言并设置可见性:可见; for the div that corresponds to the browser language.对于对应于浏览器语言的 div。 And set all the other div tags to visibility:hidden;并将所有其他 div 标签设置为可见性:隐藏;

Looking for help with detecting browser language and stripping out locale (if provided) so I have a 2 character value to use in a complex switch.寻求有关检测浏览器语言和剥离区域设置(如果提供)的帮助,因此我有一个 2 个字符的值可用于复杂的开关。

My solution would be to use the Accept-Language header in the HTTP request to find the user's preferred language and then just return one div in the correct language.我的解决方案是在 HTTP 请求中使用 Accept-Language 标头来查找用户的首选语言,然后只返回一个正确语言的 div。 This way you don't have to worry about hiding divs.这样您就不必担心隐藏 div。 Also, if you hide divs they will all be displayed initially and then only set to hidden after the Javascript executes so it won't be as good of a user experience.此外,如果您隐藏 div,它们将最初全部显示,然后仅在 Javascript 执行后才设置为隐藏,因此用户体验不会那么好。

Please see: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html请参阅: http : //www.w3.org/Protocols/rfc2616/rfc2616-sec14.html

First, this seems to be a server side solution rather than a client side solution.首先,这似乎是服务器端解决方案而不是客户端解决方案。 However, a quick search did turn up this snippet:然而,快速搜索确实出现了这个片段:

var userLang = (navigator.language) ? navigator.language : navigator.userLanguage;
alert ("The language is: " + userLang);

on this page .此页面上

So if you really must do it on the client then this might help.因此,如果您确实必须在客户端上执行此操作,那么这可能会有所帮助。

First off, you shouldn't wrap in a div , you should just put the attribute directly on the tag.首先,你不应该用div包装,你应该直接把属性放在标签上。 Secondly, if you're in HTML you can use the lang attribute, and if you're in XHTML you can use the xml:lang attribute... This is much more descriptive than id .其次,如果你在 HTML 中,你可以使用 lang 属性,如果你在 XHTML 中,你可以使用xml:lang属性......这比id更具描述性。 Finally, it's probably best to do the processing server-side if you can, using Accept-Language .最后,如果可以,最好使用Accept-Language进行服务器端的处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM