1: <?php
2: /**
3: * This file is part of the PHPLucidFrame library.
4: * Core utility for internationalization
5: *
6: * @package PHPLucidFrame\Core
7: * @since PHPLucidFrame v 1.0.0
8: * @copyright Copyright (c), PHPLucidFrame.
9: * @link http://phplucidframe.com
10: * @license http://www.opensource.org/licenses/mit-license.php MIT License
11: *
12: * This source file is subject to the MIT license that is bundled
13: * with this source code in the file LICENSE
14: */
15:
16: /**
17: * Translation helper
18: * It returns a translated string if it is found; Otherwise, the given string itself
19: *
20: * @param string $str The string being translated
21: * @param mixed $args The multiple arguments to be substituted in the string
22: *
23: * @return string The translated string
24: */
25: function _t($str/*[, mixed $args [, mixed $... ]]*/)
26: {
27: global $lc_lang;
28: global $lc_translation;
29: global $lc_translationEnabled;
30:
31: $args = func_get_args();
32: $str = array_shift($args);
33: $str = trim($str);
34:
35: if ($lc_translationEnabled == false) {
36: return (count($args)) ? vsprintf($str, $args) : $str;
37: }
38:
39: $po = session_get('po');
40: if (!is_array($po)) {
41: $po = array();
42: }
43: $po[$str] = '';
44:
45: if (isset($lc_translation[$lc_lang])) {
46: # check with lowercase
47: $lowerStr = strtolower($str);
48: if (isset($lc_translation[$lc_lang][$lowerStr]) && !empty($lc_translation[$lc_lang][$lowerStr])) {
49: $translated = $lc_translation[$lc_lang][$lowerStr];
50: $str = (is_array($translated)) ? $translated[0] : $translated;
51: }
52: }
53:
54: if (isset($translated)) {
55: $po[$str] = $translated;
56: }
57: return (count($args)) ? vsprintf($str, $args) : $str;
58: }
59: /**
60: * Get translation contents from the content file located in i18n/ctn/[lang]/*.[lang]
61: * Example, i18n/ctn/en/about.en
62: *
63: * @param string $fileName The file name
64: * @param mixed $args The array of arguments to be substituted in the string
65: * @return string The translation content
66: */
67: function _tc($fileName, $args = array())
68: {
69: global $lc_defaultLang;
70: global $lc_lang;
71:
72: $langs = array($lc_lang, $lc_defaultLang);
73: foreach ($langs as $lng) {
74: $file = I18N . 'ctn/' . $lng . '/' . $fileName . '.' . $lng;
75: if (is_file($file) && file_exists($file)) {
76: $content = file_get_contents($file);
77: if (count($args)) {
78: foreach ($args as $key => $value) {
79: $regex = '/'.$key.'\b/i';
80: $content = preg_replace($regex, $value, $content);
81: }
82: }
83: return $content;
84: }
85: }
86: return '';
87: }
88: /**
89: * @internal
90: * @ignore
91: *
92: * Loads the text .po file and returns array of translations
93: * @return mixed Array of translations on success or FALSE on failure
94: */
95: function __i18n_load()
96: {
97: global $lc_lang;
98: global $lc_translation;
99: global $lc_translationEnabled;
100:
101: if ($lc_translationEnabled == false) {
102: return false;
103: }
104:
105: $filename = I18N . $lc_lang.'.po';
106: if (!file_exists($filename)) {
107: return false;
108: }
109:
110: # Open the po file
111: if (!$file = fopen($filename, 'r')) {
112: session_delete("i18n.{$lc_lang}");
113: return false;
114: }
115:
116: # if the respective po file is already parsed
117: if ($translations = session_get("i18n.{$lc_lang}")) {
118: return $lc_translation[$lc_lang] = $translations;
119: }
120:
121: # parse the file
122: session_delete("i18n.{$lc_lang}");
123:
124: /**
125: * Thanks to CakePHP for the po file parsing logic in the do...while loop
126: * @package Cake.I18n
127: * @version 1.2.0.4116
128: * @license http://www.opensource.org/licenses/mit-license.php MIT License
129: */
130: $type = 0;
131: $translations = array();
132: $translationKey = '';
133: $plural = 0;
134: $header = '';
135:
136: do {
137: $line = trim(fgets($file));
138: if ($line === '' || $line[0] === '#') {
139: continue;
140: }
141: if (preg_match("/msgid[[:space:]]+\"(.+)\"$/i", $line, $regs)) {
142: $type = 1;
143: $translationKey = strtolower(stripcslashes($regs[1]));
144: } elseif (preg_match("/msgid[[:space:]]+\"\"$/i", $line, $regs)) {
145: $type = 2;
146: $translationKey = '';
147: } elseif (preg_match("/^\"(.*)\"$/i", $line, $regs) && ($type == 1 || $type == 2 || $type == 3)) {
148: $type = 3;
149: $translationKey .= strtolower(stripcslashes($regs[1]));
150: } elseif (preg_match("/msgstr[[:space:]]+\"(.+)\"$/i", $line, $regs) && ($type == 1 || $type == 3) && $translationKey) {
151: $translations[$translationKey] = stripcslashes($regs[1]);
152: $type = 4;
153: } elseif (preg_match("/msgstr[[:space:]]+\"\"$/i", $line, $regs) && ($type == 1 || $type == 3) && $translationKey) {
154: $type = 4;
155: $translations[$translationKey] = '';
156: } elseif (preg_match("/^\"(.*)\"$/i", $line, $regs) && $type == 4 && $translationKey) {
157: $translations[$translationKey] .= stripcslashes($regs[1]);
158: } elseif (preg_match("/msgid_plural[[:space:]]+\".*\"$/i", $line, $regs)) {
159: $type = 6;
160: } elseif (preg_match("/^\"(.*)\"$/i", $line, $regs) && $type == 6 && $translationKey) {
161: $type = 6;
162: } elseif (preg_match("/msgstr\[(\d+)\][[:space:]]+\"(.+)\"$/i", $line, $regs) && ($type == 6 || $type == 7) && $translationKey) {
163: $plural = $regs[1];
164: $translations[$translationKey][$plural] = stripcslashes($regs[2]);
165: $type = 7;
166: } elseif (preg_match("/msgstr\[(\d+)\][[:space:]]+\"\"$/i", $line, $regs) && ($type == 6 || $type == 7) && $translationKey) {
167: $plural = $regs[1];
168: $translations[$translationKey][$plural] = '';
169: $type = 7;
170: } elseif (preg_match("/^\"(.*)\"$/i", $line, $regs) && $type == 7 && $translationKey) {
171: $translations[$translationKey][$plural] .= stripcslashes($regs[1]);
172: } elseif (preg_match("/msgstr[[:space:]]+\"(.+)\"$/i", $line, $regs) && $type == 2 && !$translationKey) {
173: $header .= stripcslashes($regs[1]);
174: $type = 5;
175: } elseif (preg_match("/msgstr[[:space:]]+\"\"$/i", $line, $regs) && !$translationKey) {
176: $header = '';
177: $type = 5;
178: } elseif (preg_match("/^\"(.*)\"$/i", $line, $regs) && $type == 5) {
179: $header .= stripcslashes($regs[1]);
180: } else {
181: unset($translations[$translationKey]);
182: $type = 0;
183: $translationKey = '';
184: $plural = 0;
185: }
186: } while (!feof($file));
187: fclose($file);
188:
189: $merge[''] = $header;
190: $lc_translation[$lc_lang] = array_merge($merge, $translations);
191: # Store the array of translations in Session
192: session_set("i18n.{$lc_lang}", $lc_translation[$lc_lang]);
193:
194: return $lc_translation;
195: }
196: