I18n.php

Jeremy Bush -, 01/22/2009 05:30 pm

Download (1.2 kB)

 
1
<?php
2
3
function __($string, $args = NULL)
4
{
5
        if (I18n::get_locale() != Config::get('locale.language'))
6
        {
7
                $string = I18n::get_text($string);
8
        }
9
10
        if ($args === NULL)
11
                return $string;
12
13
        return strtr($string, $args);
14
}
15
16
class I18n_Core
17
{
18
        protected static $locale = 'en_US';
19
        protected static $translations = array();
20
21
        public static function set_locale($locale)
22
        {
23
                // Reset the translations array
24
                self::$translations = array();
25
26
                self::$locale = $locale;
27
        }
28
29
        public static function get_locale()
30
        {
31
                return self::$locale;
32
        }
33
34
        public static function get_text($string)
35
        {
36
                $locale = explode('_', self::$locale);
37
                if ( ! self::$translations)
38
                {
39
                        if (self::$translations = Kohana::find_file('i18n', $locale[0]) AND isset(self::$translation[$string]))
40
                        {
41
                                // Merge the locale translations with the main language translation
42
                                if ($locale = Kohana::find_file('i18n', $locale[0].'/'.$locale[1]))
43
                                        self::$translations = array_merge(self::$translations, $locale);
44
45
                                return self::$translations[$string];
46
                        }
47
                        else
48
                                return $string;
49
                }
50
                else
51
                {
52
                        if (isset(self::$translations[$string]))
53
                                return self::$translations[$string];
54
                        else
55
                                return $string;
56
                }
57
        }
58
59
}