JavaScript 中的土耳其语大小写转换
我想在我想要的语言环境中将 JavaScript 中的字符串转换为小写或大写。我认为像 toUpperCase()
和 toLocaleUpperCase()
这样的标准函数不能满足这个需求。 toLocale
函数的行为不正常。
例如,在我的系统上的 Safari 4、Chrome 4 Beta、Firefox 3.5.x 中,它会错误地转换带有土耳其语字符的字符串。浏览器对 navigator.language 的响应分别为 "en-US"
、"tr"
、"en-US"
。但据我所知,无法在浏览器中获取用户的 Accept-Lang 设置。尽管我已经配置了首选的每个浏览器土耳其语言环境,但只有 Chrome 为我提供了 "tr"
。我认为这些设置只影响 HTTP 标头,但我们无法通过 JavaScript 访问这些设置。
在 Mozilla 文档中,它说
字符串中的字符将转换为 ...,同时尊重当前区域设置。对于大多数语言,这将返回与...相同的结果
我认为它对土耳其语有效,它配置为 en 或 tr 没有区别。在土耳其语中,它应该将 "DıNÇ"
转换为 "dinç"
并将 "DINÇ"
转换为 "dınç"
或反之-反之亦然。
有没有JavaScript库可以满足这个需求?我认为它不仅应该在用户的区域设置中正确转换,而且还应该支持通过区域设置参数进行转换。因为开发人员无法访问用户配置的首选语言。
I want to convert strings to lower or upper case in JavaScript in the locale I wanted. I think standard functions like toUpperCase()
and toLocaleUpperCase()
do not satisfy this need. toLocale
functions do not behave as they should.
For example, in Safari 4, Chrome 4 Beta, Firefox 3.5.x on my system it converts strings with Turkish characters incorrectly. The browsers respond to navigator.language as "en-US"
, "tr"
, "en-US"
respectively. But there is no way to get user's Accept-Lang
setting in the browser as far as I could found. Only Chrome gives me "tr"
although I have configured every browser Turkish locale preferred. I think these settings only affect HTTP header, but we can't access to these settings via JavaScript.
In the Mozilla documentation it says
The characters within a string are converted to ... while respecting the current locale. For most languages, this will return the same as ...
I think it's valid for Turkish, it doesn't differ it's configured as en or tr. In Turkish it should convert "DİNÇ"
to "dinç"
and "DINÇ"
to "dınç"
or vice-versa.
Is there any JavaScript library that satisfies this need? I think it should not only converting correctly in user's locale, but also it should support conversion via a locale parameter. Because developers cannot access to user's configured preferred language.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
几年后回到这里提供更多最新的解决方案。
不需要下面的 hack,
只需使用
String.toLocaleUpperCase() 和 < a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLocaleLowerCase" rel="noreferrer">String.toLocaleLowerCase()
所有现代浏览器现在支持这个。
[旧,请勿使用此]
尝试这些功能,
希望它们对您有用。
Coming back to this years later to provide more up to date solution.
There is no need for the hack below,
just use
String.toLocaleUpperCase() and String.toLocaleLowerCase()
All modern browsers support this now.
[ OLD, DO NOT USE THIS ]
Try these functions
I hope they will work for you.
感谢您的功能。我真的很喜欢它。连续的土耳其语字符输入结果“未定义”为“ÇÇ”。尝试将“/+g”替换为“/g”。这些功能是:
Thanks for the function. I really liked it. Consecutive Turkish char input results 'undefined' as 'ÇÇ'. Try replacing '/+g' with '/g'. The functions would be:
请查看这段可以转换为大写和小写的小代码
Please look into this small piece of code that can convert to upppercase and lowercase