Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return localized weekdays for wday() #401

Closed
alberthkcheng opened this issue Apr 8, 2016 · 11 comments
Closed

Return localized weekdays for wday() #401

alberthkcheng opened this issue Apr 8, 2016 · 11 comments

Comments

@alberthkcheng
Copy link
Contributor

wday() always returns English names of weekdays. I made the following changes using date_names_lang in readr, to overcome this. Is there solution exists without using readr?

Also, the same applies to month.

wday.numeric <- function (x, label = FALSE, abbr = TRUE, language="en") 
{
  if (!label) 
    return(x)
  if (abbr) {
    labels <- date_names_lang(language)$day_ab
  }
  else {
    labels <- date_names_lang(language)$day
  }
  ordered(x, levels = 1:7, labels = labels)
}

wday(ymd(160408),label=TRUE,language="ko")
# [1] 금
# Levels: 일 < 월 < 화 < 수 < 목 < 금 < 토
@vspinu
Copy link
Member

vspinu commented Apr 8, 2016

Yes. It's relatively easy to implement. This applies to month as well.

@vspinu vspinu modified the milestones: v1.6.0, v1.5.6 Apr 8, 2016
@vspinu vspinu modified the milestone: v1.6.0 Aug 17, 2016
@hadley
Copy link
Member

hadley commented Aug 18, 2016

Where would you get the localisation databases from? I think it's better to use non-operating system sources so code works the same way across platforms.

@vspinu
Copy link
Member

vspinu commented Aug 18, 2016

You don't need a data base. I had in mind something along the following:

> enc2utf8(unique(format(lubridate:::.date_template, format = "%a@%A")))
[1] "Thu@Thursday"  "Mon@Monday"    "Tue@Tuesday"   "Sun@Sunday"    "Fri@Friday"    "Wed@Wednesday" "Sat@Saturday" 

@hadley
Copy link
Member

hadley commented Aug 18, 2016

Oh, so the user would supply the localisation date names, not a language name?

@vspinu
Copy link
Member

vspinu commented Aug 18, 2016

Yes. By default wday would return names in current locale using the trick above. The new argument locale will accept a valid locale name on user's OS, or a shortcut for 100 common languages such that wday(2, locale = "en") would also work.

@vspinu
Copy link
Member

vspinu commented Aug 18, 2016

100 is a bit too much but a small data set of common 10-20 languages should be doable.

@hadley
Copy link
Member

hadley commented Aug 18, 2016

readr has 185 ;)

@vspinu vspinu closed this as completed in 6c88890 Nov 20, 2016
alberthkcheng added a commit to alberthkcheng/lubridate that referenced this issue Jan 16, 2017
alberthkcheng added a commit to alberthkcheng/lubridate that referenced this issue Jan 16, 2017
@alberthkcheng
Copy link
Contributor Author

alberthkcheng commented Jan 16, 2017

Thank you for the fix :)

The one in Korean is good now. There is still one issue:
For traditional chinese, the format are different from the one in readr (which is more commonly used in HK, etc)

# lubridate
> enc2utf8(unique(format(.date_template, format = format)))
 [1] "四@周四@ 1@1月@上午@"  "一@周一@ 2@2月@下午@"  "二@周二@ 3@3月@上午@"  "日@周日@ 4@4月@下午@" 
 [5] "五@周五@ 5@5月@上午@"  "二@周二@ 6@6月@下午@"  "五@周五@ 7@7月@上午@"  "三@周三@ 8@8月@下午@" 
 [9] "二@周二@ 9@9月@上午@"  "五@周五@10@10月@下午@" "二@周二@11@11月@上午@" "六@周六@12@12月@下午@"
# readr
> locale("zh")
<locale>
Numbers:  123,456.78
Formats:  %Y%.%m%.%d / %H:%M
Timezone: UTC
Encoding: UTF-8
<date_names>
Days:   星期日 (周日), 星期一 (周一), 星期二 (周二), 星期三 (周三), 星期四 (周四), 星期五 (周五), 星期六
        (周六)
Months: 一月 (1月), 二月 (2月), 三月 (3月), 四月 (4月), 五月 (5月), 六月 (6月), 七月 (7月), 八月 (8月), 九月
        (9月), 十月 (10月), 十一月 (11月), 十二月 (12月)
AM/PM:  上午/下午

any idea @hadley @vspinu ?
I am willing to help to fix it if needed ;)

@alberthkcheng
Copy link
Contributor Author

Also, AM/PM translated correctly in traditional chinese but not in Japanese and Korean.
The one in readr locale() are more completed for all the attributes.

@vspinu
Copy link
Member

vspinu commented Jan 16, 2017

Also, AM/PM translated correctly in traditional chinese but not in Japanese and Korean. The one in readr locale() are more completed for all the attributes.

I guess it's because readr is using its own data base for this. lubridate relies on system's libraries and compiles attributes like PM/AM on the fly. If thse are missing that probably means they are missing at system level.

@alberthkcheng
Copy link
Contributor Author

I see. Thanks. Then I think let's leave it to #396 ... :)

vspinu added a commit that referenced this issue Jan 18, 2017
[Fix #401 ext] Return localized labels in month
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants