View previous topic :: View next topic |
Author |
Message |
sagi Smarty Regular
Joined: 30 Jul 2003 Posts: 43
|
Posted: Thu Aug 21, 2003 10:39 pm Post subject: |
|
|
Yes ofcourse.
I finished writing the tool to grab the strings from the template:
http://beep.boom.org.il/~sagi/smarty/tsmarty2c.phps
It's a command line php script, copy it to a file, chmod +x and use it like:
./tsmarty2c <list_of_files>
It'll output the C-like gettext functions to stdout.
Then you can use it with any gettext program that supports C (all of them), such as xgettext or the poedit interface.
Mind that if you use plural strings (the script supports them and will write the ngettext functions) poedit will strip them because it doesn't support plural, so you might want to save them in another file.
I intend to add some more functions to this package and after I finish I'll try to do my best to release it with proper documentation, but it might take few weeks. |
|
Back to top |
|
hbilgen Smarty n00b
Joined: 12 Sep 2003 Posts: 4
|
|
Back to top |
|
janfy Smarty n00b
Joined: 26 Dec 2003 Posts: 1 Location: Toulouse, France
|
Posted: Fri Dec 26, 2003 9:59 pm Post subject: |
|
|
Hi
I've just see your post now.
I want to use this class to my own website and i've got the same error.
To fix the problem, you have to modify the following function :
Code: |
function smarty_prefilter_i18n($tpl_source, &$smarty)
{
$GLOBALS['_NG_LANGUAGE_'] = &$smarty->_tpl_vars["_NG_LANGUAGE_"];
if (!is_object($GLOBALS['_NG_LANGUAGE_'])) {
die("Error loading Multilanguage Support");
}
// Now replace the matched language strings with the entry in the file
$return = preg_replace_callback('/##(.+?)##/', '_compile_lang', $tpl_source);
unset($GLOBALS['_NG_LANGUAGE_']);
return $return;
}
|
For me, it's working fine. (I use PHP 4.3.2 on Win 2k plateform) |
|
Back to top |
|
Alexey Smarty Rookie
Joined: 10 Nov 2003 Posts: 11 Location: Marburg, Germany
|
Posted: Thu Jan 15, 2004 3:44 pm Post subject: How to do it with a prefilter? |
|
|
I have following case of implementing multi-language support. I have to generate the data for the items-per-page selector from my PHP like something like:
[php:1:1a350f31c5]<?php
$selector=Array (10=>"10", 20=>"20", 50=>"50", 79=>"All");
?>[/php:1:1a350f31c5]Then I can make my dropdown selector with Smarty's html_options:
Code: |
{html_options options=$selector selected=$perpage}
|
Note: $select length can vary according to the total count of items.
My aim is now to put the "All" text into different languages. I have no idea how to do it using regual pre-filters (like the most multilanguage ideas in this thread). I guess I have to set up my array like:
[php:1:1a350f31c5]<?php
$selector=Array (10=>"10", 20=>"20", 50=>"50", 79=>"@@All@@");
?>[/php:1:1a350f31c5]
and to use a post-filter to translate '@@All@@' basing on XML or database dictionary. Any other ideas? |
|
Back to top |
|
chickenbak Smarty Rookie
Joined: 20 Jan 2004 Posts: 19
|
Posted: Wed Jan 21, 2004 5:14 pm Post subject: |
|
|
Great info! I am actually quite interested in the database method, so that I can build a backend to update those entries easily. What is the best way to handle pulling that array of data and then using Smarty to implement it? Thanks ahead for any info! |
|
Back to top |
|
enlar Smarty Rookie
Joined: 04 Feb 2004 Posts: 7
|
Posted: Wed Feb 04, 2004 11:32 am Post subject: Suggestions for sagi's solution |
|
|
Hi,
I think that sagi's solution is the best, specially if we want to mix smarty template string translation and php script string translation in a single, gettext-way. Thanks a lot for your effort.
I'd suggest to change
http://beep.boom.org.il/~sagi/smarty/smarty_gettext.phps
so that it creates a derived smarty class that in its constructor just execs
$smarty->register_block('t', 'smarty_translate');
This would drop the global $smarty variable requirement and make all automagic
Regards |
|
Back to top |
|
requite Smarty n00b
Joined: 09 Feb 2004 Posts: 1
|
Posted: Mon Feb 09, 2004 10:24 pm Post subject: ...I don't quite understand |
|
|
Sorry, I am new to PHP and Smarty. May someone who understand sagi's method please lend me a hand?
So, my directory structure is like this
Quote: |
+--\locale
+--\en_US
+--\LC_MESSAGES
+--translation.mo
+--translation.po
+--translation.po.poedit
+--\es_ES
+--translation.mo
+--translation.po
+--translation.po.poedit
+--\ja_JP
+--translation.mo
+--translation.po
+--translation.po.poedit
+--\zh_TW
+--translation.mo
+--translation.po
+--translation.po.poedit
+--\configs
+--test.conf
+--\templates
+--footer.tpl
+--header.tpl
+--index.tpl
+--\templates_c
+--\Smarty
+--\libs
+--\core
+--\plugins
index.php
|
PHP include_path point to \Smarty\libs
First of all, where should I put smarty_gettext.phps into? Do I need to change the extension to .php? How to use this file?
Second, where should I put tsmarty2c.php into? Do I need to change the extension to .php? How to use this file?
For example, I include the line Code: | {t name="sagi" age=18}my name is %1 and i'm %2 years old{/t} | in my index.tpl.
Now... what is the next step to make sagi to be displayed in different languages? What variable should I put in to the .mo? |
|
Back to top |
|
enlar Smarty Rookie
Joined: 04 Feb 2004 Posts: 7
|
Posted: Tue Feb 10, 2004 9:07 am Post subject: |
|
|
Hi,
You should change both extensions to .php
Put tsmarty2c.php anywhere: yust remember its location when you have to extract strings to translate from .tpl files.
smarty_gettext.php should go in your includes directory. If you don't have one, put it in the root.
The system will allow you to translate
"my name is %1 and i'm %2 years old", NOT "sagi".
Regards |
|
Back to top |
|
sagi Smarty Regular
Joined: 30 Jul 2003 Posts: 43
|
Posted: Wed Feb 18, 2004 2:34 pm Post subject: |
|
|
Sorry guys, for some reason the forum software stopped notifying me on replies so I couldn't help.
I have a newer version of the script, I'll do my best to post it soon with basic doc.
Sagi |
|
Back to top |
|
sagi Smarty Regular
Joined: 30 Jul 2003 Posts: 43
|
|
Back to top |
|
enlar Smarty Rookie
Joined: 04 Feb 2004 Posts: 7
|
Posted: Fri Feb 27, 2004 3:51 pm Post subject: bug in tsmarty2c.php |
|
|
Hi sagi,
I think there's a bug because if there are more than 1 t-blocks in a line, only the last one will bet output. For example:
<tr><th>{t}Name{/t}</th><th>{t}Start date{/t}</th><th>{t}End date{/t}</th><th></th></tr>
Will only output gettext("End date");
I'm trying to fix it, but I don't know much regex
Thanks a lot |
|
Back to top |
|
sagi Smarty Regular
Joined: 30 Jul 2003 Posts: 43
|
Posted: Fri Feb 27, 2004 4:18 pm Post subject: |
|
|
Try changing the main regex (line 32) to:
[php:1:89fc0f18a8]preg_match_all("/{$ldq}\s*({$cmd})\s*([^{$rdq}]*){$rdq}([^{$ldq}]*){$ldq}\/\\1{$rdq}/", $content, $matches);[/php:1:89fc0f18a8] |
|
Back to top |
|
enlar Smarty Rookie
Joined: 04 Feb 2004 Posts: 7
|
Posted: Fri Feb 27, 2004 4:28 pm Post subject: |
|
|
Thanks a lot Sagi,
I've just sent an email to you with a similar solution. I think this one allows for multi-line strings to be translated too.
Regards |
|
Back to top |
|
enlar Smarty Rookie
Joined: 04 Feb 2004 Posts: 7
|
Posted: Fri Feb 27, 2004 4:34 pm Post subject: Fix for multi-line strings |
|
|
Hi,
It seems that a small fix is necessary to have multi-line strings correctly read by xgettext:
LINE 36:
print 'ngettext("'.str_replace("\n", "\\n", fq($matches[3][$i])).'","'.fq($match[1]).'",x);'."\n";
LINE 38:
print 'gettext("'.str_replace("\n", "\\n", fq($matches[3][$i])).'");'."\n";
Regards |
|
Back to top |
|
sagi Smarty Regular
Joined: 30 Jul 2003 Posts: 43
|
Posted: Mon Mar 01, 2004 12:24 pm Post subject: |
|
|
Version 0.9 of smarty_gettext released
Changes:
* tsmarty2c.php:
- added support for directories (originally by Uros Gruber <uros.gruber@vizija.si>)
- fixed bug that prevented more than 1 block per line (reported by Eneko Lacunza <enlar@euskal.org>)
- convert new line to \n in output string
* smarty_gettext.php:
- run nl2br() when escaping html
Download:
http://www.boom.org.il/smarty/gettext/
The package is now released under the LGPL. |
|
Back to top |
|
|