Zend_Translate using INI adaptor

3 messages Options
Embed this post
Permalink
aztechy

Zend_Translate using INI adaptor

Reply Threaded More More options
Print post
Permalink
Hello all,

Hoping to get pointed in the right direction here.  Currently setting up our application to have translation.  What I currently have going on are the following:

In our bootstrap we set an instance of Zend_Translate and Zend_Locale in the registry as follows:

Zend_Registry::set('Zend_Locale', new Zend_Locale(Zend_Registry::get('config')->locale));
Zend_Registry::set('Zend_Translate', new Zend_Translate('ini', '../language/', null, array('scan' => Zend_Translate::LOCALE_FILENAME)));

So now in the controller i can access Zend_Translate by pulling it out of the registry as such:
$translate = Zend_Regsitry::get('Zend_Translate');

Make translation calls like:
print $translate->_('Some string I want translated');

This all works perfect, my stumbling block is how to define the keys within my ini file if the translation text is kind of unkown.  The zend writeup has an example using printf like:
printf($translate->_('The date is %1\$s'), $date);

So I figured I'd do something simple like translate a 'Hello <name>' string.  The test call to this would seem to be:

printf($translate->_("Hello %1\$s"), $name);  // Where name is defined earlier to take user input.

Then in my INI file I would define a key => translated text pair to look like:
'Hello %1\$s' = "Hola %1\$s';  // If this were the pair for my spanish file.

However this doesn't work for a couple of reasons.
1) INI adaptor breaks it seems on keys that have '\' or '$' characters in them.
2) The call to translate above would turn my key into "Hello bobby", for example if value of name was bobby, and the translater not able to find the key would just output Hello bobby onto the screen and not  "Hola %1\$s" or even Hola bobby.

I obviously am missing something here, again if someone can point me in the right direction, it would be greatly appreciated.
thomasW

Re: Zend_Translate using INI adaptor

Reply Threaded More More options
Print post
Permalink
*) You don't need printf... the translation view helper supports this out of
the box.

*) Using placeholder you must keep aware of the string format you are using.
"%1\$s" is different to '%1\$s'

When the format you are using escapes the characters then you must add
another \ which would read
"%1\\$s"... but this depends on your format.

Note that this is plain php and has nothing to do with the adapter itself.

Greetings
Thomas Weidner, I18N Team Leader, Zend Framework
http://www.thomasweidner.com

----- Original Message -----
From: "aztechy" <[hidden email]>
To: <[hidden email]>
Sent: Saturday, June 27, 2009 12:41 AM
Subject: [fw-i18n] Zend_Translate using INI adaptor


>
> Hello all,
>
> Hoping to get pointed in the right direction here.  Currently setting up
> our
> application to have translation.  What I currently have going on are the
> following:
>
> In our bootstrap we set an instance of Zend_Translate and Zend_Locale in
> the
> registry as follows:
>
> Zend_Registry::set('Zend_Locale', new
> Zend_Locale(Zend_Registry::get('config')->locale));
> Zend_Registry::set('Zend_Translate', new Zend_Translate('ini',
> '../language/', null, array('scan' => Zend_Translate::LOCALE_FILENAME)));
>
> So now in the controller i can access Zend_Translate by pulling it out of
> the registry as such:
> $translate = Zend_Regsitry::get('Zend_Translate');
>
> Make translation calls like:
> print $translate->_('Some string I want translated');
>
> This all works perfect, my stumbling block is how to define the keys
> within
> my ini file if the translation text is kind of unkown.  The zend writeup
> has
> an example using printf like:
> printf($translate->_('The date is %1\$s'), $date);
>
> So I figured I'd do something simple like translate a 'Hello <name>'
> string.
> The test call to this would seem to be:
>
> printf($translate->_("Hello %1\$s"), $name);  // Where name is defined
> earlier to take user input.
>
> Then in my INI file I would define a key => translated text pair to look
> like:
> 'Hello %1\$s' = "Hola %1\$s';  // If this were the pair for my spanish
> file.
>
> However this doesn't work for a couple of reasons.
> 1) INI adaptor breaks it seems on keys that have '\' or '$' characters in
> them.
> 2) The call to translate above would turn my key into "Hello bobby", for
> example if value of name was bobby, and the translater not able to find
> the
> key would just output Hello bobby onto the screen and not  "Hola %1\$s" or
> even Hola bobby.
>
> I obviously am missing something here, again if someone can point me in
> the
> right direction, it would be greatly appreciated.
>
> --
> View this message in context:
> http://www.nabble.com/Zend_Translate-using-INI-adaptor-tp24228285p24228285.html
> Sent from the Zend I18N/Locale mailing list archive at Nabble.com.

aztechy

Re: Zend_Translate using INI adaptor

Reply Threaded More More options
Print post
Permalink
thomasW wrote:
*) You don't need printf... the translation view helper supports this out of
the box.
Ah I just noticed now as I look at the view helper and tested it out that you can pass multiple parameters to it.  So new question, if I pull Zend_Translate from the registry and use that object to translate, I can't make the call with multiple parameters, like: $translate->_('SomeKey', 'var1').

Of course if I do a call to, if I'm in the controller, $this->view->translate('SomeKey', 'var1') the translation string will of course be properly translated with the variable.  

Is there something additional I can do with the Zend_Translate::_ call to allow for multiple params to be passed?  I figure I could create my own translate function to utilize the object and then function the same way as the view helper.  The only reason to use the Zend_Translate object is we don't always have translation calls being made in a controller.  We seem to have translation needs in view helpers, form classes and action helpers.

thomasW wrote:
*) Using placeholder you must keep aware of the string format you are using.
"%1\$s" is different to '%1\$s'

When the format you are using escapes the characters then you must add
another \ which would read
"%1\\$s"... but this depends on your format.
Definitely figured that out after a little more thought after posting :P

Thanks again.