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

  1. <?php
  2.  
  3. class lang {
  4.    
  5.    /**
  6.      * Locale name
  7.      * @var string
  8.      */
  9.    static protected $locale = null;
  10.    /**
  11.      * Internal cache
  12.      * @var array
  13.      */
  14.    static protected $cache = array();
  15.    
  16.    static protected $curr_args = array();
  17.    
  18.    /**
  19.      * common args
  20.      * @var array
  21.      */
  22.    static protected $args = array();
  23.    
  24.    /**
  25.      * Set common arg
  26.      *
  27.      * @param $key string
  28.      * @param $value string
  29.      */
  30.    static public function setArg($key, $value)
  31.    {
  32.        return self::$args[$key] = $value;
  33.    }
  34.    
  35.    /**
  36.      * Get common arg
  37.      *
  38.      * @param $key string
  39.      * @return string
  40.      */
  41.    static public function getArg($key)
  42.    {
  43.        return @self::$args[$key];
  44.    }
  45.    
  46.    /**
  47.      * Get element or whole array
  48.      *
  49.      * @param $key string
  50.      * @param $args array
  51.      * @return mixed
  52.      */
  53.    static public function get($key ,$args = array())
  54.    {
  55.        if( !isset($key) || empty($key) ) return null;
  56.        
  57.        reset($args);
  58.        
  59.        @list($group, $name) = explode('.', $key, 2);
  60.        
  61.        // Get the language data list
  62.        $lang = self::load( $group );
  63.        
  64.        if( !isset($name) || empty($name) ) return self::rewriteArgs( $lang , $args );
  65.        
  66.        $name = explode('.', $name);
  67.        
  68.        $out = $lang;
  69.  
  70.        foreach( $name as $n )
  71.        {
  72.            if( !isset($out[ $n ] ) ) return null;
  73.            $out = $out[ $n ];
  74.        }
  75.        
  76.        return self::rewriteArgs($out, $args);
  77.    }
  78.    
  79.    /**
  80.      * Rewrite arguments in string or array
  81.      *
  82.      * @param $subject mixed
  83.      * @param $args array
  84.      * @return mixed
  85.      */
  86.    static public function rewriteArgs($subject, $args)
  87.    {        
  88.        self::$curr_args = $args + self::$args;
  89.        
  90.        $out = self::__array_map_callback($subject);
  91.        
  92.        self::$curr_args = array();
  93.        
  94.        return $out;
  95.    }
  96.    
  97.    /**
  98.      *
  99.      * @param $e mixed
  100.      * @return mixed
  101.      */
  102.    static public function __array_map_callback($e)
  103.    {
  104.        if( is_array($e) ) return array_map('lang::__array_map_callback', $e);
  105.        
  106.        $matches =  preg_match_all('/$[(w+)]/i',$e, $out);
  107.  
  108.        for($i = 0 ; $i < $matches ; $i++)
  109.        {
  110.            $e = @str_replace($out[0][$i], self::$curr_args[ $out[1][$i] ], $e);
  111.        }
  112.        
  113.        return $e;
  114.    }
  115.    
  116.    /**
  117.      * Get current locale name
  118.      *
  119.      * @return string
  120.      */
  121.    static public function getLocale()
  122.    {
  123.        if( self::$locale == null ) self::$locale = Kohana::config('locale.language.0');
  124.        return self::$locale;
  125.    }
  126.    
  127.    /**
  128.      * Set current locale name
  129.      *
  130.      * @param $locale string
  131.      */
  132.    static public function setLocale($locale)
  133.    {
  134.        self::$locale = $locale;
  135.    }
  136.  
  137.    /**
  138.      * Load language array
  139.      *
  140.      * @param $group string
  141.      * @return array
  142.      */
  143.    static protected function load($group)
  144.    {
  145.        $locale = self::getLocale();
  146.        
  147.        $group = strtolower($group);
  148.        
  149.        if( isset( self::$cache[$locale][$group] ) ) return self::$cache[$locale][$group];
  150.        
  151.        $cache = Cache::instance('i18n');
  152.        
  153.        if( Kohana::config('locale.cache', false, false) == true )
  154.            $lang = $cache->get( $locale.'-'.$group );
  155.        else $lang = null;
  156.  
  157.        if( $lang == null )
  158.        {
  159.            $files = Kohana::find_file('i18n', $locale.'/'.$group, false, 'yaml');
  160.    
  161.            $lang = array();
  162.    
  163.            if( $files )
  164.            {
  165.                foreach( $files as $file )
  166.                {
  167.                    $lang = @Spyc::YAMLLoad($file) + $lang;
  168.                }
  169.            }
  170.            
  171.            if( Kohana::config('locale.cache', false, false) == true )
  172.                $cache->set( $locale.'-'.$group, $lang);
  173.        }
  174.        
  175.        return self::$cache[$locale][$group] = $lang;
  176.    }
  177. }
  178.  
  179. ?>


konfiguracia:
  1. <?php
  2. // config/cache.php
  3. $config['i18n'] = array
  4. (
  5.    'driver'   => 'file',
  6.    'params'   => APPPATH.'cache'.DIRECTORY_SEPARATOR.'i18n',
  7.    'lifetime' => 0,
  8.    'requests' => 0
  9. );
  10. ?>

  1. <?php
  2. // config/locale.php
  3. $config['cache'] = false;
  4. ?>


przykladowy plik yaml:
Kod
test: |
     ha wtf hm :> :P $[name]
     hm ?
    
hm:
    aa: aaa $[tak_tak]
    bb: aa?
    cc: asd               $[name]


usage:
  1. <?php
  2. lang::setArg('tak_tak', 'tongue.gif');
  3. echo lang::getArg('tak_tak');
  4. var_dump( lang::get('test.hm', array('name'=>'crashu')));
  5. echo lang::get('test.hm.bb');
  6. echo lang::get('test.test', array('name'=>'crashu'));
  7. ?>


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 ?