Add menu for users to switch their language
Add menu for users to switch their language

I want to present the language in the user's native language... however
there's no built-in way to translate, say, en_US to 'English', so I had
to provide a switch case mapping.

--- a/css/gitphp.css
+++ b/css/gitphp.css
@@ -90,6 +90,14 @@
 	float: right;
 	border-width: 0px;
 }
+div.lang_select {
+	font-size: 12px;
+	font-weight: normal;
+	width: 300px;
+	float: right;
+	text-align: right;
+	padding-right: 10px;
+}
 .respectwhitespace {
 	white-space: pre;
 }

--- a/include/Resource.class.php
+++ b/include/Resource.class.php
@@ -86,6 +86,70 @@
 	}
 
 	/**
+	 * GetLocaleName
+	 *
+	 * Gets the current instantiated locale's name
+	 *
+	 * @access public
+	 * @static
+	 * @return string locale name
+	 */
+	public static function GetLocaleName()
+	{
+		return GitPHP_Resource::LocaleToName(self::$currentLocale);
+	}
+
+	/**
+	 * LocaleToName
+	 *
+	 * Given a locale, returns a human readable name
+	 *
+	 * @access public
+	 * @static
+	 * @param string $locale locale
+	 * return string name
+	 */
+	public static function LocaleToName($locale)
+	{
+		switch ($locale) {
+			case 'en_US':
+				return 'English';
+			case 'fr_FR':
+				return 'Français';
+			case 'zz_Debug':
+				return 'Gibberish';
+		}
+		return '';
+	}
+
+	/**
+	 * SupportedLocales
+	 *
+	 * Gets the list of supported locales and their languages
+	 *
+	 * @access public
+	 * @static
+	 * @return array list of locales mapped to languages
+	 */
+	public static function SupportedLocales()
+	{
+		$locales = array();
+
+		$locales['en_US'] = GitPHP_Resource::LocaleToName('en_US');
+
+		if ($dh = opendir(GITPHP_LOCALEDIR)) {
+			while (($file = readdir($dh)) !== false) {
+				$fullPath = GITPHP_LOCALEDIR . '/' . $file;
+				if ((strpos($file, '.') !== 0) && is_dir($fullPath) && is_file($fullPath . '/gitphp.mo')) {
+					$locales[$file] = GitPHP_Resource::LocaleToName($file);
+				}
+			}
+		}
+		
+		return $locales;
+	}
+
+	/**
 	 * Instantiate
 	 *
 	 * Instantiates the singleton instance

--- a/include/controller/ControllerBase.class.php
+++ b/include/controller/ControllerBase.class.php
@@ -251,6 +251,22 @@
 		if (isset($this->params['searchtype']))
 			$this->tpl->assign('searchtype', $this->params['searchtype']);
 		$this->tpl->assign('resources', GitPHP_Resource::GetInstance());
+		$this->tpl->assign('currentlocale', GitPHP_Resource::GetLocale());
+		$this->tpl->assign('supportedlocales', GitPHP_Resource::SupportedLocales());
+
+		$getvars = explode('&', $_SERVER['QUERY_STRING']);
+		$getvarsmapped = array();
+		foreach ($getvars as $varstr) {
+			$eqpos = strpos($varstr, '=');
+			if ($eqpos > 0) {
+				$var = substr($varstr, 0, $eqpos);
+				$val = substr($varstr, $eqpos + 1);
+				if (!(empty($var) || empty($val))) {
+					$getvarsmapped[$var] = urldecode($val);
+				}
+			}
+		}
+		$this->tpl->assign('requestvars', $getvarsmapped);
 	}
 
 	/**

--- a/templates/header.tpl
+++ b/templates/header.tpl
@@ -43,6 +43,22 @@
       <a href="http://git-scm.com" title="git homepage">
         <img src="images/git-logo.png" width="72" height="27" alt="git" class="logo" />
       </a>
+      {if $supportedlocales}
+      <div class="lang_select">
+        <form action="{$SCRIPT_NAME}" method="get">
+	{foreach from=$requestvars key=var item=val}
+	<input type="hidden" name="{$var}" value="{$val}" />
+	{/foreach}
+	<label for="l">language:</label>
+	<select name="l">
+	  {foreach from=$supportedlocales key=locale item=language}
+	    <option {if $locale == $currentlocale}selected="selected"{/if} value="{$locale}">{$language}</option>
+	  {/foreach}
+	</select>
+	<input type="submit" value="Set" />
+	</form>
+      </div>
+      {/if}
       <a href="index.php">{if $homelink}{$homelink}{else}{t}projects{/t}{/if}</a> / 
       {if $project}
         <a href="{$SCRIPT_NAME}?p={$project->GetProject()|urlencode}&a=summary">{$project->GetProject()}</a>

comments