TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 07-02-2008, 10:20 PM   #1 (permalink)
The Contributor
 
ryanmr's Avatar
 
Join Date: Jun 2008
Location: Twin Cities, Minnesota, USA
Posts: 44
Thanks: 3
ryanmr is on a distinguished road
Default Multilingual sites

I read Wildhoney's tutorial on Multilingual sites and completely agree that XML is much better over PHP constants or something like that.
I have a question though, about variables in phrases.
HTML Code:
<!-- english -->
<phrase id="new_messages">You have new messages.</phrase>
<!-- spanish -->
<phrase id="new_message">Usted tiene mensajes nuevos.</phrase>
Above are static phrases that don't change, we don't say how many new messages.
HTML Code:
<!-- english -->
<phrase id="how_many_new_messages">You have %1$d new messages.</phrase>
<!-- spanish -->
<phrase id="how_many_new_messages">Usted tiene %1$d nuevos mensajes.</phrase>
Above are variable messages, they will change depending on number of messages. %1$d is just a way to represent the variability of it, since I don't know how otherwise. I'm not sure if sprintf would work in all cases without some type of intermediate function to give it the variables to be allowed in the phrase.
What kind of system would you use to handle this?
ryanmr is offline  
Reply With Quote
Old 07-03-2008, 12:19 AM   #2 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

The same one, if I understand what you want to do. A simple modification of Wildhoney's original class would include another almost identical function:

PHP Code:
    public function parsePhrase($szItem$szVar)
    {
        
$aItem $this->m_pXML->xpath(sprintf("//phrase[@id='%s']"$szItem));
        
$szItem = empty($aItem[0]) ? null : (string) $aItem[0];
    
        return 
sprintf($szItem$szVar);
    } 
You could continue to build on this in various ways, by using func_get_args, or by allowing $szVar the possibility of being an array which you could explode and use in the sprintf, and so on and so forth.
-m
delayedinsanity is offline  
Reply With Quote
Old 07-03-2008, 03:17 AM   #3 (permalink)
The Contributor
 
ryanmr's Avatar
 
Join Date: Jun 2008
Location: Twin Cities, Minnesota, USA
Posts: 44
Thanks: 3
ryanmr is on a distinguished road
Default

I thought it would be complicated actually.
I found vsrpintf which seems to be exactly what I want.
This is what I came up with,
PHP Code:
    public function parsePhrase($szItem$varArray)
    {
        
$aItem $this->m_pXML->xpath(sprintf("//phrase[@id='%s']"$szItem));
        
$szItem = empty($aItem[0]) ? null : (string) $aItem[0];
    
        return 
vsprintf($szItem$varArray);
    } 
I can load up $varArry with whatever I use in the string template.
Thanks for your help.
ryanmr is offline  
Reply With Quote
Old 07-03-2008, 04:40 AM   #4 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

Hey, that's way easier than I was going to make it. I didn't know about vsprintf.
-m
delayedinsanity is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 12:43 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design