1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12: class Languages
13: {
14: 15: 16:
17: private static $strings = NULL;
18:
19:
20: 21: 22: 23: 24: 25:
26: public static function init($arguments)
27: {
28: if (isset($arguments['language']) === TRUE) {
29: define('LANGUAGE', $arguments['language']);
30: unset($arguments['language']);
31:
32: self::get_language_strings();
33:
34: } else {
35: define('LANGUAGE', NULL);
36: }
37:
38: Onion::$controller->view->register_macro('% ', array('Languages', 'translate'));
39: }
40:
41:
42: 43: 44: 45: 46: 47: 48:
49: public static function translate()
50: {
51: $arguments = func_get_args();
52:
53: $string = $arguments[0];
54:
55: if (self::$strings === NULL) {
56: self::get_language_strings();
57: }
58:
59: if (isset(self::$strings[$string]) === TRUE) {
60: $arguments[0] = self::$strings[$string];
61: }
62:
63: return call_user_func_array('sprintf', $arguments);
64: }
65:
66:
67: 68: 69: 70: 71:
72: private static function get_language_strings()
73: {
74: $content = new DirectoryIterator(APP_DIR . '/strings/' . LANGUAGE);
75:
76: self::$strings = array();
77:
78: foreach ($content as $file) {
79: if ($file->isFile() === FALSE) {
80: continue;
81: }
82:
83: $strings = file_get_contents($file->getPathname());
84:
85: preg_match_all('/^"?(.*?)"?\ *=\ *"?(.*?)"?$/m', $strings, $matches);
86: foreach ($matches[1] as $key => $string) {
87: self::$strings[$string] = $matches[2][$key];
88: }
89: }
90: }
91: }
92:
93:
94: 95: 96:
97: if (function_exists('__') === FALSE) {
98: function __()
99: {
100: $arguments = func_get_args();
101: return call_user_func_array(array('Languages', 'translate'), $arguments);
102: }
103: }
104: