1 <?
2 /*
3 * Smarty plugin
4 * -------------------------------------------------------------
5 * File: block.t.php
6 * Type: block function
7 * Name: t (short for translate through gettext)
8 * Purpose: return a translated string and, if
9 * development = true, check for translations and store in
10 * appropriate file. Use babelfish to generate a translation if
11 * use_babelfish = true
12 * Usage: {t}Some text which could be translated if you wish{/t}
13 *
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 * -------------------------------------------------------------
28 *
29 * @link http://www.gnu.org/copyleft/lesser.html
30 * @copyright 2003 Tom Anderson
31 * @author Tom Anderson <toma@etree.org>
32 * @param string $language override global t_language variable
33 * @version: 1.3
34 */
35 function smarty_block_t($params, $content, &$this)
36 {
37 if (!$content) return $content;
38 // Setup local development variables.
39 // If $development = false then use_babelfish is ignored.
40 $development = false;
41 // If use_babelfish is false, placeholders will be put in .po files
42 // but no translation will be attempted using babelfish.
43 $use_babelfish = false;
45 global $t_language, $t_language_from, $t_msgfmt_location, $t_gettext_message_dir;
47 // Check for new language
48 if (isset($params['language']) && $params['language']) {
49 $t_language = $params['language'];
50 putenv ("LC_ALL={$GLOBALS[t_language]}");
51 }
53 // If under development, check for necessary files
54 if ($development) {
55 // Translate string once for every language supported
56 foreach ($GLOBALS['t_language_all'] as $t_language) {
57 // Check for language directory; create if necessary
58 if (!is_dir($t_gettext_message_dir . $t_language)) {
59 mkdir ($t_gettext_message_dir . $t_language);
60 mkdir ("$t_gettext_message_dir$t_language/LC_MESSAGES");
61 }
62 // Check for .po file
63 $po = "$t_gettext_message_dir$t_language/LC_MESSAGES/messages.po";
64 if (!file_exists($po)) _create_po_file($po);
66 // Load the .po file and check for the current translation string
67 $text = file($po);
68 $cst_content = addcslashes(trim($content), '\\"');
69 if (!_in_array_trim('msgid "' . $cst_content . '"', $text)) {
70 // String is not in translation file; add it
71 // Get translated string from babelfish if set to true
72 if ($use_babelfish) {
73 $translated = '';
74 $url ="http://babelfish.altavista.com/babelfish/tr?lp={$t_language_from}_{$t_language}&intl=1&tt=urltext&doit=done&urltext=" . urlencode(htmlspecialchars($content, ENT_NOQUOTES, 'UTF-8'));
75 $fp = fopen($url, 'r');
76 // Find the translated string
77 //foreach ($babel as $line) {
78 while ($line = fgets($fp)) {
79 if (strstr($line, "lang=$t_language")) {
80 // multibytes do not work yet with
81 // html_entity_decode but that is ok,
82 // since altavista does not seem to send entities.
83 $translated = html_entity_decode(trim(addcslashes(strip_tags($line), '\\"')), ENT_NOQUOTES);
85 break;
86 }
87 }
88 fclose($fp);
89 unset($url);
90 }
92 // Add string to .po
93 // mark translation as babelfish, so we know
94 // that we need to look after it.
95 if ($use_babelfish) {
96 error_log("#: babelfish\n", 3, $po);
97 }
98 error_log("#: " . $this->_plugins['block']['t'][1] . "\nmsgid \"" . $cst_content . "\"\nmsgstr \"$translated\"\n\n", 3, $po);
100 // Recompile translation file
101 `$t_msgfmt_location -o $t_gettext_message_dir$t_language/LC_MESSAGES/messages.mo $po`;
102 }
103 }
104 }
105 // Do the actual translation through gettext.
106 bindtextdomain ('messages', $t_gettext_message_dir);
107 return gettext(trim($content));
108 }
110 // Helper functions
111 function _in_array_trim($needle, $haystack) {
112 $needle = trim($needle);
113 foreach ($haystack as $ck) {
114 if ($needle == trim($ck)) return true;
115 }
116 return false;
117 }
119 function _create_po_file($po) {
120 touch ($po);
121 // put header info for .po file
122 $year = date('Y');
123 $date = date('r');
124 $body = <<<EOD
125 # Smarty gettext generated translation file
126 # Author toma@etree.org
127 # Version 1.0 - 4/29/2003
128 #, fuzzy
129 msgid ""
130 msgstr ""
131 "Project-Id-Version: Smarty Gettext 1.0\\n"
132 "POT-Creation-Date: $date\\n"
133 "PO-Revision-Date: $date\\n"
134 "Last-Translator: Smarty Generated\\n"
135 "Language-Team: http://babelfish.altavista.com/babelfish/\\n"
136 "MIME-Version: 1.0n\\n"
137 "Content-Type: text/plain; charset=utf-8\\n"
138 "Content-Transfer-Encoding: 8bit\\n"
139 \n
140 EOD;
141 error_log($body, 3, $po);
143 }
145 /* vim: set expandtab: */
146 ?>