From 8703b0801865eb47505c960037d916253e315fc7 Mon Sep 17 00:00:00 2001
From: alecpl <alec@alec.pl>
Date: Mon, 12 Sep 2011 08:52:01 -0400
Subject: [PATCH] - Extend rcube_label_exists() to search in loaded plugins localizations - Allow use localized addressbook field subtypes from plugins

---
 program/include/rcube_plugin_api.php |   11 +++++++++++
 program/include/main.inc             |    4 ++--
 program/include/rcmail.php           |   37 ++++++++++++++++++++++++++++++-------
 program/steps/addressbook/func.inc   |    8 ++++----
 4 files changed, 47 insertions(+), 13 deletions(-)

diff --git a/program/include/main.inc b/program/include/main.inc
index 3513a07c..909d0a5 100644
--- a/program/include/main.inc
+++ b/program/include/main.inc
@@ -97,9 +97,9 @@
  *
  * @see rcmail::text_exists()
  */
-function rcube_label_exists($name, $domain=null)
+function rcube_label_exists($name, $domain=null, &$ref_domain = null)
 {
-  return rcmail::get_instance()->text_exists($name, $domain);
+  return rcmail::get_instance()->text_exists($name, $domain, $ref_domain);
 }
 
 
diff --git a/program/include/rcmail.php b/program/include/rcmail.php
index f126a91..b364bd6 100644
--- a/program/include/rcmail.php
+++ b/program/include/rcmail.php
@@ -970,7 +970,9 @@
   /**
    * Get localized text in the desired language
    *
-   * @param mixed Named parameters array or label name
+   * @param mixed   $attrib  Named parameters array or label name
+   * @param string  $domain  Label domain (plugin) name
+   *
    * @return string Localized text
    */
   public function gettext($attrib, $domain=null)
@@ -985,7 +987,7 @@
 
     $nr = is_numeric($attrib['nr']) ? $attrib['nr'] : 1;
     $name = $attrib['name'] ? $attrib['name'] : '';
-    
+
     // attrib contain text values: use them from now
     if (($setval = $attrib[strtolower($_SESSION['language'])]) || ($setval = $attrib['en_us']))
         $this->texts[$name] = $setval;
@@ -1041,19 +1043,40 @@
 
 
   /**
-   * Check if the given text lable exists
+   * Check if the given text label exists
    *
-   * @param string Label name
+   * @param string  $name       Label name
+   * @param string  $domain     Label domain (plugin) name or '*' for all domains
+   * @param string  $ref_domain Sets domain name if label is found
+   *
    * @return boolean True if text exists (either in the current language or in en_US)
    */
-  public function text_exists($name, $domain=null)
+  public function text_exists($name, $domain = null, &$ref_domain = null)
   {
     // load localization files if not done yet
     if (empty($this->texts))
       $this->load_language();
 
-    // check for text with domain first
-    return ($domain && isset($this->texts[$domain.'.'.$name])) || isset($this->texts[$name]);
+    if (isset($this->texts[$name])) {
+        $ref_domain = '';
+        return true;
+    }
+
+    // any of loaded domains (plugins)
+    if ($domain == '*') {
+      foreach ($this->plugins->loaded_plugins() as $domain)
+        if (isset($this->texts[$domain.'.'.$name])) {
+          $ref_domain = $domain;
+          return true;
+        }
+    }
+    // specified domain
+    else if ($domain) {
+      $ref_domain = $domain;
+      return isset($this->texts[$domain.'.'.$name]);
+    }
+
+    return false;
   }
 
   /**
diff --git a/program/include/rcube_plugin_api.php b/program/include/rcube_plugin_api.php
index 0e38a31..4846cd9 100644
--- a/program/include/rcube_plugin_api.php
+++ b/program/include/rcube_plugin_api.php
@@ -422,6 +422,17 @@
 
 
   /**
+   * Returns list of loaded plugins names
+   *
+   * @return array List of plugin names
+   */
+  public function loaded_plugins()
+  {
+    return array_keys($this->plugins);
+  }
+
+
+  /**
    * Callback for template_container hooks
    *
    * @param array $attrib
diff --git a/program/steps/addressbook/func.inc b/program/steps/addressbook/func.inc
index b290bbb..a149154 100644
--- a/program/steps/addressbook/func.inc
+++ b/program/steps/addressbook/func.inc
@@ -408,12 +408,12 @@
 function rcmail_get_type_label($type)
 {
     $label = 'type'.$type;
-    if (rcube_label_exists($label))
-        return rcube_label($label);
+    if (rcube_label_exists($label, '*', $domain))
+        return rcube_label($label, $domain);
     else if (preg_match('/\w+(\d+)$/', $label, $m)
             && ($label = preg_replace('/(\d+)$/', '', $label))
-            && rcube_label_exists($label))
-        return rcube_label($label) . ' ' . $m[1];
+            && rcube_label_exists($label, '*', $domain))
+        return rcube_label($label, $domain) . ' ' . $m[1];
 
     return ucfirst($type);
 }

--
Gitblit v1.9.1