Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

best way to build a multi-language site with smarty
Goto page Previous  1, 2, 3 ... 6, 7, 8 ... 13, 14, 15  Next
 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Tips and Tricks
View previous topic :: View next topic  
Author Message
sagi
Smarty Regular


Joined: 30 Jul 2003
Posts: 43

PostPosted: Thu Aug 21, 2003 10:39 pm    Post subject: Reply with quote

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
View user's profile Send private message
hbilgen
Smarty n00b


Joined: 12 Sep 2003
Posts: 4

PostPosted: Fri Sep 12, 2003 4:12 pm    Post subject: RE: Reply with quote

Hi I'm using the same method as your (method posted in http://smarty.incutio.com/?page=SmartyMultilanguageSupport) but getting following error when I tried to run my code:
"Fatal error: Call to a member function on a non-object in D:\basisweb\www\aloe\modules\Reservation\include\SmartyML.class.php on line 30"

SmartyML.class.php file is as below:

<?php
require_once ("Smarty.class.php");

--code given in http://smarty.incutio.com/?page=SmartyMultilanguageSupport --

?>
Back to top
View user's profile Send private message
janfy
Smarty n00b


Joined: 26 Dec 2003
Posts: 1
Location: Toulouse, France

PostPosted: Fri Dec 26, 2003 9:59 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Alexey
Smarty Rookie


Joined: 10 Nov 2003
Posts: 11
Location: Marburg, Germany

PostPosted: Thu Jan 15, 2004 3:44 pm    Post subject: How to do it with a prefilter? Reply with quote

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
View user's profile Send private message
chickenbak
Smarty Rookie


Joined: 20 Jan 2004
Posts: 19

PostPosted: Wed Jan 21, 2004 5:14 pm    Post subject: Reply with quote

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
View user's profile Send private message
enlar
Smarty Rookie


Joined: 04 Feb 2004
Posts: 7

PostPosted: Wed Feb 04, 2004 11:32 am    Post subject: Suggestions for sagi's solution Reply with quote

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 Smile and make all automagic Smile

Regards
Back to top
View user's profile Send private message Visit poster's website
requite
Smarty n00b


Joined: 09 Feb 2004
Posts: 1

PostPosted: Mon Feb 09, 2004 10:24 pm    Post subject: ...I don't quite understand Reply with quote

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
View user's profile Send private message
enlar
Smarty Rookie


Joined: 04 Feb 2004
Posts: 7

PostPosted: Tue Feb 10, 2004 9:07 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
sagi
Smarty Regular


Joined: 30 Jul 2003
Posts: 43

PostPosted: Wed Feb 18, 2004 2:34 pm    Post subject: Reply with quote

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
View user's profile Send private message
sagi
Smarty Regular


Joined: 30 Jul 2003
Posts: 43

PostPosted: Wed Feb 18, 2004 6:19 pm    Post subject: Reply with quote

Here it is:
http://www.boom.org.il/smarty/gettext/smarty_gettext.zip

Includes a readme with basic information.
Back to top
View user's profile Send private message
enlar
Smarty Rookie


Joined: 04 Feb 2004
Posts: 7

PostPosted: Fri Feb 27, 2004 3:51 pm    Post subject: bug in tsmarty2c.php Reply with quote

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 Smile

Thanks a lot
Back to top
View user's profile Send private message Visit poster's website
sagi
Smarty Regular


Joined: 30 Jul 2003
Posts: 43

PostPosted: Fri Feb 27, 2004 4:18 pm    Post subject: Reply with quote

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
View user's profile Send private message
enlar
Smarty Rookie


Joined: 04 Feb 2004
Posts: 7

PostPosted: Fri Feb 27, 2004 4:28 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
enlar
Smarty Rookie


Joined: 04 Feb 2004
Posts: 7

PostPosted: Fri Feb 27, 2004 4:34 pm    Post subject: Fix for multi-line strings Reply with quote

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
View user's profile Send private message Visit poster's website
sagi
Smarty Regular


Joined: 30 Jul 2003
Posts: 43

PostPosted: Mon Mar 01, 2004 12:24 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Tips and Tricks All times are GMT
Goto page Previous  1, 2, 3 ... 6, 7, 8 ... 13, 14, 15  Next
Page 7 of 15

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP