WordPress.org

Forums

[Plugin: qTranslate] I couldn't GET the URL query to work so... (1 post)

  1. nouveller
    Member
    Posted 5 years ago #

    I've been using a bog standard $_GET to try and grab the lang value in the query generated by qTranslate but with no luck, I have no idea why it doesn't work?!

    So I've come up with a couple of functions to work around this problem to get the two letter language codes generated by qTranslate's 'Pre-Path' mode in the URL.

    Hopefully someone might find them useful.

    <?php
    
    	/*
    	*
    	*	function 		:	get_lang
    	*
    	*	description	:	retrieves the language code from the URL generated when the qTranslate plugin
    	*					has it's 'URL Modification Mode' set to 'Pre-Path'. this returns the two letter
    	*					language code if the function argument is empty otherwise it echo's it
    	*
    	*/
    
    	function get_lang($action = '') {
    
    		// get WordPress's install URL (this doesn't include the trailing '/')
    		// e.g. http://www.wordpress.com/install
    
    		$base_url =  get_bloginfo('wpurl');
    
    		// get the url of the current page, this should always include
    		// the install url and the trailing language code
    		// e.g. http://www.wordpress.com/install/en/...
    
    		$url = get_permalink(); 
    
    		// a regular expression that dyanmically uses the $base_url.
    		// after that it searches for the following forward slash,
    		// then two characters and a forward slash and captures the two characters
    		// e.g. http://www.wordpress.com/install/(en)/
    
    		$regex = '#'.$base_url.'/([a-z]{2})/#';
    
    		// a preg_match function to store the captured information
    		// into the $matches var
    
    		preg_match($regex, $url, $matches);
    
    		// store the first peice of captured information from the preg_match
    		// into a variable called $country which should be the language code
    
    		$country = $matches['1'];
    
    		// if the action in the function has been set to echo, echo the results
    		// if nothing is set in the arguemnt, just return it
    
    		if($action == 'echo') {
    
    			echo $country;
    
    		} else {
    
    			return $country;
    
    		}
    
    	} # end function get_lang
    
    	/*
    	*
    	*	function 		:	lang_switch
    	*
    	*	description	:	retrieves the language code from the URL generated when the qTranslate plugin
    	*					has it's 'URL Modification Mode' set to 'Pre-Path'. then echo's the messages
    	*					set in the functions argument based on what country code was retrieved from the URL
    	*
    	*	$en 			:	english message
    	*	$fr 			: 	french message
    	*	$es 			: 	spanish message
    	*	$de			: 	german message
    	*	$zh 			: 	chinese message
    	*	$ar			: 	arrabic message
    	*
    	*/
    
    	function lang_switch($en = '', $fr = '', $es = '', $de = '', $zh = '', $ar = '') {
    
    		// get_lang gets the country code and it's then stored in $country
    
    		$country = get_lang();
    
    		// echo's the function arguments based on what language code was obtained from the URL
    
    		switch($country){
    
    			case 'en' : echo $en; break;
    
    			case 'fr' : echo $fr; break;
    
    			case 'es' : echo $es; break;
    
    			case 'de' : echo $de; break;
    
    			case 'zh' : echo $zh; break;
    
    			case 'ar' : echo $ar; break;
    
    		}
    
    	} # end function language_switch
    
    	//lang_switch('English', 'Français', 'Español', 'Deutsch', '中文', 'العربية');
    
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.