[R] Change of locale

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Jan 12 12:56:45 CET 2015


You don't want to change the locale, rather the message language.  That 
is not a locale, and e.g.

LANGUAGE=en

is the correct form.

See

http://cran.r-project.org/doc/manuals/r-patched/R-admin.html#Localization-of-messages

for what may be possible during a session: nothing is guaranteed. 
Documented behaviour is never a bug: in this case you are using OS 
services and it depends on the OS (and as the manual says, on precisely 
what versions of the support software the OS is using).

I see

 > a
Error: object 'a' not found
 > Sys.setlocale("LC_MESSAGES", "zh_CN.UTF-8")
[1] "zh_CN.UTF-8"
 > a
错误: 找不到对象'a'
 > Sys.setlocale("LC_MESSAGES", "en_GB.UTF-8")
[1] "en_GB.UTF-8"
 > a
Error: object 'a' not found

on Fedora 20.


On 12/01/2015 06:19, Chel Hee Lee wrote:
> Could anyone kindly guide me how to change locale?  Any advice would be
> greatly appreciated.   What I am trying to do is to see if messages are
> correctly in a given locale.  The environment variables are:
>
>  > sessionInfo()
> R version 3.1.2 (2014-10-31)
> Platform: x86_64-pc-linux-gnu (64-bit)
>
> locale:
>   [1] LC_CTYPE=en_CA.UTF-8       LC_NUMERIC=C
>   [3] LC_TIME=en_CA.UTF-8        LC_COLLATE=en_CA.UTF-8
>   [5] LC_MONETARY=en_CA.UTF-8    LC_MESSAGES=en_CA.UTF-8
>   [7] LC_PAPER=en_CA.UTF-8       LC_NAME=C
>   [9] LC_ADDRESS=C               LC_TELEPHONE=C
> [11] LC_MEASUREMENT=en_CA.UTF-8 LC_IDENTIFICATION=C
>
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods   base
>
> * Since I do not create any objects yet, the following message must be
> shown when typing the alphabet 'a'.
>
>  > a
> Error: object 'a' not found
>  > Sys.getenv()[c("LANG", "LANGUAGE")]
>              LANG         LANGUAGE
>     "en_CA.UTF-8" "en_CA:en_US:en"
>  > Sys.getlocale("LC_MESSAGES")
> [1] "en_CA.UTF-8"
>  >
>
> I am happy with this result.  Now, I'd like to see messages in Chineses.
>
>  > Sys.setlocale("LC_MESSAGES", "zh_CN.UTF-8")
> [1] "zh_CN.UTF-8"
>  > a
> Error: object 'a' not found
>
> The message is still shown in English; thus, I am changing the value of
> 'LANGUAGE'.
>
>  > Sys.setenv(LANGUAGE="zh_CN.UTF-8")
>  > a
> 错误: 找不到对象'a'
>  > Sys.getenv()[c("LANG", "LANGUAGE")]
>           LANG      LANGUAGE
> "en_CA.UTF-8"  "zh_CN.UTF-8"
>  > Sys.getlocale("LC_MESSAGES")
> [1] "zh_CN.UTF-8"
>  >
>
> Now, I'd like to use messages in English again.  At this time, I changed
> the value of LANGUAGE rather than LC_MESSAGES.
>
>  > Sys.setenv(LANGUAGE="en_CA.UTF-8")
>  > a
> 错误: 找不到对象'a'
>
> I still see this message in Chinese so that I change 'LC_MESSAGES'.
>
>  > Sys.setlocale("LC_MESSAGES", "en_CA.UTF-8")
> [1] "en_CA.UTF-8"
>  > a
> Error: object 'a' not found
>  > Sys.getenv()[c("LANG", "LANGUAGE")]
>           LANG      LANGUAGE
> "en_CA.UTF-8"  "en_CA.UTF-8"
>  > Sys.getlocale("LC_MESSAGES")
> [1] "en_CA.UTF-8"
>  >
>
> Thus, should I set the same value on both 'LANGUAGE' and 'LC_MESSAGES'
> in order to change messages in a given locale?   However, it is
> questionable.  Please see the following example (messages in Japanese):
>
>  > Sys.setenv(LANGUAGE="ja_JP.UTF-8")
>  > a
>   エラー:  オブジェクト 'a' がありません
>  > Sys.getenv()[c("LANG", "LANGUAGE")]
>           LANG      LANGUAGE
> "en_CA.UTF-8"  "ja_JP.UTF-8"
>  > Sys.getlocale("LC_MESSAGES")
> [1] "en_CA.UTF-8"
>  >
>  > Sys.setenv(LANGUAGE="en_CA.UTF-8")
>  > a
>   エラー:  オブジェクト 'a' がありません
>  > Sys.getenv()[c("LANG", "LANGUAGE")]
>           LANG      LANGUAGE
> "en_CA.UTF-8"  "en_CA.UTF-8"
>  > Sys.getlocale("LC_MESSAGES")
> [1] "en_CA.UTF-8"
>  > a
>   エラー:  オブジェクト 'a' がありません
>  >
>
> Is this a bug?  Any advice would be greatly appreciated.
>
> Chel Hee Lee
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.


-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Emeritus Professor of Applied Statistics, University of Oxford
1 South Parks Road, Oxford OX1 3TG, UK



More information about the R-help mailing list