JavaScript中的土耳其语案例转换 [英] Turkish case conversion in JavaScript

查看:206
本文介绍了JavaScript中的土耳其语案例转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我想要的语言环境中将字符串转换为JavaScript中的大小写。我认为标准函数如 toUpperCase() toLocaleUpperCase()不满足此需求。 toLocale 函数的行为不正常。

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.

例如,在Safari 4中,Chrome 4 Beta,Firefox 3.5 .x在我的系统上它会错误地转换土耳其字符串。浏览器响应navigator.language为en-UStr分别是en-US。但是我无法在浏览器中找到用户的 Accept-Lang 设置。只有Chrome为我提供了tr,尽管我已将所有浏览器土耳其语区域设置为首选。我认为这些设置只影响HTTP标头,但我们无法通过JavaScript访问这些设置。

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.

Mozilla文档它说


字符串中的字符转换为...,同时尊重当前的区域设置。对于大多数语言,这将返回相同的...

The characters within a string are converted to ... while respecting the current locale. For most languages, this will return the same as ...

我认为它对土耳其语有效,它没有区别它的配置作为en或tr。在土耳其语中,它应该将DİNÇ转换为dinçDINÇdınç,反之亦然。

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.

是否有任何满足此要求的JavaScript库需要?我认为它不仅应该在用户的语言环境中正确转换,而且还应该支持通过locale参数进行转换。因为开发人员无法访问用户配置的首选语言。

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.

推荐答案

多年后回来提供更新的解决方案。

Coming back to this years later to provide more up to date solution.

下面不需要黑客攻击,

只需使用
String.toLocaleUpperCase() String.toLocaleLowerCase()

"dinç".toLocaleUpperCase('tr-TR') // "DİNÇ"

现在所有浏览器都支持此功能。

All browsers support this now.

[旧,不要使用]

尝试这些函数

String.prototype.turkishToUpper = function(){
    var string = this;
    var letters = { "i": "İ", "ş": "Ş", "ğ": "Ğ", "ü": "Ü", "ö": "Ö", "ç": "Ç", "ı": "I" };
    string = string.replace(/(([iışğüçö]))+/g, function(letter){ return letters[letter]; })
    return string.toUpperCase();
}

String.prototype.turkishToLower = function(){
    var string = this;
    var letters = { "İ": "i", "I": "ı", "Ş": "ş", "Ğ": "ğ", "Ü": "ü", "Ö": "ö", "Ç": "ç" };
    string = string.replace(/(([İIŞĞÜÇÖ]))+/g, function(letter){ return letters[letter]; })
    return string.toLowerCase();
}

// Example
"DİNÇ".turkishToLower(); // => dinç
"DINÇ".turkishToLower(); // => dınç

我希望它们适合你。

这篇关于JavaScript中的土耳其语案例转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