[rkward-cvs] [rkward] rkward: Allow switching frontend language (esp. of plugins) at runtime.

Thomas Friedrichsmeier thomas.friedrichsmeier at ruhr-uni-bochum.de
Thu Dec 11 19:41:55 UTC 2014


Git commit c32b2ca33a4d9638e3f101126c3266f017d13a64 by Thomas Friedrichsmeier.
Committed on 11/12/2014 at 19:40.
Pushed by tfry into branch 'master'.

Allow switching frontend language (esp. of plugins) at runtime.
In particular for automated testing, but made public, as there might be corner-cases, where this is useful outside of testing.

M  +20   -0    rkward/misc/rkmessagecatalog.cpp
M  +2    -0    rkward/misc/rkmessagecatalog.h
M  +3    -0    rkward/rbackend/rinterface.cpp
M  +1    -0    rkward/rbackend/rpackages/rkward/NAMESPACE
M  +19   -0    rkward/rbackend/rpackages/rkward/R/rk.utility-functions.R
A  +22   -0    rkward/rbackend/rpackages/rkward/man/rk.switch.frontend.language.Rd
M  +1    -0    rkward/rbackend/rpackages/rkwardtests/R/rkwardtests-internal.R

http://commits.kde.org/rkward/c32b2ca33a4d9638e3f101126c3266f017d13a64

diff --git a/rkward/misc/rkmessagecatalog.cpp b/rkward/misc/rkmessagecatalog.cpp
index 253c554..6649dbb 100644
--- a/rkward/misc/rkmessagecatalog.cpp
+++ b/rkward/misc/rkmessagecatalog.cpp
@@ -19,6 +19,8 @@
 
 #include <libintl.h>
 #include <QFile>
+#include <kglobal.h>
+#include <klocale.h>
 
 #include "../debug.h"
 
@@ -104,3 +106,21 @@ RKMessageCatalog* RKMessageCatalog::nullCatalog () {
 	if (!null_catalog) null_catalog = getCatalog  ("rkward_dummy", QString ());
 	return null_catalog;
 }
+
+#ifdef Q_WS_WIN
+	extern "C" int __declspec(dllimport) _nl_msg_cat_cntr;
+#endif
+
+// static
+void RKMessageCatalog::switchLanguage (const QString &new_language_code) {
+	RK_TRACE (MISC);
+
+	qputenv ("LANGUAGE", new_language_code.toAscii ().data ());
+	KLocale *l = new KLocale ("rkward", new_language_code);
+	KGlobal::setLocale (l);
+	// magic to make gettext discard cache
+#ifndef _MSC_VER
+	extern int _nl_msg_cat_cntr;
+#endif
+	++_nl_msg_cat_cntr;
+}
diff --git a/rkward/misc/rkmessagecatalog.h b/rkward/misc/rkmessagecatalog.h
index f132be8..c530c81 100644
--- a/rkward/misc/rkmessagecatalog.h
+++ b/rkward/misc/rkmessagecatalog.h
@@ -39,6 +39,8 @@ public:
 	static RKMessageCatalog *getCatalog (const QString &name, const QString &pathhint);
 /** Returns a dummy null-catalog */
 	static RKMessageCatalog *nullCatalog ();
+/** Switch language to use for any coming translations */
+	static void switchLanguage (const QString &new_language_code);
 private:
 	RKMessageCatalog (const QString &name, const QString &path);
 	~RKMessageCatalog ();
diff --git a/rkward/rbackend/rinterface.cpp b/rkward/rbackend/rinterface.cpp
index 2a19abc..009b57a 100644
--- a/rkward/rbackend/rinterface.cpp
+++ b/rkward/rbackend/rinterface.cpp
@@ -43,6 +43,7 @@
 #include "../windows/rkhtmlwindow.h"
 #include "../plugin/rkcomponentmap.h"
 #include "../misc/rkcommonfunctions.h"
+#include "../misc/rkmessagecatalog.h"
 #include "rksessionvars.h"
 
 #include "../windows/rkwindowcatcher.h"
@@ -667,6 +668,8 @@ QStringList RInterface::processPlainGenericRequest (const QStringList &calllist)
 		RKPrintAgent::printPostscript (calllist.value (1), true);
 	} else if (call == "endBrowserContext") {
 		RKDebugHandler::instance ()->endDebug ();
+	} else if (call == "switchLanguage") {
+		RKMessageCatalog::switchLanguage (calllist.value (1));
 	} else {
 		return (QStringList ("Error: unrecognized request '" + call + "'."));
 	}
