How to set locale in JavaScript, for example for toLocaleUpperCase()?

独自空忆成欢 提交于 2019-12-05 02:36:02

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.com/package/upper-case and https://www.npmjs.com/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.org/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.org/International/articles/language-tags/). Simplest example looks like this

'selam dünya'.toLocaleUpperCase('tr'); // SELAM DÜNYA
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!