<commit_before><?php
/**
 * Habari Locale Class
 *
 * Provides translation services.
 * @package Habari
 */

class Locale
{
	static $uselocale = false;
	static $messages = array();
	static $locale;

	/**
	 * function set
	 * Sets the locale for the whole system
	 * @param string A locale string such as 'en' or 'en_US'
	 **/	 
	public static function set($locale)
	{
		self::$locale = $locale;
		//self::load_domain('habari');
	}
	
	/**
	 * function load_domain
	 * Loads a translation domain from a locale .mo file into the Locale class.
	 * This function reads a cryptic but common translation file format.  Its 
	 * internal workings are not entirely meant to be understood.	 
	 * @param string The domain string to load
	 **/	 	 	 	
	private static function load_domain($domain)
	{
		$file = HABARI_PATH . '/system/locale/' . self::$locale . '/LC_MESSAGES/' . $domain . '.mo';
		if ( file_exists( $file ) ) { 
			$fp = fopen($file, 'rb');
			$data = fread($fp, filesize($file));
			fclose($fp);
			
			if ( $data ) {
				$header = substr( $data, 8, 12 );
				$header = unpack( 'L1msgcount/L1msgblock/L1transblock', $header );
			
				for ( $msgindex = 0; $msgindex < $header['msgcount']; $msgindex++ ) {
					$msginfo = unpack( 'L1length/L1offset', substr( $data, $header['msgblock'] + $msgindex * 8, 8 ) );
					list( $msgid, $msgid2 ) = explode( "\0", substr( $data, $msginfo['offset'], $msginfo['length'] ) );
					$transinfo = unpack( 'L1length/L1offset', substr( $data, $header['transblock'] + $msgindex * 8, 8 ) );
					list( $trans, $trans2 ) = explode( "\0", substr( $data, $transinfo['offset'], $transinfo['length'] ) );
					self::$messages[$domain][$msgid] = array(
						array( $msgid, $msgid2 ),
						array( $trans, $trans2 )
					);
				}
			}
		}
	}
	
	/**
	 * function _e
	 * Echo a version of the string translated into the current locale
	 * @param string The text to echo translated
	 * @param string The domain to search for the message	 
	 **/	 	 	 	
	public static function _e($text, $domain = 'habari')
	{
		echo self::__($text);
	}
	
	/**
	 * function __
	 * Return a version of the string translated into the current locale
	 * @param string The text to translate
	 * @param string The domain to search for the message	 
	 * @return string The string, translated
	 **/	 	 	 	 	 	
	public static function __($text, $domain = 'habari')
	{
		if ( isset( self::$messages[$domain][$text] ) ) {
			return self::$messages[$domain][$text][1][0];
		}
		else {
			return $text;
		}
	}
	
	/**
	 * function _n
	 * Return a singular or plural string translated into the current 
	 * locale based on the count provided.
	 * @param string The singular form
	 * @param string The plural form
	 * @param string The count
	 * @param string The domain to search for the message	 
	 * @return string The appropriately translated string
	 **/       
	public static function _n( $singular, $plural, $count, $domain= 'habari' )
	{
		if ( isset( self::$messages[$domain][$singular] ) ) {
			return ( $count == 1 ? self::$messages[$domain][$singular][1][0] : self::$messages[$domain][$singular][1][1] );
		}
		else {
			return ( $count == 1 ? $singular : $plural );
		}
	}
}

/**
 * function _e
 * Echo a version of the string translated into the current locale
 * Alias for Locale::_e() 
 * @param string The text to echo translated
 **/	 	 	 	
function _e($text)
{
	return Locale::_e($text);
}

/**
 * function __
 * Return a version of the string translated into the current locale
 * Alias for Locale::__() 
 * @param string The text to translate
 * @return string The string, translated
 **/	 	 	 	 	 	
function __($text)
{
	return Locale::__($text);
}

/**
 * function _n
 * Return a singular or plural string translated into the current 
 * locale based on the count provided.
 * @param string The singular form
 * @param string The plural form
 * @param string The count
 * @return string The appropriately translated string
 **/       
function _n($singular, $plural, $count)
{
	return Locale::_n($singular, $plural, $count);
}


?><commit_msg>- Fixes http://code.google.com/p/habari/issues/detail?id=97 - Locale does not load_domain - Basic i18n functionality should be complete now, now it's wrapping literals and translating...<commit_after><?php
/**
 * Habari Locale Class
 *
 * Provides translation services.
 * 
 * @package Habari
 */

class Locale
{
	static $uselocale= false;
	static $messages= array();
	static $locale;

	/**
	 * function set
	 * Sets the locale for the whole system
	 * @param string A locale string such as 'en' or 'en_US'
	 **/	 
	public static function set( $locale )
	{
		self::$locale= $locale;
		self::load_domain( 'habari' );
	}
	
