Thanks for your suggestion, but is there any way to accomplish this without
changing the cake core?  

Lapinski


Julien Buratto-5 wrote:
> 
> 
> I thought that this could interest someone else.
> 
> If you want to be able to translate strings which contains dynamic
> data such:
> "Hello, my name is Julien" where "Julien" is a name, you could do
> this:
> 
> sprintf(__("Hello, my name is %s",true),"Julien");
> 
> and your translation string would be "Hello, my name is %s" which is
> easy to translate.
> The issue with that is that it's absolutely not handy to use in views.
> 
> What I liked to have was a function where I could pass values, such
> as:
> 
> __("Hello, my name is %s and I'm %d years old",true,array("Julien",
> 34));
> expected result:
> Hello, my name is Julien and I'm 34 years old.
> 
> Id order to achive this I have changed the cake/basics.php file where
> the __ function is located, basic steps are:
> - add a third parameter to the function __ in order to support the
> array of values
> - use the vsprintf to pass an array of values to the sprintf string
> - evaluate if there are any value passed otherwise use the older
> behaviour
> 
> Result is very simple and looks clean to me:
> 
> /**
>  * Returns a translated string if one is found; Otherwise, the
> submitted message.
>  *
>  * @param string $singular Text to translate
>  * @param boolean $return Set to true to return translated string, or
> false to echo
>  * @return mixed translated string if $return is false string will be
> echoed
>  * @link http://book.cakephp.org/view/693/__
>  */
>       function __($singular, $return = false,$values=array()) {
>               if (!$singular) {
>                       return;
>               }
>               if (!class_exists('I18n')) {
>                       App::import('Core', 'i18n');
>               }
> 
>               if ($return === false) {
>                       if (count($values)>0) echo 
> vsprintf(I18n::translate($singular),
> $values);
>                       else echo I18n::translate($singular);
>               } else {
>                       if (count($values)>0) return 
> vsprintf(I18n::translate($singular),
> $values);
>                       else return I18n::translate($singular);
>               }
> 
> 
> > 
> 
> 

-- 
View this message in context: 
http://www.nabble.com/Localization---values-in-translated-strings---my-solution-tp24180760p24183152.html
Sent from the CakePHP mailing list archive at Nabble.com.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to