Smarty-gettext és una llibreria que permet utilitzar gettext en plantilles d'Smarty.
$ sudo apt-get install smarty-gettext
Els fitxers instal·lats són:
$ dpkg -L smarty-gettext /. /usr /usr/share /usr/share/php /usr/share/php/smarty /usr/share/php/smarty/libs /usr/share/php/smarty/libs/plugins /usr/share/php/smarty/libs/plugins/block.t.php /usr/share/doc /usr/share/doc/smarty-gettext /usr/share/doc/smarty-gettext/copyright /usr/share/doc/smarty-gettext/changelog.gz /usr/share/doc/smarty-gettext/README.gz /usr/share/doc/smarty-gettext/changelog.Debian.gz /usr/bin /usr/bin/tsmarty2c /usr/bin/tsmarty2c.php
Fixeu-vos que s'instal·lar com un plugin de Smarty (carpeta) /usr/share/php/smarty/libs/plugins).
Hi ha una script de PHP per executar per línia d'ordres anomenat tsmarty2c.php que forma part de la llibreria smarty-gettext. Suposant que heu instal·lat la llibreria smarty-gettext a la carpeta lib de la vostra aplicació:
./libs/smarty-gettext/tsmarty2c.php
A la mateixa carpeta teniu un README que explica com s'ha d'utilitzar:
./libs/smarty-gettext/README
Podeu crear un script a l'arrel de la vostra aplicació que s'encarregui d'actualitzar els locales:
$ sudo joe ./updateLocales.sh
#!/bin/bash #Smarty gettext php libs/smarty-gettext/tsmarty2c.php smarty/templates > smarty_messages.c #Extract text from smarty and php code xgettext --package-name=webfaltes --package-version 1.0 [email protected] -kT_ngettext:1,2 -kT_ -o messages_new.po smarty_messages.c *.php includes/*.php javascript/calendar.php #Establir par�metres de la plantilla: sed -i s/SOME\ DESCRIPTIVE\ TITLE/Webfaltes\ po\ file/ messages_new.po sed -i s/FIRST\ AUTHOR/Sergi\ Tur/ messages_new.po sed -i s/YEAR/2009/ messages_new.po sed -i s/THE\ PACKAGE\'S\ COPYRIGHT\ HOLDER/Sergi\ Tur\ Badenas/ messages_new.po sed -i s/PACKAGE/webfaltes/ messages_new.po sed -i s/charset=CHARSET/charset=UTF-8/ messages_new.po sed -i s/FULL\ NAME/Sergi\ Tur/ messages_new.po sed -i s/[email protected]/[email protected]/ messages_new.po mv messages_new.po messages_new.pot #Message init is not useful here #msginit -o messages_new.po CATALAN_PO_FILE=locales/ca_ES/LC_MESSAGES/messages.po SPANISH_PO_FILE=locales/es_ES/LC_MESSAGES/messages.po ENGLISH_PO_FILE=locales/en_US/LC_MESSAGES/messages.po #Catalan if [ -f $CATALAN_PO_FILE ]; then echo "Updating $CATALAN_PO_FILE..." msgmerge -v $CATALAN_PO_FILE messages_new.pot -U else echo "Creating new file $CATALAN_PO_FILE..." cp messages_new.pot $CATALAN_PO_FILE fi #Spanish if [ -f $SPANISH_PO_FILE ]; then echo "Updating $SPANISH_PO_FILE..." msgmerge -v $SPANISH_PO_FILE messages_new.pot -U else echo "Creating new file $SPANISH_PO_FILE..." cp messages_new.pot $SPANISH_PO_FILE fi #English if [ -f $ENGLISH_PO_FILE ]; then echo "Updating $ENGLISH_PO_FILE..." msgmerge -v $ENGLISH_PO_FILE messages_new.pot -U else echo "Creating new file $ENGLISH_PO_FILE..." cp messages_new.pot $ENGLISH_PO_FILE fi echo "Now you can translate po files..." #Compile #msgfmt -v -c --statistics -o locales/ca_ES/LC_MESSAGES/messages.mo locales/ca_ES/LC_MESSAGES/messages.po #msgfmt -v -c --statistics -o locales/es_ES/LC_MESSAGES/messages.mo locales/es_ES/LC_MESSAGES/messages.po #msgfmt -v -c --statistics -o locales/en_US/LC_MESSAGES/messages.mo locales/en_US/LC_MESSAGES/messages.po
Si us dona l'error:
$ sudo tail -f /var/log/apache2/error.log [Sat Jul 28 12:29:12 2012] [error] [client 109.69.15.146] PHP Fatal error: Uncaught exception 'SmartyCompilerException' with message 'Syntax Error in template "/usr/share/webfaltes/smarty/templates/identifica.tpl" on line 45 "<fieldset><legend>{t}User autentication{/t}</legend>" unknown tag "t"' in /usr/share/php/smarty3/sysplugins/smarty_internal_templatecompilerbase.php:656\nStack trace:\n#0 /usr/share/php/smarty3/sysplugins /smarty_internal_templatecompilerbase.php(441): Smarty_Internal_TemplateCompilerBase->trigger_template_error('unknown tag "t"', 45)\n#1 /usr/share/php/smarty3/sysplugins/smarty_internal_templateparser.php(2397): Smarty_Internal_TemplateCompilerBase->compileTag('t', Array)\n#2 /usr/share/php/smarty3/sysplugins/smarty_internal_templateparser.php(3095): Smarty_Internal_Templateparser->yy_r37()\n#3 /usr/share/php/smarty3/sysplugins /smarty_internal_templateparser.php(3195): Smarty_Internal_Templateparser->yy_reduce(37)\n#4 /usr/share/php/smarty3/sysplugins/smarty_internal_smartytemplatecompiler.php(105): Smarty_Internal_Templateparser->doParse(10, 'User in /usr/share/php/smarty3/sysplugins/smarty_internal_templatecompilerbase.php on line 656
El problema és que el paquet instal·la el plugin per a la versio 2 d'smarty. La sol·lució és:
$ cd /usr/share/php/smarty3/plugins $ sudo ln -s /usr/share/php/smarty/libs/plugins/block.t.php .