[rkward-cvs] [rkward] packages/rkwarddev: hijacking noquote() from R base to use it in i18n()

m.eik michalke meik.michalke at uni-duesseldorf.de
Sun Feb 1 21:13:07 UTC 2015


Git commit 756b91884cd1a74eb51bc51eff70f2f689a2d589 by m.eik michalke.
Committed on 01/02/2015 at 21:11.
Pushed by meikm into branch 'master'.

hijacking noquote() from R base to use it in i18n()

M  +15   -3    packages/rkwarddev/R/i18n.R
M  +12   -0    packages/rkwarddev/R/rk-internal.R
M  +6    -2    packages/rkwarddev/man/i18n.Rd

http://commits.kde.org/rkward/756b91884cd1a74eb51bc51eff70f2f689a2d589

diff --git a/packages/rkwarddev/R/i18n.R b/packages/rkwarddev/R/i18n.R
index 0d48d14..f5dfb0d 100644
--- a/packages/rkwarddev/R/i18n.R
+++ b/packages/rkwarddev/R/i18n.R
@@ -22,7 +22,8 @@
 #' your JavaScript portions in R. Depending on the provided values for its arguments,
 #' will return one of \code{i18n()}, \code{i18nc()}, \code{i18np()}, or \code{i18ncp()}.
 #' 
-#' @param msgid Character string, the message to be translated (if applicable, its singular form).
+#' @param msgid Either a character string, the message to be translated (if applicable, its singular form),
+#'    or an object of class \code{\link[noquote]{noquote}}, which will be pasted as a \code{noquote()} function call.
 #' @param ... Either character string which will be pasted unquoted to be used in conjunctions with
 #'    placeholders in msgid, or XiMpLe.node objects of which the JavaScript variable name will be
 #'    used.
@@ -35,6 +36,8 @@
 #' @examples
 #' i18n("Select data")
 #' i18n("Comparing a single pair", "n_pairs", plural="Comparing %1 distinct pairs")
+#' 
+#' echo(i18n(noquote("A string I'll quote, later")))
 
 i18n <- function(msgid, ..., context=NULL, plural=NULL, newline=""){
   placeholders <- list(...)
@@ -50,10 +53,13 @@ i18n <- function(msgid, ..., context=NULL, plural=NULL, newline=""){
     pluralQuoted <- paste0(", ", qp(plural))
   } else {}
  
+  # deal with noquoted strings, by "misusing" the noquote() function from R base
+  msgid.nq <- rk.noquote(msgid)
+
   if(length(placeholders) > 0){
     # do some sanitiy checks here -- is there a placeholder in the strings for each dots value?
     # grep valid placeholders out of the messages
-    msgCleaned <- gsub("([^%[:digit:]]{2,})", " ", paste(msgid, plural))
+    msgCleaned <- gsub("([^%[:digit:]]{2,})", " ", paste(msgid.nq, plural))
     msgSplit <- unique(unlist(strsplit(msgCleaned, "[[:space:]]+")))
     msgPlHd <- msgSplit[grep("%[[:digit:]]", msgSplit)]
     # which placeholders are needed?
@@ -69,11 +75,17 @@ i18n <- function(msgid, ..., context=NULL, plural=NULL, newline=""){
     placeholderString <- paste0(", ", paste0(sapply(placeholders, function(ph){id(ph, js=TRUE)}), collapse=", "))
   } else {}
   
+  if(identical(msgid, msgid.nq)){
+    msgid.value <- qp(msgid)
+  } else {
+    # message is already quoted
+    msgid.value <- msgid.nq
+  }
   result <- new("rk.JS.i18n",
     value=paste0(
       JSfunction, "(",
       context,
-      qp(msgid),
+      msgid.value,
       pluralQuoted,
       placeholderString,
       ")",
diff --git a/packages/rkwarddev/R/rk-internal.R b/packages/rkwarddev/R/rk-internal.R
index e8499bd..2dfafd0 100644
--- a/packages/rkwarddev/R/rk-internal.R
+++ b/packages/rkwarddev/R/rk-internal.R
@@ -111,6 +111,18 @@ indent <- function(level, by="\t"){
 } ## end function indent()
 
 
+## function rk.noquote()
+# use noquote() in a slightly different manner:
+# if "text" is a noquote object, returns "noquote(\"text\")"
+rk.noquote <- function(text){
+  if(inherits(text, "noquote")){
+    return(paste0("noquote(", qp(paste0(text)), ")"))
+  } else {
+    return(text)
+  }
+} ## end function rk.noquote()
+
+
 ## function checkCreateFiles()
 # used by rk.plugin.skeleton()
 checkCreateFiles <- function(file.name, ow, action=NULL){
diff --git a/packages/rkwarddev/man/i18n.Rd b/packages/rkwarddev/man/i18n.Rd
index 4c68a1a..d5d2ab0 100644
--- a/packages/rkwarddev/man/i18n.Rd
+++ b/packages/rkwarddev/man/i18n.Rd
@@ -7,8 +7,10 @@
 i18n(msgid, ..., context = NULL, plural = NULL, newline = "")
 }
 \arguments{
-\item{msgid}{Character string, the message to be translated (if applicable,
-      its singular form).}
+\item{msgid}{Either a character string, the message to be translated (if applicable,
+      its singular form),
+or an object of class \code{\link[noquote]{noquote}},
+      which will be pasted as a \code{noquote()} function call.}
 
 \item{...}{Either character string which will be pasted unquoted to be used in conjunctions with
 placeholders in msgid,
@@ -34,5 +36,7 @@ will return one of \code{i18n()}, \code{i18nc()}, \code{i18np()}, or \code{i18nc
 \examples{
 i18n("Select data")
 i18n("Comparing a single pair", "n_pairs", plural="Comparing \%1 distinct pairs")
+
+echo(i18n(noquote("A string I'll quote, later")))
 }
 


------------------------------------------------------------------------------
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
rkward-cvs mailing list
rkward-cvs at lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rkward-cvs


More information about the rkward-tracker mailing list