TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   Multilingual sites (http://www.talkphp.com/advanced-php-programming/3057-multilingual-sites.html)

ryanmr 07-02-2008 10:20 PM

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?

delayedinsanity 07-03-2008 12:19 AM

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

ryanmr 07-03-2008 03:17 AM

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.

delayedinsanity 07-03-2008 04:40 AM

Hey, that's way easier than I was going to make it. I didn't know about vsprintf.
-m


All times are GMT. The time now is 06:22 PM.

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