Set locale to system default UTF-8

两盒软妹~` 提交于 2019-12-17 06:09:23

问题


When running R inside rApache, the locale is inherited from the Apache webserver, and therefore Sys.getlocale() is always equal to "C". I would like my web application to use UTF8, so I use:

Sys.setlocale("LC_ALL", 'en_US.UTF-8')

However this doesn't work on machines that do not have this locale available:

1: Setting LC_CTYPE failed, using "C" 
2: Setting LC_COLLATE failed, using "C" 
3: Setting LC_TIME failed, using "C" 
4: Setting LC_MESSAGES failed, using "C" 
5: Setting LC_MONETARY failed, using “C”

Is there any way to use Sys.setlocale to set the locale to the system default UTF-8? I.e. something that would also work on Windows or a German Linux?


回答1:


Answering my own question: On Ubuntu the default LANG is defined in /etc/default/locale:

jeroen@dev:~⟫ cat /etc/default/locale
# Created by cloud-init v. 0.7.7 on Wed, 29 Jun 2016 11:02:51 +0000
LANG="en_US.UTF-8"

So in R we could do something like:

readRenviron("/etc/default/locale")
LANG <- Sys.getenv("LANG")
if(nchar(LANG))
   Sys.setlocale("LC_ALL", LANG)

Apache also has a line in /etc/apache2/envvars that can be uncommented to enable this.




回答2:


Try this:

Sys.setlocale(category = "LC_ALL", locale = "English_United States.1252")



回答3:


I guess you need to make a check for the OS. The locale names differ by OS, see the examples at the help file.

?Sys.getlocale()

Examples

Sys.getlocale()
Sys.getlocale("LC_TIME")
## Not run: 
Sys.setlocale("LC_TIME", "de")     # Solaris: details are OS-dependent
Sys.setlocale("LC_TIME", "de_DE.utf8")   # Modern Linux etc.
Sys.setlocale("LC_TIME", "de_DE.UTF-8")  # ditto
Sys.setlocale("LC_TIME", "de_DE")  # OS X, in UTF-8
Sys.setlocale("LC_TIME", "German") # Windows

## End(Not run)
Sys.getlocale("LC_PAPER")          # may or may not be set

## Not run: 
Sys.setlocale("LC_COLLATE", "C")   # turn off locale-specific sorting,
                                   # usually, but not on all platforms
## End(Not run)


来源:https://stackoverflow.com/questions/20577764/set-locale-to-system-default-utf-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!