Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Localization is platform-dependent #14

Open
leeper opened this issue Sep 25, 2015 · 1 comment
Open

Localization is platform-dependent #14

leeper opened this issue Sep 25, 2015 · 1 comment

Comments

@leeper
Copy link
Member

leeper commented Sep 25, 2015

In setlang, the current code sets the LANG environment variable. On Linux, this may also (or alternatively) need to set a locale, e.g.:

Sys.setlocale("LC_MESSAGES", "ko_KR.utf8")

There's also this issue about platform-dependent printing of UTF-8 messages.

h/t @richierocks

@richierocks
Copy link
Collaborator

On Windows it's LC_CTYPE rather than LC_MESSAGES.

I tried dynamically setting LC_CTYPE to something sensible before printing error messages, but it doesn't work. As ?Sys.setlocale states:

Attempts to change the character set (by ‘Sys.setlocale("LC_TYPE",
)’, if that implies a different character set) during a session
may not work and are likely to lead to some confusion.

So you have to set the locale before you do anything useful. My current thought is that (at least under Wnidows) if the LANG environment variable is set, then you should check the LC_CTYPE locale setting any time you load a package with translations and wrn the user that they need to set this to something sensible.

For Linux it's enough to check that LC_MESSAGES is in a UTF-8 locale. I think all OS X locales are UTF-8 anyway, but I have no idea about BSD or Solaris.

check_localization <- function(code)
{
  if(.Platform$OS.type == "windows")
  {
    lang <- Sys.getenv("LANG", NA)
    if(!is.na(lang))
    {
      lc_ctype <- Sys.getlocale("LC_CTYPE")
      # TODO: all the rest of ISO 639-1
      new_lc_ctype <- switch(
        lang,
        de = "German",
        en = "English",
        fr = "French",
        hu = "Hungarian",
        ko = "Korean",
        nl = "Dutch",
        ru = "Russian",
        sv = "Swedish",
        tr = "Turkish",
        uk = "Ukranian"
      )
      if(!grepl(new_lc_ctype, lc_ctype))
      {
        warning(
          "You have set the LANG environment variable to ",
          lang,
          " but the LC_CTYPE locale setting is ",
          lc_ctype,
          " which may result in error messages being incorrectly displayed.\n",
          'Consider setting Sys.setlocale("LC_CTYPE", "', 
          new_lc_ctype,
          '").'
        )
      }
    }
  } else if(Sys.info()["sysname"] == "Linux")
  {
    lc_messages <- Sys.getlocale("LC_MESSAGES")
    # Possibly better to check l10n_info()$`UTF-8`?
    if(!grepl("utf8", lc_messages))
    {
      new_lc_messages <- paste0(lc_messages, ".utf8")
      warning(
        "The LC_MESSAGES locale setting is not UTF-8, which may result in error messages being incorrectly displayed.\n",
        'Consider setting Sys.setlocale("LC_MESSAGES", "', 
          new_lc_messages,
          '").'
      )
    }    
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants