From b2bcd453375f3ddf9a8e86612bbd53581458c5c5 Mon Sep 17 00:00:00 2001
From: Olav Morken <olav.morken@uninett.no>
Date: Wed, 2 Jul 2008 13:03:20 +0000
Subject: [PATCH] Template: add support for dictionary names in tag names.

This patch makes it possible to write '{<dictionary>:<tag>}' as the tag
name. This will load the given dictionary and fetch the given tag from there.


git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@753 44740490-163a-0410-bde0-09ae8108e29a
---
 lib/SimpleSAML/XHTML/Template.php | 43 ++++++++++++++++++++++++++++---
 1 file changed, 40 insertions(+), 3 deletions(-)

diff --git a/lib/SimpleSAML/XHTML/Template.php b/lib/SimpleSAML/XHTML/Template.php
index ea93c0cc3..ff3f62749 100644
--- a/lib/SimpleSAML/XHTML/Template.php
+++ b/lib/SimpleSAML/XHTML/Template.php
@@ -24,6 +24,13 @@ class SimpleSAML_XHTML_Template {
 	
 	public $data = null;
 
+
+	/**
+	 * Associative array of dictionaries.
+	 */
+	private $dictionaries = array();
+
+
 	/**
 	 * Constructor
 	 *
@@ -187,22 +194,52 @@ class SimpleSAML_XHTML_Template {
 	}
 
 
+	/**
+	 * Retrieve a dictionary.
+	 *
+	 * This function retrieves a dictionary with the given name.
+	 *
+	 * @param $name  The name of the dictionary, as the filename in the dictionary directory,
+	 *               without the '.php'-ending.
+	 * @return  An associative array with the dictionary.
+	 */
+	private function getDictionary($name) {
+		assert('is_string($name)');
+
+		if(!array_key_exists($name, $this->dictionaries)) {
+			$dictDir = $this->configuration->getPathValue('dictionarydir');
+			$this->dictionaries[$name] = $this->readDictionaryFile($dictDir . $name . '.php');
+		}
+
+		return $this->dictionaries[$name];
+	}
+
+
 	/**
 	 * Retrieve a tag.
 	 *
 	 * This function retrieves a tag as an array with language => string mappings.
 	 *
-	 * @param $tag  The tag name.
+	 * @param $tag  The tag name. The tag name can also be on the form '{<dictionary>:<tag>}', to retrieve
+	 *              a tag from the specific dictionary.
 	 * @return As associative array with language => string mappings, or NULL if the tag wasn't found.
 	 */
 	public function getTag($tag) {
 		assert('is_string($tag)');
 
-		if(!array_key_exists($tag, $this->langtext)) {
+		if(substr($tag, 0, 1) === '{' && preg_match('/^{(\w+?):(.*)}$/', $tag, $matches)) {
+			$dictionary = $matches[1];
+			$tag = $matches[2];
+			$dictionary = $this->getDictionary($dictionary);
+		} else {
+			$dictionary = $this->langtext;
+		}
+
+		if(!array_key_exists($tag, $dictionary)) {
 			return NULL;
 		}
 
-		return $this->langtext[$tag];
+		return $dictionary[$tag];
 	}
 
 
-- 
GitLab