From 7953ffc2d31c88a04687798a542d23bf3f9d6e3e Mon Sep 17 00:00:00 2001
From: Olav Morken <olav.morken@uninett.no>
Date: Wed, 2 Jul 2008 13:02:41 +0000
Subject: [PATCH] Template: add getTranslation()-method.

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

diff --git a/lib/SimpleSAML/XHTML/Template.php b/lib/SimpleSAML/XHTML/Template.php
index 3ae94f20f..aae3d1289 100644
--- a/lib/SimpleSAML/XHTML/Template.php
+++ b/lib/SimpleSAML/XHTML/Template.php
@@ -206,6 +206,44 @@ class SimpleSAML_XHTML_Template {
 	}
 
 
+	/**
+	 * Retrieve the preferred translation of a given text.
+	 *
+	 * @param $translations  The translations, as an associative array with language => text mappings.
+	 * @return The preferred translation.
+	 */
+	public function getTranslation($translations) {
+		assert('is_array($translations)');
+
+		/* Look up translation of tag in the selected language. */
+		$selected_language = $this->getLanguage();
+		if (array_key_exists($selected_language, $translations)) {
+			return $translations[$selected_language];
+		}
+
+		/* Look up translation of tag in the default language. */
+		$default_language = $this->getDefaultLanguage();
+		if(array_key_exists($default_language, $translations)) {
+			return $translations[$default_language];
+		}
+
+		/* Look up translation of tag in the base language. */
+		$base_language = $this->getBaseLanguage();
+		if(array_key_exists($base_language, $translations)) {
+			return $translations[$base_language];
+		}
+
+		/* Pick the first translation available. */
+		if(count($translations) > 0) {
+			$languages = array_keys($translations);
+			return $translations[$languages[0]];
+		}
+
+		/* We don't have anything to return. */
+		throw new Exception('Nothing to return from translation.');
+	}
+
+
 	/**
 	 * Include text in the current language.
 	 *
-- 
GitLab