diff --git a/rkward/rbackend/rpackages/rkward/NAMESPACE b/rkward/rbackend/rpackages/rkward/NAMESPACE
index 332a8dc..7ce14d3 100644
--- a/rkward/rbackend/rpackages/rkward/NAMESPACE
+++ b/rkward/rbackend/rpackages/rkward/NAMESPACE
@@ -108,6 +108,7 @@ export(rk.show.html)
 export(rk.show.message)
 export(rk.show.plot.info)
 export(rk.show.question)
+export(rk.switch.frontend.language)
 export(rk.sync)
 export(rk.sync.global)
 export(rk.toggle.plot.history)
diff --git a/rkward/rbackend/rpackages/rkward/R/rk.utility-functions.R b/rkward/rbackend/rpackages/rkward/R/rk.utility-functions.R
index fd99cff..80770e7 100644
--- a/rkward/rbackend/rpackages/rkward/R/rk.utility-functions.R
+++ b/rkward/rbackend/rpackages/rkward/R/rk.utility-functions.R
@@ -158,3 +158,22 @@
 		invisible (FALSE)
 	}
 }
+
+#' Switch language / translation to use in the frontend.
+#'
+#' This feature is mostly intended for the purpose of automated testing, which needs a
+#' defined language to work. It might also be useful for translators, or e.g. to look up
+#' some terms untranslated in special cases where you are more familiar with the English terms than
+#' your native language terms. Note that this will only strings that are translated after the call., only those which get
+#' translated after the call. Most new dialogs you open, and importantly new plugin dialogs should
+#' show strings in the new lanuage, however.
+#' 
+#' To change the language in the backend, use \code{Sys.setenv(LANGUAGE=...)} or \code{Sys.setlocale()}.
+#'
+#' @param LANG language code to use. "C" for no translation, i.e. generally English
+#'
+#' @export
+#' @rdname rk.switch.frontend.language
+"rk.switch.frontend.language" <- function (LANG="C") {
+   .rk.do.plain.call ("switchLanguage", as.character (LANG))
+}
diff --git a/rkward/rbackend/rpackages/rkward/man/rk.switch.frontend.language.Rd b/rkward/rbackend/rpackages/rkward/man/rk.switch.frontend.language.Rd
new file mode 100644
index 0000000..7f0bf0b
--- /dev/null
+++ b/rkward/rbackend/rpackages/rkward/man/rk.switch.frontend.language.Rd
@@ -0,0 +1,22 @@
+% Generated by roxygen2 (4.0.2): do not edit by hand
+\name{rk.switch.frontend.language}
+\alias{rk.switch.frontend.language}
+\title{Switch language / translation to use in the frontend.}
+\usage{
+rk.switch.frontend.language(LANG = "C")
+}
+\arguments{
+\item{LANG}{language code to use. "C" for no translation, i.e. generally English}
+}
+\description{
+This feature is mostly intended for the purpose of automated testing, which needs a
+defined language to work. It might also be useful for translators, or e.g. to look up
+some terms untranslated in special cases where you are more familiar with the English terms than
+your native language terms. Note that this will only strings that are translated after the call., only those which get
+translated after the call. Most new dialogs you open, and importantly new plugin dialogs should
+show strings in the new lanuage, however.
+}
+\details{
+To change the language in the backend, use \code{Sys.setenv(LANGUAGE=...)} or \code{Sys.setlocale()}.
+}
+
diff --git a/rkward/rbackend/rpackages/rkwardtests/R/rkwardtests-internal.R b/rkward/rbackend/rpackages/rkwardtests/R/rkwardtests-internal.R
index d306ccd..0888e0e 100644
--- a/rkward/rbackend/rpackages/rkwardtests/R/rkwardtests-internal.R
+++ b/rkward/rbackend/rpackages/rkwardtests/R/rkwardtests-internal.R
@@ -237,6 +237,7 @@ rktest.initializeEnvironment <- function () {
 	if (.Platform$OS.type == "unix"){
 		invisible (Sys.setlocale (category="LC_MESSAGES", locale="C"))
 	}
+	rk.switch.frontend.language ("C")
 	options (useFancyQuotes=FALSE)
 
 	# This version of rk.set.output.html.file does not notify the frontend of the change. Without this, you'll get lots of output windows.





More information about the rkward-tracker mailing list