All pastes #403779 Raw Edit

Unnamed

public text v1 · immutable
·published 2007-03-20 16:57 UTC
rendered paste body
<?php

/**
 * CyCle IDE, Gettext Manager
 *
 * @author Leon Pegg
 * @package CyCleIDE
 */

# Note : all constants should be UPPERCASE and words separated with _
# Note : all classes should be CamelCaps no _
# Note : methods and class properties should all have modifiers (public/private/protected)
# Note : static modifiers must precede method and properties modifiers (public/private/protected)
# Note : all variables should be lowercase no _
# Note : all methods/functions should be lowercase and words seperated with _
# Note : { should not be on new line

require_once('ini.php');

if (!extension_loaded('xml')) {
	echo 'The XML Extention is not present.';
	die();
}

if (!function_exists('gettext')) {
	function bind_textdomain_codeset($domain, $codeset ){}
	function bindtextdomain($domain, $directory){}
	function dcgettext($domain, $message, $category){}
	function dcngettext($domain, $msgid1, $msgid2, $n, $category){}
	function dgettext($domain, $message){}
	function dngettext($domain, $msgid1, $msgid2, $n){}
	function gettext($message){return $message;}
	function ngettext($msgid1, $msgid2, $n){}
	function textdomain($textdomain){}
	function _($message){return $message;}
	define('GETTEXT_SUPPORTED', false);
} else {
	define('GETTEXT_SUPPORTED', true);
}

class IdeGettext {

	const SUPPORTED = GETTEXT_SUPPORTED;

	static private $instance = null;

	protected $country = 'GB';
	protected $language = 'en';

	protected $languages = array('GB' => array('en' => 'en'));
	protected $countrylong = array('GB' => 'Britain (UK).');
	protected $languagelong = array('en' => 'English.');

	protected function __construct() {
		if (IdeGettext::SUPPORTED == false) {
			return;
		}
		// singlton
	}

	protected function __clone(){
		// singlton
	}
	
	/**
	 * Get an instance of IdeGettext
	 *
	 * @return Idegettext
	 */
	static public function get_instance() {
		if (self::$instance == null) {
			self::$instance = new self;
		}
		return self::$instance;
	}

	/**
	 * Get an array of installed countries
	 *
	 * @return array
	 */
	public function get_countries() {
		// get countries
		$countries = array();
		foreach ($this->languages as $country => $languages) {
			$countries[] = $country;
		}
		return $countries;
	}

	/**
	 * Get an array of installed languages for country
	 *
	 * @param string_type $country
	 * @return array
	 */
	public function get_languages($country) {
		// get languages
		return $this->languages[$country];
	}

	/**
	 * Set gettext to langauge
	 *
	 * @param string $country
	 * @param string $language
	 */
	public function set_language($country, $language) {
		// set language
		if (array_search($language,$this->languages[$country])) {
			$this->country = $country;
			$this->language = $language;
			$code = $language.'_'.$country;
			$domain = 'cycle';
			bindtextdomain($domain, Ide::LOCALE_DIR);
			textdomain($domain);
			putenv("LANG=$code"); // windows is stupid and needs this trick
			putenv("LANGUAGE=$code"); // windows is stupid and needs this trick
			setlocale(LC_ALL, $code);
		}
	}

	/**
	 * Get current language code
	 *
	 * @return string
	 */
	public function get_language() {
		return $this->language;
	}

	/**
	 * Get language full name
	 *
	 * @param string $language
	 * @return string
	 */
	public function get_language_long($language){
		return $this->languagelong[$language];
	}

	/**
	 * Get country full name
	 *
	 * @param string $country
	 * @return string
	 */
	public function get_country_long($country){
		return $this->countrylong[$country];
	}

	/**
	 * Get current country code
	 *
	 * @return string
	 */
	public function get_country() {
		return $this->country;
	}

}

$settings = IdeIni::get_instance();
$gettext = IdeGettext::get_instance();

$country = $settings->get_value('gettext', 'country');
$language = $settings->get_value('gettext', 'language');
echo $gettext->get_language_long($language).' '.$gettext->get_country_long($country)."\n";
$gettext->set_language($country, $language);

?>