JavaScript 中的土耳其语大小写转换

发布于 2024-08-14 01:06:55 字数 942 浏览 6 评论 0原文

我想在我想要的语言环境中将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦亿 2024-08-21 01:06:56

几年后回到这里提供更多最新的解决方案。

不需要下面的 hack,

只需使用
String.toLocaleUpperCase() 和 < a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLocaleLowerCase" rel="noreferrer">String.toLocaleLowerCase()

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

所有现代浏览器现在支持这个。


[旧,请勿使用此]

尝试这些功能,

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ç

希望它们对您有用。

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()

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

All modern browsers support this now.


[ OLD, DO NOT USE THIS ]

Try these functions

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ç

I hope they will work for you.

最初的梦 2024-08-21 01:06:56

感谢您的功能。我真的很喜欢它。连续的土耳其语字符输入结果“未定义”为“ÇÇ”。尝试将“/+g”替换为“/g”。这些功能是:

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();
}

Thanks for the function. I really liked it. Consecutive Turkish char input results 'undefined' as 'ÇÇ'. Try replacing '/+g' with '/g'. The functions would be:

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();
}
对你而言 2024-08-21 01:06:56
String.prototype.tUpper = function(){
   return this.replace(/i/g,"İ").toLocaleUpperCase();
}

String.prototype.tLower = function(){
    return this.replace(/I/g,"ı").toLocaleLowerCase();
}
String.prototype.tUpper = function(){
   return this.replace(/i/g,"İ").toLocaleUpperCase();
}

String.prototype.tLower = function(){
    return this.replace(/I/g,"ı").toLocaleLowerCase();
}
梦萦几度 2024-08-21 01:06:56
    var  a="lişliş lğüğpğp İŞİŞİŞ lşi ĞĞHFGH ÜGFHFHG ühüüüğ üğüğş ş ş Ş  İ i  ılk Ilk";

var s=a.split(' ');

var netice="";

s.forEach(function(g) {
  if (g.length>1)
    netice+=g[0].toLocaleUpperCase('tr')+
      g.slice(1).toLocaleLowerCase('tr')+" "; 
  else
    netice+=g.toLocaleUpperCase('tr')+" ";
});

alert(netice); 
    var  a="lişliş lğüğpğp İŞİŞİŞ lşi ĞĞHFGH ÜGFHFHG ühüüüğ üğüğş ş ş Ş  İ i  ılk Ilk";

var s=a.split(' ');

var netice="";

s.forEach(function(g) {
  if (g.length>1)
    netice+=g[0].toLocaleUpperCase('tr')+
      g.slice(1).toLocaleLowerCase('tr')+" "; 
  else
    netice+=g.toLocaleUpperCase('tr')+" ";
});

alert(netice); 
素食主义者 2024-08-21 01:06:56

请查看这段可以转换为大写和小写的小代码

var manualLowercase = function(s) {
  return isString(s)
      ? s.replace(/[A-Z]/g, function(ch) {return String.fromCharCode(ch.charCodeAt(0) | 32);})
      : s;
};

var manualUppercase = function(s) {
  return isString(s)
      ? s.replace(/[a-z]/g, function(ch) {return String.fromCharCode(ch.charCodeAt(0) & ~32);})
      : s;
};

Please look into this small piece of code that can convert to upppercase and lowercase

var manualLowercase = function(s) {
  return isString(s)
      ? s.replace(/[A-Z]/g, function(ch) {return String.fromCharCode(ch.charCodeAt(0) | 32);})
      : s;
};

var manualUppercase = function(s) {
  return isString(s)
      ? s.replace(/[a-z]/g, function(ch) {return String.fromCharCode(ch.charCodeAt(0) & ~32);})
      : s;
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文