Skip to content

Commit

Permalink
Add support for translations (#554)
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley authored Jul 17, 2024
1 parent 7ae80ff commit 8816213
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 15 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Suggests:
VignetteBuilder:
knitr
Config/Needs/website: tidyverse/tidytemplate
Config/potools/style: explicit
Config/testthat/edition: 3
Encoding: UTF-8
LazyData: true
Expand Down
2 changes: 1 addition & 1 deletion R/detect.R
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ str_like <- function(string, pattern, ignore_case = TRUE) {
check_lengths(string, pattern)
check_character(pattern)
if (inherits(pattern, "stringr_pattern")) {
cli::cli_abort("{.arg pattern} must be a plain string, not a stringr modifier.")
cli::cli_abort(tr_("{.arg pattern} must be a plain string, not a stringr modifier."))
}
check_bool(ignore_case)

Expand Down
10 changes: 7 additions & 3 deletions R/interp.R
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,12 @@ interp_placeholders <- function(string, error_call = caller_env()) {

# If there are nested placeholders, each part will not contain a full
# placeholder in which case we report invalid string interpolation template.
if (any(!grepl("\\$(\\[.*?\\])?\\{.+\\}", parts)))
cli::cli_abort("Invalid template string for interpolation.", call = error_call)
if (any(!grepl("\\$(\\[.*?\\])?\\{.+\\}", parts))) {
cli::cli_abort(
tr_("Invalid template string for interpolation."),
call = error_call
)
}

# For each part, find the opening and closing braces.
opens <- lapply(strsplit(parts, ""), function(v) which(v == "{"))
Expand Down Expand Up @@ -169,7 +173,7 @@ extract_expressions <- function(matches, error_call = caller_env()) {
parse(text = text),
error = function(e) {
cli::cli_abort(
"Failed to parse input {.str {text}}",
tr_("Failed to parse input {.str {text}}"),
parent = e,
call = error_call
)
Expand Down
4 changes: 2 additions & 2 deletions R/match.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
str_match <- function(string, pattern) {
check_lengths(string, pattern)
if (type(pattern) != "regex") {
cli::cli_abort("{.arg pattern} must be a regular expression.")
cli::cli_abort(tr_("{.arg pattern} must be a regular expression."))
}

stri_match_first_regex(string,
Expand All @@ -65,7 +65,7 @@ str_match <- function(string, pattern) {
str_match_all <- function(string, pattern) {
check_lengths(string, pattern)
if (type(pattern) != "regex") {
cli::cli_abort("{.arg pattern} must be a regular expression.")
cli::cli_abort(tr_("{.arg pattern} must be a regular expression."))
}

stri_match_all_regex(string,
Expand Down
2 changes: 1 addition & 1 deletion R/modifiers.R
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ type.default <- function(x, error_call = caller_env()) {
}

cli::cli_abort(
"{.arg pattern} must be a string, not {.obj_type_friendly {x}}.",
tr_("{.arg pattern} must be a string, not {.obj_type_friendly {x}}."),
call = error_call
)
}
Expand Down
8 changes: 4 additions & 4 deletions R/replace.R
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ str_transform_all <- function(string, pattern, replacement, error_call = caller_
error = function(cnd) {
cli::cli_abort(
c(
"Failed to apply {.arg replacement} function.",
i = "It must accept a character vector of any length."
tr_("Failed to apply {.arg replacement} function."),
i = tr_("It must accept a character vector of any length.")
),
parent = cnd,
call = error_call
Expand All @@ -217,13 +217,13 @@ str_transform_all <- function(string, pattern, replacement, error_call = caller_

if (!is.character(new_flat)) {
cli::cli_abort(
"{.arg replacement} function must return a character vector, not {.obj_type_friendly {new_flat}}.",
tr_("{.arg replacement} function must return a character vector, not {.obj_type_friendly {new_flat}}."),
call = error_call
)
}
if (length(new_flat) != length(old_flat)) {
cli::cli_abort(
"{.arg replacement} function must return a vector the same length as the input ({length(old_flat)}), not length {length(new_flat)}.",
tr_("{.arg replacement} function must return a vector the same length as the input ({length(old_flat)}), not length {length(new_flat)}."),
call = error_call
)
}
Expand Down
2 changes: 1 addition & 1 deletion R/split.R
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ str_split_i <- function(string, pattern, i) {
}
map_chr(pieces, last)
} else {
cli::cli_abort("{.arg i} must not be 0.")
cli::cli_abort(tr_("{.arg i} must not be 0."))
}
}

Expand Down
2 changes: 1 addition & 1 deletion R/trunc.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ str_trunc <- function(string, width, side = c("right", "left", "center"),

if (width... < 0) {
cli::cli_abort(
"`width` ({width}) is shorter than `ellipsis` ({str_length(ellipsis)})."
tr_("`width` ({width}) is shorter than `ellipsis` ({str_length(ellipsis)}).")
)
}

Expand Down
8 changes: 6 additions & 2 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ check_lengths <- function(string, pattern, replacement = NULL, error_call = call
}

no_boundary <- function(call = caller_env()) {
cli::cli_abort("{.arg pattern} can't be a boundary.", call = call)
cli::cli_abort(tr_("{.arg pattern} can't be a boundary."), call = call)
}
no_empty <- function(call = caller_env()) {
cli::cli_abort("{.arg pattern} can't be the empty string ({.code \"\"}).", call = call)
cli::cli_abort(tr_("{.arg pattern} can't be the empty string ({.code \"\"})."), call = call)
}

tr_ <- function(...) {
enc2utf8(gettext(paste0(...), domain = "R-stringr"))
}
67 changes: 67 additions & 0 deletions po/R-stringr.pot
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
msgid ""
msgstr ""
"Project-Id-Version: stringr 1.5.1.9000\n"
"POT-Creation-Date: 2024-07-17 11:07-0500\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

#: detect.R:141
msgid "{.arg pattern} must be a plain string, not a stringr modifier."
msgstr ""

#: interp.R:105
msgid "Invalid template string for interpolation."
msgstr ""

#: interp.R:176
msgid "Failed to parse input {.str {text}}"
msgstr ""

#: match.R:54 match.R:68
msgid "{.arg pattern} must be a regular expression."
msgstr ""

#: modifiers.R:216
msgid "{.arg pattern} must be a string, not {.obj_type_friendly {x}}."
msgstr ""

#: replace.R:208
msgid "Failed to apply {.arg replacement} function."
msgstr ""

#: replace.R:209
msgid "It must accept a character vector of any length."
msgstr ""

#: replace.R:220
msgid ""
"{.arg replacement} function must return a character vector, not {."
"obj_type_friendly {new_flat}}."
msgstr ""

#: replace.R:226
msgid ""
"{.arg replacement} function must return a vector the same length as the "
"input ({length(old_flat)}), not length {length(new_flat)}."
msgstr ""

#: split.R:122
msgid "{.arg i} must not be 0."
msgstr ""

#: trunc.R:32
msgid "`width` ({width}) is shorter than `ellipsis` ({str_length(ellipsis)})."
msgstr ""

#: utils.R:23
msgid "{.arg pattern} can't be a boundary."
msgstr ""

#: utils.R:26
msgid "{.arg pattern} can't be the empty string ({.code \"\"})."
msgstr ""

0 comments on commit 8816213

Please sign in to comment.