File: locale.R

package info (click to toggle)
r-cran-rsconnect 1.3.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,044 kB
  • sloc: python: 185; sh: 13; makefile: 5
file content (27 lines) | stat: -rw-r--r-- 723 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
detectLocale <- function() {
  if (!isWindows()) {
    locales <- strsplit(Sys.getlocale("LC_CTYPE"), ".", fixed = TRUE)[[1]]
    locales[[1]]
  } else {
    tryCatch(windowsLocale(), error = function(err) "en_US")
  }
}

# Possible values
# listed at https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-lcid
windowsLocale <- function() {
  key <- utils::readRegistry("Control Panel\\International", hive = "HCU")
  normalizeLocale(key$LocaleName)
}

# Remove script tag, if present
normalizeLocale <- function(x) {
  pieces <- strsplit(x, "-")[[1]]
  all_upper <- pieces[pieces == toupper(pieces)]

  if (length(all_upper) == 0) {
    pieces[[1]]
  } else {
    paste0(pieces[[1]], "_", all_upper[[1]])
  }
}