Internationalization class.
Protected constructor since this is a static class.
NULL
protected function __construct()
{
// Nothing here
}
Checks if a language pack exists and throws an exception if it doesn't.
| Type | Description |
|---|---|
| string | Name of the language pack |
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)));
}
}
Set and/or get the default language.
| Type | Description |
|---|---|
| string | (optional) Name of the language pack |
string
public static function language($language = null)
{
if($language !== null)
{
static::languageExists($language);
static::$language = $language;
}
return static::$language;
}
Returns a translated string of the current language. If no translation exists then the submitted string will be returned.
| 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 |
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);
}
Returns the plural form of a noun.
| Type | Description |
|---|---|
| string | Noun to pluralize |
| int | (optional) Number of "<noun>s" |
| string | (optional) Language rules to use for pluralization |
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']);
}
Loads the inflection rules for the requested language.
| Type | Description |
|---|---|
| string | Name of the language pack |
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)));
}
}
Loads the translation strings for the requested language.
| Type | Description |
|---|---|
| string | Name of the language pack |
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);
}
}
}