在Windows下使用任何语言环境的工作日 [英] Using weekdays with any locale under Windows
问题描述
我想获得一周中的某一天,让它在任何地区都能持续运作。在包含拉丁字母的语言环境中,一切都很好。
Sys.getlocale()
## [1]LC_COLLATE = English_United Kingdom.1252; LC_CTYPE = English_United Kingdom.1252; LC_MONETARY = English_United Kingdom.1252; LC_NUMERIC = C; LC_TIME = English_United Kingdom.1252
weekdays(Sys.Date())
## [ 1]星期二
我有两个与其他语言环境相关的问题。
如果我设置
Sys.setlocale(LC_ALL,Arabic_Qatar)
## [1]LC_COLLATE = Arabic_Qatar.1256; LC_CTYPE = Arabic_Qatar.1256; LC_MONETARY = Arabic_Qatar.1256; LC_NUMERIC = C; LC_TIME = Arabic_Qatar.1256
那么我有时(正确)得到
工作日())
## [1]الثلاثاء
工作日(Sys.Date())
## [1]
取决于我的设置问题是,我不知道是什么导致了差异。
我认为这可能与
getOption(encoding)
有关,但我已经尝试显式设置options =native.enc)
和选项(encoding =UTF-8)
,没有什么区别。
我试过几个最新版本的R,并且所有的问题都是一致的。
我应该设置什么来确保工作日总是正确显示?
了解
平日(Sys.Date())
格式(as.POSIXlt(Sys.Date()),%A)
,它调用内部format.POSIXlt
方法。
其次,似乎过分的改变所有的语言环境。我以为我应该只能设置时间选项。但是,如果我设置语言环境的各个组件,
weekdays
会返回一个问号字符串。for(c(LC_TIME,LC_CTYPE,LC_COLLATE,LC_MONETARY)中的类别)
{
Sys.setlocale(category,Arabic_Qatar)
print(Sys.getlocale())
print(weekdays(Sys.Date()))
}
## [1]LC_COLLATE = English_United Kingdom.1252; LC_CTYPE = English_United Kingdom.1252; LC_MONETARY = English_United Kingdom.1252; LC_NUMERIC = C; LC_TIME = Arabic_Qatar.1256
## [1]
## [1]LC_COLLATE = English_United Kingdom.1252; LC_CTYPE = Arabic_Qatar.1256; LC_MONETARY = English_United Kingdom.1252; LC_NUMERIC = C; LC_TIME = Arabic_Qatar.1256
## [1]
## [1]LC_COLLATE = Arabic_Qatar.1256; LC_CTYPE = Arabic_Qatar.1256; LC_MONETARY = English_United Kingdom.1252; LC_NUMERIC = C; LC_TIME = Arabic_Qatar.1256
## [1]
## [1]LC_COLLATE = Arabic_Qatar.1256; LC_CTYPE = Arabic_Qatar.1256; LC_MONETARY = Arabic_Qatar.1256; LC_NUMERIC = C; LC_TIME = Arabic_Qatar.1256
## [1]
区域设置的哪些部分会影响工作日的打印方式? p>
更新:问题似乎与Windows相关。当我在具有locale
ar_QA.UTF8
的Linux盒子上运行代码时,正确显示工作日。
进一步更新:在他的答案中提到的agstudy,在Windows下设置区域设置是奇怪的,因为你不能只使用像en-GB的ISO代码。对于Windows 7 / Vista / Server 2003 / XP,您可以使用 setlocale语言字符串或国家语言支持值。对于Qatari阿拉伯语,没有setlocale语言字符串,因此我们必须使用NLS值。我们有几个选择:
Sys.setlocale(LC_TIME,ARQ)#语言缩写名称
对应于语言/国家对Arabic(Qatar)的Sys.setlocale(LC_TIME,Arabic_Qatar)#
显式包含ANSI代码页的Sys.setlocale(LC_TIME,Arabic_Qatar.1256)#
Sys.setlocale(LC_TIME,Arabic)#有时也是一种可能性,但默认为沙特阿拉伯语
所以问题不是R不能支持Windows下的阿拉伯语语言环境(虽然我不完全相信
Sys.setlocale
)。
绝望的最后一次尝试:试图通过使用Windows Management Instrumentation Command更改操作系统区域设置不工作,因为R似乎没有识别更改。
系统(wmic os set locale = MS_4001 )
##更新'\\PC402729\ROOT\CIMV2:Win32_OperatingSystem = @'
##的属性更新成功。
system(wmic os get locale)#和之前一样
解决方案命名语言环境的系统是特定于操作系统的。我们建议您阅读地方语言
在Windows下:
支持的语言列表是列出的 MSDN语言字符串。令人惊讶的是,那里没有阿拉伯语。 语言字符串列包含在R 中设置区域设置的合法输入,甚至在列表中 contry / regions字符串没有国家阿拉伯语。
当然,您可以更改区域设置的全局设置(面板设置 - >区域 - > ..),但这将改变它的全局,正确的输出没有编码问题。
在linux下(ubuntu在我的情况下):
默认情况下支持,但可以使用
locale
轻松设置它。locale -a ##列出所有已经支持的语言
sudo locale-gen ar_QA.UTF-8 ##安装它以防不存在
现在:
Sys.setlocale('LC_TIME','ar_QA .UTF-8')
[1]ar_QA.UTF-8
>格式(Sys.Date(),'%A')
[1]الثلاثاء
还要注意,在R控制台下,打印不像R studio那么漂亮,因为它是从从左到右而不是从从右到左写的。
I'm trying to get the day of the week, and have it work consistently in any locale. In locales with Latin alphabets, everything is fine.
Sys.getlocale() ## [1] "LC_COLLATE=English_United Kingdom.1252;LC_CTYPE=English_United Kingdom.1252;LC_MONETARY=English_United Kingdom.1252;LC_NUMERIC=C;LC_TIME=English_United Kingdom.1252" weekdays(Sys.Date()) ## [1] "Tuesday"
I have two related problems with other locales.
If I set
Sys.setlocale("LC_ALL", "Arabic_Qatar") ## [1] "LC_COLLATE=Arabic_Qatar.1256;LC_CTYPE=Arabic_Qatar.1256;LC_MONETARY=Arabic_Qatar.1256;LC_NUMERIC=C;LC_TIME=Arabic_Qatar.1256"
then I sometimes (correctly) get
weekdays(Sys.Date()) ## [1] "الثلاثاء
and sometimes get
weekdays(Sys.Date()) ## [1] "ÇáËáÇËÇÁ"
depending upon my setup. The problem is, I can't figure out what is causing the difference.
I thought it might be something to do with
getOption("encoding")
, but I've tried explicitly settingoptions(encoding = "native.enc")
andoptions(encoding = "UTF-8")
and it makes no difference.I've tried several recent versions of R, and the problem is consistent across all of them.
At the moment, the string displays correctly in R GUI, but incorrectly when I use an IDE (Architect and RStudio tested).
What should I set to ensure that weekdays always displays correctly?
It may be helpful to know that
weekdays(Sys.Date())
is equivalent toformat(as.POSIXlt(Sys.Date()), "%A")
, which calls an internalformat.POSIXlt
method.Secondly, it seems overkill to change all of the locale. I thought I should just be able to set the time options. However, if I set individual components of the locale,
weekdays
returns a string of question marks.for(category in c("LC_TIME", "LC_CTYPE", "LC_COLLATE", "LC_MONETARY")) { Sys.setlocale(category, "Arabic_Qatar") print(Sys.getlocale()) print(weekdays(Sys.Date())) } ## [1] "LC_COLLATE=English_United Kingdom.1252;LC_CTYPE=English_United Kingdom.1252;LC_MONETARY=English_United Kingdom.1252;LC_NUMERIC=C;LC_TIME=Arabic_Qatar.1256" ## [1] "????????" ## [1] "LC_COLLATE=English_United Kingdom.1252;LC_CTYPE=Arabic_Qatar.1256;LC_MONETARY=English_United Kingdom.1252;LC_NUMERIC=C;LC_TIME=Arabic_Qatar.1256" ## [1] "????????" ## [1] "LC_COLLATE=Arabic_Qatar.1256;LC_CTYPE=Arabic_Qatar.1256;LC_MONETARY=English_United Kingdom.1252;LC_NUMERIC=C;LC_TIME=Arabic_Qatar.1256" ## [1] "????????" ## [1] "LC_COLLATE=Arabic_Qatar.1256;LC_CTYPE=Arabic_Qatar.1256;LC_MONETARY=Arabic_Qatar.1256;LC_NUMERIC=C;LC_TIME=Arabic_Qatar.1256" ## [1] "????????"
What parts of the locale affect how the weekdays are printed?
Update: The problem seems to be Windows-related. When I run the code on a Linux box with locale
"ar_QA.UTF8"
, the weekdays are correctly displayed.
Further update: As agstudy mentioned in his answer, setting locales under Windows is odd, since you can't just use ISO codes like "en-GB". For Windows 7/Vista/Server 2003/XP you can set a locale using setlocale language strings or National Language Support values. For Qatari Arabic, there is no setlocale language string, so we must use an NLS value. We have several choices:
Sys.setlocale("LC_TIME", "ARQ") # the language abbreviation name Sys.setlocale("LC_TIME", "Arabic_Qatar") # corresponding to the language/country pair "Arabic (Qatar)" Sys.setlocale("LC_TIME", "Arabic_Qatar.1256") # explicitly including the ANSI codepage Sys.setlocale("LC_TIME", "Arabic") # would sometimes be a possibility too, but it defaults to Saudi Arabic
So the problem isn't that R cannot support Arabic locales under Windows (though I'm not entirely convinced of the robustness of
Sys.setlocale
).
Desperate last ditch attempt: Trying to magically fix things by using Windows Management Instrumentation Command to change the OS locale doesn't work, since R doesn't appear to recognise the changes.
system("wmic os set locale=MS_4001") ## Updating property(s) of '\\PC402729\ROOT\CIMV2:Win32_OperatingSystem=@' ## Property(s) update successful. system("wmic os get locale") # same as before
解决方案The system of naming locales is OS-specific. I recommend you to read the locales from R Installation and Administration manual for a complete explanation.
under windows :
The list of supported language is listed MSDN Language Strings. And surprisingly there is not Arabic language there. The "Language string" column contains the legal input for setting locale in R and even in the list contry /regions strings there no country spoken arabic there.
Of course you can change your locale global settings( panel setting --> region --> ..) but this will change it globally and it is not sure to get the right output without encoding problem.
under linux(ubuntu in my case):
Arabic is generally not supported by default, but is easy to set it using
locale
.locale -a ## to list all already supported language sudo locale-gen ar_QA.UTF-8 ## install it in case does not exist
under RStudio now :
Sys.setlocale('LC_TIME','ar_QA.UTF-8') [1] "ar_QA.UTF-8" > format(Sys.Date(),'%A') [1] "الثلاثاء
Note also that under R console the printing is not as pretty as in R studio because it is written from left to right not from right to left.
这篇关于在Windows下使用任何语言环境的工作日的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!