将语言环境设置为系统默认的UTF-8 [英] Set locale to system default UTF-8
问题描述
在rApache中运行R时,语言环境是从Apache Web服务器继承的,因此Sys.getlocale()
始终等于"C"
.我希望我的Web应用程序使用UTF8
,所以我使用:
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"
是否可以使用Sys.setlocale
将语言环境设置为系统默认的UTF-8
? IE.在Windows或德语Linux上也可以使用的东西?
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?
推荐答案
回答我自己的问题:在Ubuntu上,默认的LANG
在/etc/default/locale
中定义:
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"
因此,在R中,我们可以执行以下操作:
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在/etc/apache2/envvars
中也有一行,可以取消注释以启用此功能.
Apache also has a line in /etc/apache2/envvars
that can be uncommented to enable this.
这篇关于将语言环境设置为系统默认的UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!