TYPO3-Testaccount
Testen Sie die aktuelle TYPO3 Version 4.4.2 kostenlos und unverbindlich für einen Monat!

jetzt testen

Schulungen

jetzt buchen

Weiterer Dropdown Sprachumschalter


Autor Nachricht
Verfasst am: 17. 11. 2008 [12:16]
einpraegsam.net
Moderator
Themenersteller
Dabei seit: 17.01.2005
Beiträge: 8534
So, vermutlich gibt es so etwas schon in der einen oder anderen Art - aber vielleicht kann es ja trotzdem jemand brauchen:

Beispiel:
http://www.conject.com/en/diverse-daten/test/languageswitcher.html

Typoscript:
TYPOSCRIPT
includeLibs.user_langswitch = fileadmin/user_langswitch.php
 
temp.languageswitcher = COA
temp.languageswitcher {
	10 = USER_INT
	10.userFunc = user_langswitch->start
	10.userFunc.lang = en,de,es,pt,ru,me
	10.userFunc.label = en:English (Americas/Europe),me:English (Middle East/Asia-Pac),de<img src="typo3conf/ext/mm_forum//res/smilies/icon_biggrin.gif" alt="icon_biggrin.gif" />eutsch (German), ru:Russian (Russian), es:, pt:,
}
 
page.10.marks.languageswitcher < temp.languageswitcher


userFunc:
PHP
<?php
 
	class user_langswitch {
 
		var $debug = 0; // debug output (viewable code)
 
		// Main function for languageswitcher selectbox
		function start($content='', $conf=array()) {
			// config
			global $TSFE;
    		$this->cObj = $TSFE->cObj; // cObject
			$this->label = array(); $code = $all = ''; // init
			$this->lang = t3lib_div::trimExplode(',', $conf['userFunc.']['lang'], 1); // array with languages
			$tmp_label = t3lib_div::trimExplode(',', $conf['userFunc.']['label'], 1); // array with labels
			foreach ($tmp_label as $key => $value) { // one loop for every label
				$tmp_curlabel = t3lib_div::trimExplode(':', $value, 1); // split on :
				$this->label[$tmp_curlabel[0]] = $tmp_curlabel[1]; // e.g. "en" => "switch to english"
			}
 
			// let's go
			if (is_array($this->label) && count($this->label) > 0 && is_array($this->lang) && count($this->lang) > 0) { // only if settings available (label and lang keys)
				foreach ($this->label as $key => $value) { // one loop for every allowed label
					if ($value) {
						$code .= '<option value="' . $this->makeLink($key) . '"' . ($this->lang[$_GET['L']] == $key ? ' selected="selected"' : '') . '>'; // option prefix
						$code .= $value; // value
						$code .= '</option>'; // option postfix
					}
				}
 
				$all .= $this->debug ? '<textarea cols="50" rows="20">' : '';
				$all .= '<form name="language" action="" class="languageselectform">';
				$all .= '<select name="select" onchange="goto(this.form)" class="languageselect">';
				$all .= $code;
				$all .= '</select>';
				$all .= '</form>';
				$all .= $this->addJS();
				$all .= $this->debug ? '</textarea>' : '';
			}
 
			// return
			return $all;
		}
 
 
		// Function makeLink() generate localized link from language key (en, de)
		function makeLink($key) {
			$tmp_lang = array_flip($this->lang); // get array like en=>0, de=>1
			return '/'.$this->cObj->typolink('x', array('returnLast' => 'url', 'parameter' => $GLOBALS['TSFE']->id, 'additionalParams' => '&L='.$tmp_lang[$key])); // return link
		}
 
 
		// Add javascript for redirection
		function addJS() {
			$js = '<script type="text/javascript">
						function goto(form) { 
							var index = form.select.selectedIndex;
							if (form.select.options[index].value != "0&quot<img src="typo3conf/ext/mm_forum//res/smilies/icon_wink.gif" alt="icon_wink.gif" /> {
								location = form.select.options[index].value;
							}
						}
					</script>
				';
			return $js;
		}
 
 
	}
 
?>


Installation:
- Datei user_langswitch.php in fileadmin anlegen (beispielsweise)
- Per TS Datei einbinden
- Mit userFunc auf Klasse zugreifen und Sprachen und Labels definieren
- Wichtig: Reihenfolge von lang (bei mir &L=0 ist en, &L=1 ist de, etc...)


Grüße, Alex

ProfilWWW
Verfasst am: 04. 05. 2009 [13:34]
ristic
Dabei seit: 21.02.2008
Beiträge: 146
Exzellent, genau das hab ich gebraucht. icon_biggrin.gif Danke erst mal! Hab es noch etwas erweitert, falls mal eine Sprache gelöscht wurde, funktioniert das Ganze leider nur wenn man im TS bei userFunc.lang etwas trickst. Mit diesen Änderungen im TS und im PHP vor der ersten foreach-Schleife sollte es klappen.

TYPOSCRIPT
temp.languageswitcher = COA
temp.languageswitcher {
  	10 = USER_INT
  	10.userFunc = user_langswitch->start
  	# was hier gesetzt wird wird auch nur angezeigt
	  10.userFunc.lang = en,de_DE,fr,es,pl,fr_FR
        # hier werden die richtigen IDs gesetzt, so kann auch nix schief laufen
	  10.userFunc.id = 0, 1, 4, 5, 6, 7
  	10.userFunc.label = en:International (English),de_DE<img src="typo3conf/ext/mm_forum//res/smilies/icon_biggrin.gif" alt="icon_biggrin.gif" />eutschland (Deutsch),fr:International (Français)
}


Das vor die erste foreach-Schleife.
PHP
// get the ids of the language from the TS and combine it
$this->id = t3lib_div::trimExplode(',', $conf['userFunc.']['id'], 1); // array with ids	
$this->lang = array_combine($this->id, $this->lang);


Danke nochmal an Alex! icon_wink.gif Falls ich dazu komme, werd ich hierzu mal eine Extension erstellen.

Gruß

Daniel

Life moves pretty fast. You don’t stop and look around once in a while, you could miss it. [Ferris Bueller]
ProfilWWW
Verfasst am: 09. 06. 2009 [11:22]
massel
Dabei seit: 06.07.2005
Beiträge: 1
Sprachwechsler funktioniert wunderbar, aber nun mault der Validator.
general entity "L" not defined and no default entity


icon_cry.gif

Weiß jemand Rat?

Gruß Massel
Profil