在阿拉伯语区域设置中将日期格式化为完整的月份名称

ags*_*udy 5 settings r date arabic

要以完整月份名称格式格式化日期:[月份名称] [年份] 我使用:

\n\n
format(Sys.Date(),"%B %Y")\n
Run Code Online (Sandbox Code Playgroud)\n\n

现在,我会做同样的事情,但用阿拉伯语:

\n\n
## save locals\nloc <- Sys.getlocale("LC_TIME")\nSys.setlocale("LC_TIME","Arabic")\nformat(Sys.Date(),"%B %Y")\n## "????? 2015"          ## <----should have "\xd8\xac\xd9\x88\xd9\x8a\xd9\x84\xd9\x8a\xd8\xa9 2015"\n## restore locales\nSys.setlocale("LC_TIME",loc)\n
Run Code Online (Sandbox Code Playgroud)\n\n

阿拉伯月份不被“???”取代。我不认为这是打印/Unicode 问题,因为阿拉伯语在控制台中正确显示:

\n\n
"\xd9\x85\xd8\xb1\xd8\xad\xd8\xa8\xd8\xa7 "\n[1] "\xd9\x85\xd8\xb1\xd8\xad\xd8\xa8\xd8\xa7 "\n
Run Code Online (Sandbox Code Playgroud)\n\n

内部strptime 调用进行格式化,来自?strptime

\n\n
\n

在适当可用的情况下使用特定于区域设置的字符串转换

\n
\n\n

我认为strptime阿拉伯语没有正确的翻译。如果这是真的,我可以在哪里帮助解决这个问题?

\n\n

编辑

\n\n

AS 在评论中指出这似乎可能是特定于系统/操作系统的问题。\nindeed,在Ubuntu下 机器下,安装 language-pack-ar 并调用

\n\n
Sys.setlocale("LC_TIME", "ar_AE.utf8"); \nformat(Sys.Date(),"%B %Y") \n[1] "\xd9\x8a\xd9\x88\xd9\x84\xd9\x8a\xd9\x88 2015" \n
Run Code Online (Sandbox Code Playgroud)\n\n

给出了正确的答案。

\n\n

但在Windows下下,将语言环境设置为阿拉伯语(设置面板 -> 区域..)并调用问题的相同代码并不能解决问题。

\n

ags*_*udy 3

这个答案与这个答案密切相关。我刚刚向 @RichieCotton 给出的函数添加了一个格式参数

\n\n
get_today_windows <- function(locale = NULL, fmt = "%B %Y")\n{\n  if(!is.null(locale))\n  {\n    lc_ctype <- Sys.getlocale("LC_CTYPE")\n    lc_time <- Sys.getlocale("LC_TIME")\n    on.exit(Sys.setlocale("LC_CTYPE", lc_ctype))\n    on.exit(Sys.setlocale("LC_TIME", lc_time), add = TRUE)\n    Sys.setlocale("LC_CTYPE", locale)\n    Sys.setlocale("LC_TIME", locale)\n  }\n  ## here I am changing \n  today <- format(Sys.Date(), format = fmt )\n  current_codepage <- as.character(l10n_info()$codepage)\n  iconv(today, from = current_codepage, to = "utf8")\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

使用不同的阿拉伯语言进行测试,因为国家之间存在一些差异:

\n\n
get_today_windows("Arabic_Qatar")\n## [1] "\xd9\x8a\xd9\x88\xd9\x84\xd9\x8a\xd9\x88 2015"\n\nget_today_windows("Arabic_Tunisia")\n## [1] "\xd8\xac\xd9\x88\xd9\x8a\xd9\x8a\xd9\x87 2015"\n\nget_today_windows("Arabic_Saudi Arabia.1256")\n## [1] "\xd9\x8a\xd9\x88\xd9\x84\xd9\x8a\xd9\x88 2015"\n
Run Code Online (Sandbox Code Playgroud)\n