-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement with_language() and local_language() (#180)
Uses LC_MESSAGES to reset cache on windows, and warns in two scenarios where setting the LANGUAGE will fail to have any effect.
- Loading branch information
Showing
6 changed files
with
99 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#' Language | ||
#' | ||
#' Temporarily change the language used for translations. | ||
#' | ||
#' @param lang A BCP47 language code like "en" (English), "fr" (French), | ||
#' "fr_CA" (French Canadian). Formally, this is a lower case two letter | ||
#' [ISO 639 country code](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes), | ||
#' optionally followed by a "_" and an upper case two letter | ||
#' [ISO 3166 region code](https://en.wikipedia.org/wiki/ISO_3166-2). | ||
#' @inheritParams with_collate | ||
#' @export | ||
#' @examples | ||
#' with_language("en", try(mean[[1]])) | ||
#' with_language("fr", try(mean[[1]])) | ||
#' with_language("es", try(mean[[1]])) | ||
with_language <- function(lang, code) { | ||
local_language(lang) | ||
code | ||
} | ||
|
||
#' @export | ||
#' @rdname with_language | ||
local_language <- function(lang, .local_envir = parent.frame()) { | ||
if (!has_nls()) { | ||
warning("Changing language has no effect when R installed without NLS") | ||
} | ||
|
||
# > Note: The variable LANGUAGE is ignored if the locale is set to ‘C’. | ||
# > In other words, you have to first enable localization, by setting LANG | ||
# > (or LC_ALL) to a value other than ‘C’, before you can use a language | ||
# > priority list through the LANGUAGE variable. | ||
# --- https://www.gnu.org/software/gettext/manual/html_node/The-LANGUAGE-variable.html | ||
if (identical(Sys.getenv("LANG"), "C")) { | ||
warning("Changing language has no effect when envvar LANG='C'") | ||
} | ||
|
||
local_envvar(LANGUAGE = lang, .local_envir = .local_envir) | ||
if (Sys.info()[["sysname"]] != "Windows") { | ||
# Reset cache to avoid gettext() retrieving cached value from a previous | ||
# language. I think this works because Sys.setlocale() calls setlocale() | ||
# which https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=931456 claims | ||
# resets the cache. So if there's some OS/setup that this technique fails | ||
# on, we might try bindtextdomain() instead or as well. | ||
local_locale(c(LC_MESSAGES = ""), .local_envir = .local_envir) | ||
} | ||
} | ||
|
||
has_nls <- function() capabilities("NLS")[[1]] |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
test_that("can temporary change language", { | ||
skip_if_not(has_nls()) | ||
|
||
expect_error(with_language("en", mean[[1]]), "not subsettable") | ||
expect_error(with_language("fr", mean[[1]]), "non indi\u00e7able") | ||
expect_error(with_language("es", mean[[1]]), "no es subconjunto") | ||
}) | ||
|
||
test_that("warns if LANG=C", { | ||
skip_if_not(has_nls()) | ||
|
||
local_envvar(LANG = "C") | ||
expect_warning(with_language("en", "x"), "has no effect") | ||
}) |