TalkPHP
 
 
Account Login
Latest Articles
» How to keep your forms from double posting data
» cURL Basics
» Securing your PHP applications Part 1
» The way the function rolls
» Database Abstraction with Zend_Db - Part 2
Advertisement
Associates
Associates
techtuts Darkmindz
CSS Tutorials Tutorialsphere.com - Free Online Tutorials
Boston PHP SurfnLearn
Reply
 
LinkBack Thread Tools 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: 27
Thanks: 2
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)
The Gregarious
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas
Posts: 651
Thanks: 24
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: 27
Thanks: 2
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)
The Gregarious
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas
Posts: 651
Thanks: 24
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
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 01:42 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0