简体   繁体   English

将语言环境设置为系统默认UTF-8

[英]Set locale to system default UTF-8

When running R inside rApache, the locale is inherited from the Apache webserver, and therefore Sys.getlocale() is always equal to "C" . 在rApache中运行R时,语言环境继承自Apache Web服务器,因此Sys.getlocale()始终等于"C" I would like my web application to use UTF8 , so I use: 我希望我的Web应用程序使用UTF8 ,所以我使用:

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 ? 有没有办法使用Sys.setlocale将语言环境设置为系统默认的UTF-8 Ie something that would also work on Windows or a German Linux? 即在Windows或德语Linux上也可以使用的东西?

Answering my own question: On Ubuntu the default LANG is defined in /etc/default/locale : 回答我自己的问题:在Ubuntu上,默认的LANG/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: 所以在R中我们可以做类似的事情:

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. Apache在/etc/apache2/envvars中也有一行可以取消注释以启用它。

尝试这个:

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

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM