admin管理员组

文章数量:1289525

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.

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

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.

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

Share Improve this question asked Jan 27, 2013 at 14:06 Amir E. AharoniAmir E. Aharoni 1,3183 gold badges14 silver badges25 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 1

There isn't really anything much out there but I came across this JavaScript setlocale function script that you might find useful.

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./package/upper-case and https://www.npmjs./package/lower-case for example, it will work for many other languages too.

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.

This option may not have existed back in 2013 but may help new visitors on this topic:

According to MDN (https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLocaleUpperCase) the function toLocaleUpperCase takes an optional parameter 'locale'.

Setting the right language tag is a topic on its own (https://www.w3/International/articles/language-tags/). Simplest example looks like this

'selam dünya'.toLocaleUpperCase('tr'); // SELAM DÜNYA

本文标签: stringHow to set locale in JavaScriptfor example for toLocaleUpperCase()Stack Overflow