Skip to content

Instantly share code, notes, and snippets.

@jennybc
Last active February 14, 2021 00:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jennybc/01dd730312fdc240ab15b94dd9ea24d3 to your computer and use it in GitHub Desktop.
Save jennybc/01dd730312fdc240ab15b94dd9ea24d3 to your computer and use it in GitHub Desktop.
Localization demo

I execute:

reprex({
  # inheriting everything from local system
  # for Jenny, this means English
  "a" / 2
  1:3 == c(1:2, 5)
  format(as.Date(c("2019-01-01", "2019-02-01")), "%d %B %Y")
})

which yields:

# inheriting everything from local system
# for Jenny, this means English
"a" / 2
#> Error in "a"/2: non-numeric argument to binary operator
1:3 == c(1:2, 5)
#> [1]  TRUE  TRUE FALSE
format(as.Date(c("2019-01-01", "2019-02-01")), "%d %B %Y")
#> [1] "01 January 2019"  "01 February 2019"

I execute:

reprex_locale({
  # the LANGUAGE env var affects messages (and that is all it affects here)
  # in reprex_locale(), that is controlled by the `language` argument
  "a" / 2
  1:3 == c(1:2, 5)
  format(as.Date(c("2019-01-01", "2019-02-01")), "%d %B %Y")
}, language = "fr")

which yields:

# the LANGUAGE env var affects messages (and that is all it affects here)
# in reprex_locale(), that is controlled by the `language` argument
"a" / 2
#> Error in "a"/2: argument non numérique pour un opérateur binaire
1:3 == c(1:2, 5)
#> [1]  TRUE  TRUE FALSE
format(as.Date(c("2019-01-01", "2019-02-01")), "%d %B %Y")
#> [1] "01 January 2019"  "01 February 2019"

I execute:

reprex_locale({
  # the LC_TIME component of the locale affects, e.g., the character strings
  # associated with the months
  # in reprex_locale(), locale components can be set with the `locale` argument
  "a" / 2
  1:3 == c(1:2, 5)
  format(as.Date(c("2019-01-01", "2019-02-01")), "%d %B %Y")
}, language = "fr", locale = c(LC_TIME = "fr_FR"))

which yields:

# the LC_TIME component of the locale affects, e.g., the character strings
# associated with the months
# in reprex_locale(), locale components can be set with the `locale` argument
"a" / 2
#> Error in "a"/2: argument non numérique pour un opérateur binaire
1:3 == c(1:2, 5)
#> [1]  TRUE  TRUE FALSE
format(as.Date(c("2019-01-01", "2019-02-01")), "%d %B %Y")
#> [1] "01 janvier 2019" "01 février 2019"

Created on 2021-02-13 by the reprex package (v1.0.0.9000)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment