Turkish case conversion in JavaScript

25,569

Solution 1

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.

Solution 2

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

Solution 3

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

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

Solution 4

    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); 
Share:
25,569
sanilunlu
Author by

sanilunlu

Updated on December 31, 2021

Comments

  • sanilunlu
    sanilunlu over 2 years

    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.

  • László Papp
    László Papp about 10 years
    Please note that the language used on this site is English.
  • Ahmet Can Güven
    Ahmet Can Güven over 6 years
    @scokmen gotcha
  • toolforger
    toolforger almost 5 years
    This answer is wrong for the question as stated, as it does not deal with iışğüçö, which is specifically what the question is about. You should delete this answer.