mako\I18n


Description


Internationalization class.


Class methods


Toggle source

protected __construct()


Protected constructor since this is a static class.

Return value

NULL

protected function __construct()
{
	// Nothing here
}

Toggle source

protected static languageExists($language)


Checks if a language pack exists and throws an exception if it doesn't.


Parameters

Type Description
string Name of the language pack
Return value

NULL

protected static function languageExists($language)
{
	if(!is_dir(MAKO_APPLICATION.'/i18n/' . $language))
	{
		throw new RuntimeException(vsprintf("%s(): The '%s' language pack does not exist.", array(__METHOD__, $language)));
	}
}

Toggle source

public static language($language = NULL)


Set and/or get the default language.


Parameters

Type Description
string (optional) Name of the language pack
Return value

string

public static function language($language = null)
{
	if($language !== null)
	{
		static::languageExists($language);

		static::$language = $language;
	}

	return static::$language;
}

Toggle source

public static translate($string, $vars = array ( ), $language = NULL)


Returns a translated string of the current language. If no translation exists then the submitted string will be returned.


Parameters

Type Description
string Text to translate
array (optional) Value or array of values to replace in the translated text
string (optional) Name of the language you want to translate to
Return value

string

public static function translate($string, array $vars = array(), $language = null)
{
	$language = $language === null ? static::$language : $language;

	if(empty(static::$strings[$language]))
	{			
		static::loadStrings($language);
	}

	$string = isset(static::$strings[$language][$string]) ? static::$strings[$language][$string] : $string;

	return (empty($vars)) ? $string : vsprintf($string, $vars);
}

Toggle source

public static plural($word, $count = NULL, $language = NULL)


Returns the plural form of a noun.


Parameters

Type Description
string Noun to pluralize
int (optional) Number of "<noun>s"
string (optional) Language rules to use for pluralization
Return value

string

public static function plural($word, $count = null, $language = null)
{
	$language = $language === null ? static::$language : $language;

	if(empty(static::$inflection[$language]))
	{			
		static::loadInflection($language);
	}

	return call_user_func(static::$inflection[$language]['pluralize'], $word, $count, static::$inflection[$language]['rules']);
}

Toggle source

protected static loadInflection($language)


Loads the inflection rules for the requested language.


Parameters

Type Description
string Name of the language pack
Return value

NULL

protected static function loadInflection($language)
{
	static::languageExists($language);

	if(file_exists(MAKO_APPLICATION . '/i18n/' . $language . '/inflection.php'))
	{
		static::$inflection[$language] = include(MAKO_APPLICATION . '/i18n/' . $language . '/inflection.php');
	}
	else
	{
		throw new RuntimeException(vsprintf("%s:(): The '%s' language pack does not contain any inflection rules.", array(__METHOD__, $language)));
	}
}

Toggle source

protected static loadStrings($language)


Loads the translation strings for the requested language.


Parameters

Type Description
string Name of the language pack
Return value

NULL

protected static function loadStrings($language)
{
	static::languageExists($language);

	static::$strings[$language] = false;

	if(Config::get('mako.lang_cache'))
	{
		static::$strings[$language] = Cache::instance()->read(MAKO_APPLICATION_ID . '_lang_' . $language);
	}

	if(static::$strings[$language] === false)
	{
		static::$strings[$language] = array();

		// Fetch strings from packages

		$files = glob(MAKO_PACKAGES . '/*/i18n/' . $language . '/strings/*.php', GLOB_NOSORT);

		foreach($files as $file)
		{
			static::$strings[$language] = array_merge(static::$strings[$language], include($file));
		}

		// Fetch strings from application

		$files = glob(MAKO_APPLICATION . '/i18n/' . $language . '/strings/*.php', GLOB_NOSORT);

		foreach($files as $file)
		{
			static::$strings[$language] = array_merge(static::$strings[$language], include($file));
		}

		if(Config::get('mako.lang_cache'))
		{
			Cache::instance()->write(MAKO_APPLICATION_ID . '_lang_' . $language, static::$strings[$language], 3600);
		}
	}
}