	/**
	 * function load_domain
	 * Loads a translation domain from a locale .mo file into the Locale class.
	 * This function reads a cryptic but common translation file format.  Its 
	 * internal workings are not entirely meant to be understood.	 
	 * @param string The domain string to load
	 **/	 	 	 	
	private static function load_domain( $domain )
	{
		$file= HABARI_PATH . '/system/locale/' . self::$locale . '/LC_MESSAGES/' . $domain . '.mo';
		if ( file_exists( $file ) ) { 
			$fp= fopen( $file, 'rb' );
			$data= fread( $fp, filesize( $file ) );
			fclose( $fp );
			
			if ( $data && strlen( $data ) >= 20 ) {
				$header= substr( $data, 8, 12 );
				$header= unpack( 'L1msgcount/L1msgblock/L1transblock', $header );
			
				for ( $msgindex= 0; $msgindex < $header['msgcount']; $msgindex++ ) {
					$msginfo= unpack( 'L1length/L1offset', substr( $data, $header['msgblock'] + $msgindex * 8, 8 ) );
					list( $msgid, $msgid2 )= explode( "\0", substr( $data, $msginfo['offset'], $msginfo['length'] ) );
					$transinfo= unpack( 'L1length/L1offset', substr( $data, $header['transblock'] + $msgindex * 8, 8 ) );
					list( $trans, $trans2 )= explode( "\0", substr( $data, $transinfo['offset'], $transinfo['length'] ) );
					self::$messages[$domain][$msgid] = array(
						array( $msgid, $msgid2 ),
						array( $trans, $trans2 )
					);
				}
			}
		}
	}
	
	/**
	 * function _e
	 * Echo a version of the string translated into the current locale
	 * @param string The text to echo translated
	 * @param string The domain to search for the message	 
	 **/	 	 	 	
	public static function _e( $text, $domain = 'habari' )
	{
		echo self::__( $text );
	}
	
	/**
	 * function __
	 * Return a version of the string translated into the current locale
	 * @param string The text to translate
	 * @param string The domain to search for the message	 
	 * @return string The string, translated
	 **/	 	 	 	 	 	
	public static function __( $text, $domain = 'habari' )
	{
		if ( isset( self::$messages[$domain][$text] ) ) {
			return self::$messages[$domain][$text][1][0];
		}
		else {
			return $text;
		}
	}
	
		
	/**
	 * function _ne
	 * Echo singular or plural version of the string, translated into the current locale, based on the count provided.
	 * @param string The singular form
	 * @param string The plural form
	 * @param string The count
	 * @param string The domain to search for the message	 
	 **/	 	 	 	
	public static function _ne( $singular, $plural, $count, $domain= 'habari' )
	{
		echo self::_n( $singular, $plural, $count, $domain );
	}
	
	/**
	 * function _n
	 * Return a singular or plural string translated into the current 
	 * locale based on the count provided.
	 * @param string The singular form
	 * @param string The plural form
	 * @param string The count
	 * @param string The domain to search for the message	 
	 * @return string The appropriately translated string
	 **/       
	public static function _n( $singular, $plural, $count, $domain= 'habari' )
	{
		if ( isset( self::$messages[$domain][$singular] ) ) {
			return ( $count == 1 ? self::$messages[$domain][$singular][1][0] : self::$messages[$domain][$singular][1][1] );
		}
		else {
			return ( $count == 1 ? $singular : $plural );
		}
	}
}

/**
 * function _e
 * Echo a version of the string translated into the current locale
 * Alias for Locale::_e() 
 * @param string The text to echo translated
 **/	 	 	 	
function _e( $text )
{
	return Locale::_e( $text );
}

/**
 * function _ne
 * Echo singular or plural version of the string, translated into the current locale, based on the count provided.
 * Alias for Locale::_ne()
 * @param string The singular form
 * @param string The plural form
 * @param string The count
 **/	 	
function _ne( $singular, $plural, $count )
{
	return Locale::_ne( $singular, $plural, $count );
}

/**
 * function __
 * Return a version of the string translated into the current locale
 * Alias for Locale::__() 
 * @param string The text to translate
 * @return string The string, translated
 **/	 	 	 	 	 	
function __( $text )
{
	return Locale::__( $text );
}

/**
 * function _n
 * Return a singular or plural string translated into the current 
 * locale based on the count provided.
 * @param string The singular form
 * @param string The plural form
 * @param string The count
 * @return string The appropriately translated string
 **/       
function _n( $singular, $plural, $count )
{
	return Locale::_n( $singular, $plural, $count );
}

?>
{'fraction_non_alphanumeric': 0.13846994535519125, 'fraction_numerical': 0.006994535519125683, 'mean_word_length': 3.268190298507463, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2625037', 'n_tokens_mistral': 3200, 'n_tokens_neox': 2836, 'n_words': 991}