如何在 JavaScript 中设置语言环境,例如 toLocaleUpperCase()?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14548166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set locale in JavaScript, for example for toLocaleUpperCase()?
提问by Amir E. Aharoni
I'd like to use the JavaScript toLocaleUpperCase() method to make sure that the capitalization works correctly for the Turkish language. I cannot be sure, however, that Turkish will be set as the user's locale.
我想使用 JavaScript toLocaleUpperCase() 方法来确保土耳其语的大写字母正确工作。但是,我不能确定土耳其语是否会被设置为用户的语言环境。
Is there a way in modern browsers to set the locale in run time, if I know for sure that the string is in Turkish?
如果我确定字符串是土耳其语,现代浏览器是否有办法在运行时设置语言环境?
(I ran into this problem while thinking about Turkish, but actually it can be any other language.)
(我在考虑土耳其语时遇到了这个问题,但实际上它可以是任何其他语言。)
采纳答案by SimonDever
There isn't really anything much out there but I came across this JavaScript setlocale functionscript that you might find useful.
那里没有什么真正的东西,但我遇到了这个JavaScript setlocale 函数脚本,您可能会觉得它很有用。
回答by tayfun
You unfortunately cannot set locale during runtime. All hope is not lost though, there are many good libraries on npm for you to use. Check out https://www.npmjs.com/package/upper-caseand https://www.npmjs.com/package/lower-casefor example, it will work for many other languages too.
不幸的是,您无法在运行时设置语言环境。尽管如此,所有希望都没有消失,npm 上有许多不错的库供您使用。例如,查看https://www.npmjs.com/package/upper-case和https://www.npmjs.com/package/lower-case,它也适用于许多其他语言。
If that's too much, you can roll your own simple library:
如果太多,您可以推出自己的简单库:
var ALL_LETTERS_LOWERCASE = 'abc?defg?h?ijklmno?prs?tuüvyz';
var ALL_LETTERS_UPPERCASE = 'ABC?DEFG?HI?JKLMNO?PRS?TUüVYZ';
function toLowerCaseLetter(letter) {
letter_index = ALL_LETTERS_UPPERCASE.indexOf(letter);
return ALL_LETTERS_LOWERCASE[letter_index];
}
function toLowerCase(my_str) {
var lower_cased = ''
for (letter of my_str) {
lower_cased += toLowerCaseLetter(letter);
}
return lower_cased;
}
console.log(toLowerCase('?DEFG?HI?JKLMNO?PRS?TUü'))
Very similar for upper case version.
大写版本非常相似。
回答by Bernard Perrin
This option may not have existed back in 2013 but may help new visitors on this topic:
此选项在 2013 年可能不存在,但可能对有关此主题的新访问者有所帮助:
According to MDN (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLocaleUpperCase) the function toLocaleUpperCase takes an optional parameter 'locale'.
根据 MDN ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLocaleUpperCase) toLocaleUpperCase 函数采用可选参数“locale”。
Setting the right language tag is a topic on its own (https://www.w3.org/International/articles/language-tags/). Simplest example looks like this
设置正确的语言标签本身就是一个主题(https://www.w3.org/International/articles/language-tags/)。最简单的例子看起来像这样
'selam dünya'.toLocaleUpperCase('tr'); // SELAM DüNYA

