Search code examples
javascriptnode.jsinternationalizationecmascript-intl

String.toLocaleUpperCase(): Set locale on Node.js


The JavaScript String toLocaleUpperCase() method is supposed to account for case mapping on different locales, like Turkish (i is not mapper to dotless I, but to dotted İ in Turkish).

However, it seems the locale is captured from the OS only: I don't see a way to set up the locale. So if I am using a en-US OS and want to toLocaleUpperCase() a Turkish string in "tr", it will return the en-US version (dotless I, not dotted İ).

Assuming I am using Node.js compiled with INTL (https://github.com/nodejs/node/wiki/Intl), how could I define the locale to toLocaleUpperCase()?


Solution

  • This isn't quite an answer right now. But ECMA-402 now specifies (as of the spec's 2nd edition) the behavior of String.prototype.toLocaleUpperCase. ECMA-402 changes the function to take a locale argument (undefined, string, or arraylike of locales) -- similar to what Number.prototype.toLocaleString and the various other locale-sensitive functions in ECMA-262 take, when modified by ECMA-402 -- and behaves consistent with that locale. Once Node implements that (it doesn't right now), you can simply change

    var str = "bira";
    str.toLocaleUpperCase(); // "BIRA"
    

    to

    var str = "bira";
    str.toLocaleUpperCase("tr-TR"); // BİRA
    

    and be on your way.

    (Disclaimer: I don't know Turkish at all, I just picked a word containing "i" off a Turkish word list, and I'm presuming locale-sensitive uppercasing works the way I expect it does, for the purpose of the example above.)