5

To format dates in the full month name format : [Month Name] [YEAR] I use:

format(Sys.Date(),"%B %Y")

Now , I would do the same thing but in arabic:

## save locals
loc <- Sys.getlocale("LC_TIME")
Sys.setlocale("LC_TIME","Arabic")
format(Sys.Date(),"%B %Y")
## "????? 2015"          ## <----should have "جويلية 2015"
## restore locales
Sys.setlocale("LC_TIME",loc)

Arabic months are not replaced by "????". I don't think it is a print/Unicode problem since arabic is displayed correctly in the console:

"مرحبا "
[1] "مرحبا "

Internally strptime is called for formatting, from ?strptime:

Locale-specific conversions to and from character strings are used where appropriate and available

I think strptime don't have the right translation in arabic language. If this is true where can I contribute to fix this?

edit

AS pointed in comments This seems like it could be a system/OS problem specific. indeed , under Ubuntu machine , installing language-pack-ar and calling

Sys.setlocale("LC_TIME", "ar_AE.utf8"); 
format(Sys.Date(),"%B %Y") 
[1] "يوليو 2015" 

gives the right answer.

But under Windows , turning the locale to Arabic ( settings panel -> region..) and calling the same code of the question don't resolve the problem.

agstudy
  • 119,832
  • 17
  • 199
  • 261
  • Nitpick - in the last line of the code, shouldn't `op` be `loc` to reset the locale? – thelatemail Jul 06 '15 at 01:00
  • This seems like it could be a problem specific to your system/OS. I get a warning that "OS reports request to set locale to "Arabic" cannot be honored" when I try to run your `Sys.setlocale` command because that locale isn't available on my system. – Joshua Ulrich Jul 06 '15 at 01:17
  • @JoshuaUlrich I get a different result "Ìæííå 2015" but not arabic..at least here I have some UNICODE charcaters better than ???. – agstudy Jul 06 '15 at 01:29
  • 2
    I installed `language-pack-ar` on my Ubuntu machine and called `Sys.setlocale("LC_TIME", "ar_AE.utf8"); format(Sys.Date(),"%B %Y")` and received `[1] "يوليو 2015"` – Joshua Ulrich Jul 06 '15 at 02:21
  • 2
    To [a question](http://stackoverflow.com/questions/26603564/using-weekdays-with-any-locale-under-windows) you offered [an answer](http://stackoverflow.com/a/26604195/3710546). [Richie Cotton](http://stackoverflow.com/a/26609089/3710546), suggested to _"set LC_CTYPE before LC_TIME, or it won't work"_. Did you try this trick? –  Jul 06 '15 at 02:51
  • @Pascal thank you ! I tried it and it works fine! – agstudy Jul 06 '15 at 03:07

1 Answers1

3

This answer is closely inspired from this one. I just added a format argument to the function given by @RichieCotton

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

Testing it with different Arabic languages, since there some difference between countries:

get_today_windows("Arabic_Qatar")
## [1] "يوليو 2015"

get_today_windows("Arabic_Tunisia")
## [1] "جوييه 2015"

get_today_windows("Arabic_Saudi Arabia.1256")
## [1] "يوليو 2015"
Community
  • 1
  • 1
agstudy
  • 119,832
  • 17
  • 199
  • 261