Thread: [Phplib-users] Templates and i18n...
Brought to you by:
nhruby,
richardarcher
From: Aric C. <gre...@pe...> - 2002-12-02 22:44:56
|
I've been adapting my code to use a getText() function to provide = translated strings. Using templates, I then setvar() all the strings = from getText() and put appropriate {} tags into the templates. It occured to me that this could be much simpler. Why not have the = template class do it for you? You could "register" your custom = getText() function (subclass the template object). Then when ever the = template parser encounters a translatable string it calls the function. = A translatable string could be identified in the template like so: = {"this is a string"}. This would reduce the amount of coding needed, = and would make it easier for the template designer. I think it would be pretty easy to add. Any thoughts? |
From: Richard A. <rh...@ju...> - 2002-12-03 00:53:46
|
At 14:44 -0800 2/12/02, Aric Caley wrote: >I've been adapting my code to use a getText() function to provide >translated strings. Hi Aric, What is the difference between: >I then ... put appropriate {} tags into the templates. and >A translatable string could be identified in the >template like so: {"this is a string"}. In both cases you are adding {} tags to the template file, which seems to me like the same amount of work. ...R. |
From: Aric C. <gre...@pe...> - 2002-12-03 01:12:26
|
> Hi Aric, > > What is the difference between: > > >I then ... put appropriate {} tags into the templates. > > and > > >A translatable string could be identified in the > >template like so: {"this is a string"}. > > > In both cases you are adding {} tags to the template file, > which seems to me like the same amount of work. Well, because right now, the template might look like this: <h1>{TITLELABEL}</h2> And the code would have this: $template->set_var(array("TITLELABEL" => getText("This is the page title"))); Now if there's a lot of text like that, your set_var() gets big. With the other method, it goes something like this: <h1>{"This is the page title"}</h2> Which for the template designer is a little nicer -- he sees the actual text at least. The template designer might also do the language translation file, so he could add text in both places and not have to bother the code guy to add more set_vars(). The code would then be something like this: class $mytemplate extends $template { function getText($str) { /* whatever code here you need to load a language file and index $str to get a new string */ return $str; // defaults to same } } And then that's it for your application. No more set_var() for all those strings, because the template class calls the getText method whenever it sees a {""}. Does that make more sense? |
From: Richard A. <rh...@ju...> - 2002-12-03 03:01:08
|
At 17:11 -0800 2/12/02, Aric Caley wrote: >Well, because right now, the template might look like this: > ><h1>{TITLELABEL}</h2> > >And the code would have this: > >$template->set_var(array("TITLELABEL" => getText("This is the page >title"))); OK, I can see where you are going with this. I would approach this slightly differently. I would build a database with the translations in it. Columns like: lang char(2), # ISO country code varname char(16), # contains the template varname trans text, # the translated text with indexes on lang and varname The template file would contain lots of tags like: <h1>{TITLELABEL}</h1> You can extract all the template varnames from the template file with a preg_match_all(). Then loop through the results generating a massive SQL query which pulls all the text translations required for that page out of the database with one query. Then loop through the SQL result set and call $t->set_var($db->f('varname'), $db->f('trans')) for each record. Your PHP script wouldn't even need to know the template varnames :) ...R. |
From: Aric C. <gre...@pe...> - 2002-12-03 18:39:24
|
> OK, I can see where you are going with this. I would approach this > slightly differently. > > I would build a database with the translations in it. Columns like: > lang char(2), # ISO country code > varname char(16), # contains the template varname > trans text, # the translated text > with indexes on lang and varname > > The template file would contain lots of tags like: > <h1>{TITLELABEL}</h1> > > You can extract all the template varnames from the template file with > a preg_match_all(). Then loop through the results generating a massive > SQL query which pulls all the text translations required for that page > out of the database with one query. Then loop through the SQL result > set and call $t->set_var($db->f('varname'), $db->f('trans')) for each > record. And that's why you have the getText() method which you can replace with whatever method you choose -- be it loading a language file or doing a SQL query. To support your method of doing a "massive SQL query" the getText() method could be required to accept a string or an array of all the strings in the template. I'm not sure of the most efficient way to do all this. I was thinking something like doing a preg_replace_callback() and using the getText() method as the callback. > > Your PHP script wouldn't even need to know the template varnames :) That's pretty much the whole idea. :) |