2009-03-01 02:38:32 +02:00
|
|
|
<?php defined('SYSPATH') or die('No direct script access.');
|
|
|
|
/**
|
|
|
|
* Internationalization (i18n) class.
|
|
|
|
*
|
2009-03-21 00:01:47 +02:00
|
|
|
* @package Kohana
|
2009-03-01 02:38:32 +02:00
|
|
|
* @author Kohana Team
|
|
|
|
* @copyright (c) 2008-2009 Kohana Team
|
|
|
|
* @license http://kohanaphp.com/license.html
|
|
|
|
*/
|
|
|
|
class i18n_Core {
|
|
|
|
|
|
|
|
// The default language of all messages
|
2009-05-31 01:57:20 +03:00
|
|
|
public static $default_lang = 'en-US';
|
2009-03-01 02:38:32 +02:00
|
|
|
|
|
|
|
// The current language
|
2009-05-31 01:57:20 +03:00
|
|
|
public static $lang = 'en-US';
|
2009-03-01 02:38:32 +02:00
|
|
|
|
|
|
|
// Cache of loaded languages
|
|
|
|
protected static $_cache = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns translation of a string. If no translation exists, the original
|
|
|
|
* string will be returned.
|
|
|
|
*
|
|
|
|
* @param string text to translate
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function get($string)
|
|
|
|
{
|
|
|
|
// Load the translation table
|
|
|
|
$table = i18n::load(i18n::$lang);
|
|
|
|
|
|
|
|
// Return the translated string if it exists
|
|
|
|
return isset($table[$string]) ? $table[$string] : $string;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads the translation table for a given language.
|
|
|
|
*
|
|
|
|
* @param string language to load
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected static function load($lang)
|
|
|
|
{
|
|
|
|
if ( ! isset(i18n::$_cache[$lang]))
|
|
|
|
{
|
|
|
|
// Separate the language and locale
|
2009-05-31 01:57:20 +03:00
|
|
|
list ($language, $locale) = explode('-', strtolower($lang), 2);
|
2009-03-01 02:38:32 +02:00
|
|
|
|
|
|
|
// Start a new translation table
|
|
|
|
$table = array();
|
|
|
|
|
|
|
|
if ($files = Kohana::find_file('i18n', $language))
|
|
|
|
{
|
|
|
|
foreach ($files as $file)
|
|
|
|
{
|
|
|
|
// Load the strings that are in this file
|
|
|
|
$strings = Kohana::load($file);
|
|
|
|
|
|
|
|
// Merge the language strings into the translation table
|
|
|
|
$table = array_merge($table, $strings);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($files = Kohana::find_file('i18n', $language.'/'.$locale))
|
|
|
|
{
|
|
|
|
foreach ($files as $file)
|
|
|
|
{
|
|
|
|
// Load the strings that are in this file
|
|
|
|
$strings = Kohana::load($file);
|
|
|
|
|
|
|
|
// Merge the locale strings into the translation table
|
|
|
|
$table = array_merge($table, $strings);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cache the translation table locally
|
|
|
|
i18n::$_cache[$lang] = $table;
|
|
|
|
}
|
|
|
|
|
|
|
|
return i18n::$_cache[$lang];
|
|
|
|
}
|
|
|
|
|
2009-03-02 02:06:44 +02:00
|
|
|
} // End i18n
|