Search code examples
c++utf-8locale

Is there a way to specify custom locale directory for C++ program on macOS


In my mac, /usr/share/locale is the default locale directory where all the locales are placed. I want to specify a custom locale directory, so that a C++ program (i.e. libc/libc++) can pick up locale from there.

#include <locale>
int main() {
  std::locale a("en_US.utf-8"); // OK.
  std::locale b("en_US.utf8"); // Error. Failed to create locale for en_US.UTF8
}

This C++ program fails to construct locale for second case, because there is no en_US.utf8 folder in /usr/share/locale directory on my mac. I would want to create en_US.utf8 by coping/linking en_US.utf-8, but the directory /usr/share/locale is write protected, i.e. you cannot alter this directory even with sudo mode. Only way to alter this directory is to restart mac in safe-mode, alter this directory and restart again in normal mode. That is not a encouraged option. So i need to copy the entire /usr/share/locale directory in a custom directory and alter the locale folders in custom directory. All i need is a way to specify that custom locale directory for my C++ program.

Any solution to make my C++ program work by altering the program at one place without altering all occurrence of locale usage(utf8 -> utf-8), is a valid solution for me.

Note: Writing a wrapper on std::locale won't work for me because as I mentioned I cannot afford to alter all usage of locale in a very large C++ project.


Solution

  • I got the answer of my question.

    PATH_LOCALE=<new_path> ./binary
    

    It works for mac (BSD). Thanks.