Windows 中的 Javascript 本地化
我正在寻找一种本地化 Windows 边栏小工具的方法。它主要是用 Javascript 编写的。我特别想要的是一种获取用户的“语言代码”(例如 en-us)的方法,因为该语言代码将用于从服务器获取本地化的 XML 文件。
如果有人对如何获取此语言代码有任何想法,或者更好的是,有一些完成此任务的代码,那就太好了。提前致谢。
编辑:看起来索要代码的人删除了他的评论,所以我删除了示例代码,因为它只是混乱,没有贡献。
I'm looking for a way to localize a Windows Sidebar Gadget. It is written primarily in Javascript. What I specifically want is a way to get the user's "language code" (eg. en-us) because the language code will be used to get a localized XML file from a server.
If anyone has any ideas for how to get this language code or, even better, some code that accomplishes this task that would be great. Thanks in advance.
Edit: It looks like whoever asked for code deleted his comment, so I deleted the sample code as it was just clutter that didn't contribute.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有多种方法可以实现此目的,您希望使用哪种方法取决于您的需求以及您的设置允许的功能。
navigator.language
将告诉您安装的浏览器使用的语言 - 而不是用户从首选项中选择的语言。因此,这只会给您一个提示,但对您来说可能已经足够了。地理位置。您可以使用多种 API 来实现此目的,但同样,它们不会告诉您用户设置的浏览器语言首选项,而是告诉您它们位于世界的哪个位置。再说一遍,这可能是您需要的,也可能不是。
在服务器上查看 js-file/html-page/etc 的请求标头。具体来说,查看 Accept-Language 标头,它反映了用户在浏览器中设置的所需语言首选项。从您的服务器,将其放入变量中为您的 js 播种。唉,这个值无法从 javascript 本身读取,因此您需要服务器。
There are several ways of accomplishing this, which method you wish to use depends on your needs and what your setup allows you.
navigator.language
will tell you what language the installed browser has - not what the user has chosen as language from the preferences tho. So this will only give you a hint, but it might be enough for you.Geolocation. There are several APIs you can use for this, but again, they will not tell you the browsers language preference set by the user, but where they are in the world. again, this might be what you need and might not.
On the server look at the request headers for the js-file/html-page/etc. Specifically, look at the
Accept-Language
header which reflects the users desired language preference as set in the browser. From your server, seed your js with this in a variable. Alas, this value cannot be read from javascript itself, so you need the server for it.这是不可能的。您无法从浏览器获取该设置,因此它不适用于 Javascript。定义一种标准语言并让用户选择另一种语言。
It's not possible. You can't get that setting from the Browser, so it's not available to Javascript. Define a standard language and let the user to choose another.