Moje xx podejście do tematu, lubie mieć oddzielony kod od "tekstów". Wcześniej operowałem na plikach php lub xml, ale niedawno natknąłem się na YAML'a, spodobala mi sie jego składnia i prostota ....
Wymagania:
- kohanaPHP
-
http://code.google.com/p/spyc/klase nalezy umiescic w /(app)/helpers
spyc (spyc.php) najlepiej w /(app)/libraries
<?php
class lang {
/**
* Locale name
* @var string
*/
static protected
$locale = null; /**
* Internal cache
* @var array
*/
/**
* common args
* @var array
*/
/**
* Set common arg
*
* @param $key string
* @param $value string
*/
static public function setArg
($key, $value) {
return self::$args[$key] = $value;
}
/**
* Get common arg
*
* @param $key string
* @return string
*/
static public function getArg
($key) {
return @self::$args[$key];
}
/**
* Get element or whole array
*
* @param $key string
* @param $args array
* @return mixed
*/
{
@list
($group, $name) = explode('.', $key, 2
);
// Get the language data list
$lang = self::load( $group );
if( !isset($name) || empty($name) ) return self::rewriteArgs( $lang , $args );
$out = $lang;
foreach( $name as $n )
{
if( !isset($out[ $n ] ) ) return null; $out = $out[ $n ];
}
return self::rewriteArgs($out, $args);
}
/**
* Rewrite arguments in string or array
*
* @param $subject mixed
* @param $args array
* @return mixed
*/
static public function rewriteArgs
($subject, $args) {
self::$curr_args = $args + self::$args;
$out = self::__array_map_callback($subject);
self::$curr_args = array();
return $out;
}
/**
*
* @param $e mixed
* @return mixed
*/
static public function __array_map_callback
($e) {
for($i = 0 ; $i < $matches ; $i++)
{
$e = @str_replace($out[0
][$i], self::$curr_args[ $out[1
][$i] ], $e); }
return $e;
}
/**
* Get current locale name
*
* @return string
*/
static public function getLocale
() {
if( self::$locale == null ) self::$locale = Kohana::config('locale.language.0');
return self::$locale;
}
/**
* Set current locale name
*
* @param $locale string
*/
{
self::$locale = $locale;
}
/**
* Load language array
*
* @param $group string
* @return array
*/
static protected
function load
($group) {
$locale = self::getLocale();
if( isset( self::$cache[$locale][$group] ) ) return self::$cache[$locale][$group];
$cache = Cache::instance('i18n');
if( Kohana::config('locale.cache', false, false) == true )
$lang = $cache->get( $locale.'-'.$group );
else $lang = null;
if( $lang == null )
{
$files = Kohana::find_file('i18n', $locale.'/'.$group, false, 'yaml');
if( $files )
{
foreach( $files as $file )
{
$lang = @Spyc::YAMLLoad($file) + $lang;
}
}
if( Kohana::config('locale.cache', false, false) == true )
$cache->set( $locale.'-'.$group, $lang);
}
return self::$cache[$locale][$group] = $lang;
}
}
?>
konfiguracia:
<?php
// config/cache.php
(
'driver' => 'file',
'params' => APPPATH.'cache'.DIRECTORY_SEPARATOR.'i18n',
'lifetime' => 0,
'requests' => 0
);
?>
<?php
// config/locale.php
$config['cache'] = false;
?>
przykladowy plik yaml:
Kod
test: |
ha wtf hm :> :P $[name]
hm ?
hm:
aa: aaa $[tak_tak]
bb: aa?
cc: asd $[name]
usage:
<?php
lang
::setArg('tak_tak', '
');echo lang
::getArg('tak_tak'); echo lang
::get('test.hm.bb'); echo lang
::get('test.test', array('name'=>'crashu')); ?>
output:
Kod
:P
array(3) {
["aa"]=>
string(6) "aaa :P"
["bb"]=>
string(3) "aa?"
["cc"]=>
string(18) "asd crashu"
}
aa?
ha wtf hm :> :P crashu
hm ?
Ten post edytował crashu 9.07.2009, 21:09:02