WordPress: get enabled languages for qTranslate

Whilst qTranslate lists various useful functions on the forum sticky post “Functions for Developers and Template Authors“, it’s definitely a little thin on details.

I needed a way to check the currently enabled languages in qTranslate so that I could automatically populate a meta box in the back end of a client’s website. This meta box needed to auto-update if they ever add a new language to the site.

Fortunately this is possible with the following function:

qtrans_getSortedLanguages();

This function has one optional parameter, $reverse, which is a boolean defaulting to false. So if you need to get the languages in reverse order from the user set order, just run qtrans_getSortedLanguages(TRUE);

The function can be found in qtranslate_utils.php in the root of the qTranslate plugin. This file has a whole chunk of functions, so if you’re struggling have a look through what’s available and something might help you out.

Below is the full function for reference.

function qtrans_getSortedLanguages($reverse = false) {
	global $q_config;
	$languages = $q_config['enabled_languages'];
	ksort($languages);
	// fix broken order
	$clean_languages = array();
	foreach($languages as $lang) {
		$clean_languages[] = $lang;
	}
	if($reverse) krsort($clean_languages);
	return $clean_languages;
}

Comments