From 9f01cf6a532f98b10f726bbbac3228880f6c3d41 Mon Sep 17 00:00:00 2001
From: Gabriel Birke <Gabriel.Birke@delti.com>
Date: Fri, 26 Oct 2007 15:11:30 +0200
Subject: [PATCH] Translatable JavaScript strings for plugins

Strings to be used in plugin provided JavaScript can now be put into the plugin's
lang.php files. It has to be stored as subkeys of $lang['js']. Eg the following in
lib/plugins/blah/lang/en/lang.php

$lang['js']['foo']

darcs-hash:20071026131130-79ce3-75ab69b1ba527c823e0e5ef0fde031032aaa2548.gz
---
 lib/exe/js.php | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)

diff --git a/lib/exe/js.php b/lib/exe/js.php
index 17f9c5ae1..c4fd81616 100644
--- a/lib/exe/js.php
+++ b/lib/exe/js.php
@@ -85,6 +85,9 @@ function js_out(){
     print "var notSavedYet = '".js_escape($lang['notsavedyet'])."';";
     print "var reallyDel   = '".js_escape($lang['del_confirm'])."';";
 
+    // load JS strings form plugins
+    $lang['js']['plugins'] = js_pluginstrings();
+    
     // load JS specific translations
     $json = new JSON();
     echo 'LANG = '.$json->encode($lang['js']).";\n";
@@ -236,6 +239,34 @@ function js_pluginscripts(){
     return $list;
 }
 
+/**
+ * Return an two-dimensional array with strings from the language file of each plugin.
+ *
+ * - $lang['js'] must be an array. 
+ * - Nothing is returned for plugins without an entry for $lang['js']
+ *
+ * @author Gabriel Birke <birke@d-scribe.de>
+ */
+function js_pluginstrings()
+{
+    global $conf;
+    $pluginstrings = array();
+    $plugins = plugin_list();
+    foreach ($plugins as $p){
+        if (isset($lang)) unset($lang);
+        if (@file_exists(DOKU_PLUGIN."$p/lang/en/lang.php")) {
+            include DOKU_PLUGIN."$p/lang/en/lang.php";
+        }
+        if (isset($conf['lang']) && $conf['lang']!='en' && @file_exists(DOKU_PLUGIN."$p/lang/".$conf['lang']."/lang.php")) {
+            include DOKU_PLUGIN."$p/lang/".$conf['lang']."/lang.php";
+        }
+        if (isset($lang['js'])) {
+            $pluginstrings[$p] = $lang['js'];
+        }
+    }
+    return $pluginstrings;
+}
+
 /**
  * Escapes a String to be embedded in a JavaScript call, keeps \n
  * as newline
-- 
GitLab