问题描述
在 rApache 中运行 R 时,区域设置继承自 Apache 网络服务器,因此 Sys.getlocale()
始终等于 "C"
.我希望我的网络应用程序使用 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
将locale 设置为系统默认的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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!