Why strings aren't translated here?

View: New views
7 Messages — Rating Filter:   Alert me  

Why strings aren't translated here?

by Milan Dinić :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I have a problem with my plugin (
http://wordpress.org/extend/plugins/gravatar-signup-encouragement/ ). Two
strings aren't translated with gettext even though they are in po/mo files.
First one is text of message (line 71) which is made only on activation
(line 93) and later is saved in database. Second is URL to locale version of
Gravatar (line 58). In both cases untranslated strings are shown.

Does anyone know what is problem with them?

Thanks in advance
_______________________________________________
wp-hackers mailing list
wp-hackers@...
http://lists.automattic.com/mailman/listinfo/wp-hackers

Re: Why strings aren't translated here?

by Peter Westwood :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


On 29 Oct 2009, at 21:20, Milan Dinić wrote:

> I have a problem with my plugin (
> http://wordpress.org/extend/plugins/gravatar-signup- 
> encouragement/ ). Two
> strings aren't translated with gettext even though they are in po/mo  
> files.
> First one is text of message (line 71) which is made only on  
> activation
> (line 93) and later is saved in database. Second is URL to locale  
> version of
> Gravatar (line 58). In both cases untranslated strings are shown.
>
> Does anyone know what is problem with them?

You are calling the translation functions before you load you  
translation files.

You call the translation for these when the plugin is included but  
load the file on the 'init' hook which runs afterwards.

Hope that helps
--
Peter Westwood
http://blog.ftwr.co.uk | http://westi.wordpress.com
C53C F8FC 8796 8508 88D6 C950 54F4 5DCD A834 01C5

_______________________________________________
wp-hackers mailing list
wp-hackers@...
http://lists.automattic.com/mailman/listinfo/wp-hackers

Re: Why strings aren't translated here?

by Milan Dinić :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thank you for quick answer. So when should I load textdomain?

2009/10/29 Peter Westwood <peter.westwood@...>

>
> On 29 Oct 2009, at 21:20, Milan Dinić wrote:
>
>  I have a problem with my plugin (
>> http://wordpress.org/extend/plugins/gravatar-signup-encouragement/ ). Two
>> strings aren't translated with gettext even though they are in po/mo
>> files.
>> First one is text of message (line 71) which is made only on activation
>> (line 93) and later is saved in database. Second is URL to locale version
>> of
>> Gravatar (line 58). In both cases untranslated strings are shown.
>>
>> Does anyone know what is problem with them?
>>
>
> You are calling the translation functions before you load you translation
> files.
>
> You call the translation for these when the plugin is included but load the
> file on the 'init' hook which runs afterwards.
>
> Hope that helps
> --
> Peter Westwood
> http://blog.ftwr.co.uk | http://westi.wordpress.com
> C53C F8FC 8796 8508 88D6 C950 54F4 5DCD A834 01C5
>
> _______________________________________________
> wp-hackers mailing list
> wp-hackers@...
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>
_______________________________________________
wp-hackers mailing list
wp-hackers@...
http://lists.automattic.com/mailman/listinfo/wp-hackers

Re: Why strings aren't translated here?

by Otto-19 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

You're loading the textdomain properly (at init), but you're trying to
translate those strings too early. Remove them from the "main body" of
the function, and add them to a function somewhere.

Basically, you shouldn't generally do anything in the main body of a
plugin that requires WordPress to be fully loaded, because your plugin
is included before a lot of stuff happens. Always try to put stuff
into functions, then hook those in at the right places.

And honestly, you're doing some strange and seemingly unnecessary
stuff here. Here's an example of what I mean:

You translate this string into a global variable:
$gse_tip_text_unformated = sprintf(__("It seems that you don't have an
avatar on Gravatar. Click <a href='%s' target='_blank'>here</a> to
make one.", "gse_textdomain"), $gse_url_placeholder);

Then, later, in a function, you use it (again, global variable):
$gse_options['tip_text'] = $gse_tip_text_unformated;

But that's the only place you ever use it. Why in the world make a
global variable to hold it at all? Why put it in the main body? Why
not just do this?
$gse_options['tip_text'] = sprintf(__("It seems that you don't have an
avatar on Gravatar. Click <a href='%s' target='_blank'>here</a> to
make one.", "gse_textdomain"), $gse_url_placeholder);

Translation works anywhere. Don't use globals that you don't have to
use. Translate only when you actually need to do the translation,
don't pre-translate stuff early. Hey, you may not even need those
strings, so translating them early is a waste of time and resources.
The way you're doing it now, all those strings get translated on every
single page load, even if the strings are never even used (like,
you're not displaying the admin screen). It's wholly unnecessary to do
all that stuff.

Eliminate these globals. Move translations to the point at which
they're needed. Save yourself a lot of trouble.

-Otto



On Thu, Oct 29, 2009 at 4:35 PM, Milan Dinić <liste@...> wrote:

> Thank you for quick answer. So when should I load textdomain?
>
> 2009/10/29 Peter Westwood <peter.westwood@...>
>
>>
>> On 29 Oct 2009, at 21:20, Milan Dinić wrote:
>>
>>  I have a problem with my plugin (
>>> http://wordpress.org/extend/plugins/gravatar-signup-encouragement/ ). Two
>>> strings aren't translated with gettext even though they are in po/mo
>>> files.
>>> First one is text of message (line 71) which is made only on activation
>>> (line 93) and later is saved in database. Second is URL to locale version
>>> of
>>> Gravatar (line 58). In both cases untranslated strings are shown.
>>>
>>> Does anyone know what is problem with them?
>>>
>>
>> You are calling the translation functions before you load you translation
>> files.
>>
>> You call the translation for these when the plugin is included but load the
>> file on the 'init' hook which runs afterwards.
>>
>> Hope that helps
>> --
>> Peter Westwood
>> http://blog.ftwr.co.uk | http://westi.wordpress.com
>> C53C F8FC 8796 8508 88D6 C950 54F4 5DCD A834 01C5
>>
>> _______________________________________________
>> wp-hackers mailing list
>> wp-hackers@...
>> http://lists.automattic.com/mailman/listinfo/wp-hackers
>>
> _______________________________________________
> wp-hackers mailing list
> wp-hackers@...
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>
_______________________________________________
wp-hackers mailing list
wp-hackers@...
http://lists.automattic.com/mailman/listinfo/wp-hackers

Re: Why strings aren't translated here?

by Milan Dinić :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thank you for your detailed answer. I'm not sure why I started using global
variable for translation but I think it was because I tried various ways to
make translation working and none succeed so trial with variables was last
one.

Now I tried what you suggested ( $gse_options['tip_text'] = sprintf(__("It
seems that ... ) but it still doesn't translate that string. Maybe is
problem because it is run in register_activation_hook. You can see that for
yourself if you uninstall plugin and rename sr_RS.mo file to en_US.mo file
and see that on activation original string is saved in database and not
translated one, while other strings in settings page are translated.

Thanks again.

2009/10/30 Otto <otto@...>

> You're loading the textdomain properly (at init), but you're trying to
> translate those strings too early. Remove them from the "main body" of
> the function, and add them to a function somewhere.
>
> Basically, you shouldn't generally do anything in the main body of a
> plugin that requires WordPress to be fully loaded, because your plugin
> is included before a lot of stuff happens. Always try to put stuff
> into functions, then hook those in at the right places.
>
> And honestly, you're doing some strange and seemingly unnecessary
> stuff here. Here's an example of what I mean:
>
> You translate this string into a global variable:
> $gse_tip_text_unformated = sprintf(__("It seems that you don't have an
> avatar on Gravatar. Click <a href='%s' target='_blank'>here</a> to
> make one.", "gse_textdomain"), $gse_url_placeholder);
>
> Then, later, in a function, you use it (again, global variable):
> $gse_options['tip_text'] = $gse_tip_text_unformated;
>
> But that's the only place you ever use it. Why in the world make a
> global variable to hold it at all? Why put it in the main body? Why
> not just do this?
> $gse_options['tip_text'] = sprintf(__("It seems that you don't have an
> avatar on Gravatar. Click <a href='%s' target='_blank'>here</a> to
> make one.", "gse_textdomain"), $gse_url_placeholder);
>
> Translation works anywhere. Don't use globals that you don't have to
> use. Translate only when you actually need to do the translation,
> don't pre-translate stuff early. Hey, you may not even need those
> strings, so translating them early is a waste of time and resources.
> The way you're doing it now, all those strings get translated on every
> single page load, even if the strings are never even used (like,
> you're not displaying the admin screen). It's wholly unnecessary to do
> all that stuff.
>
> Eliminate these globals. Move translations to the point at which
> they're needed. Save yourself a lot of trouble.
>
> -Otto
>
>
>
> On Thu, Oct 29, 2009 at 4:35 PM, Milan Dinić <liste@...> wrote:
> > Thank you for quick answer. So when should I load textdomain?
> >
> > 2009/10/29 Peter Westwood <peter.westwood@...>
> >
> >>
> >> On 29 Oct 2009, at 21:20, Milan Dinić wrote:
> >>
> >>  I have a problem with my plugin (
> >>> http://wordpress.org/extend/plugins/gravatar-signup-encouragement/ ).
> Two
> >>> strings aren't translated with gettext even though they are in po/mo
> >>> files.
> >>> First one is text of message (line 71) which is made only on activation
> >>> (line 93) and later is saved in database. Second is URL to locale
> version
> >>> of
> >>> Gravatar (line 58). In both cases untranslated strings are shown.
> >>>
> >>> Does anyone know what is problem with them?
> >>>
> >>
> >> You are calling the translation functions before you load you
> translation
> >> files.
> >>
> >> You call the translation for these when the plugin is included but load
> the
> >> file on the 'init' hook which runs afterwards.
> >>
> >> Hope that helps
> >> --
> >> Peter Westwood
> >> http://blog.ftwr.co.uk | http://westi.wordpress.com
> >> C53C F8FC 8796 8508 88D6 C950 54F4 5DCD A834 01C5
> >>
> >> _______________________________________________
> >> wp-hackers mailing list
> >> wp-hackers@...
> >> http://lists.automattic.com/mailman/listinfo/wp-hackers
> >>
> > _______________________________________________
> > wp-hackers mailing list
> > wp-hackers@...
> > http://lists.automattic.com/mailman/listinfo/wp-hackers
> >
> _______________________________________________
> wp-hackers mailing list
> wp-hackers@...
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>
_______________________________________________
wp-hackers mailing list
wp-hackers@...
http://lists.automattic.com/mailman/listinfo/wp-hackers

Re: Why strings aren't translated here?

by Otto-19 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Fri, Oct 30, 2009 at 3:20 PM, Milan Dinić <liste@...> wrote:
> Now I tried what you suggested ( $gse_options['tip_text'] = sprintf(__("It
> seems that ... ) but it still doesn't translate that string. Maybe is
> problem because it is run in register_activation_hook.

Yes, that is indeed your problem. Or, rather, the lack of the init is
your problem.

When the activation hook fires, your plugin has just been included.
Init occurred before that happened. So your init doesn't get run, and
thus your text domain never gets loaded before you try to start
translating things. You need to load the text domain before you use
translation functions.


-Otto
Sent from Memphis, TN, United States
_______________________________________________
wp-hackers mailing list
wp-hackers@...
http://lists.automattic.com/mailman/listinfo/wp-hackers

Re: Why strings aren't translated here?

by Milan Dinić :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I solved this by placing another load_plugin_textdomain function in function
that is run on register_activation_hook. That way string is localized on
activation of plugin.

For second string I followed your advice and replaced variable with
function, and string is localized in function.

Thank you for your help.

2009/11/2 Otto <otto@...>

> On Fri, Oct 30, 2009 at 3:20 PM, Milan Dinić <liste@...> wrote:
> > Now I tried what you suggested ( $gse_options['tip_text'] =
> sprintf(__("It
> > seems that ... ) but it still doesn't translate that string. Maybe is
> > problem because it is run in register_activation_hook.
>
> Yes, that is indeed your problem. Or, rather, the lack of the init is
> your problem.
>
> When the activation hook fires, your plugin has just been included.
> Init occurred before that happened. So your init doesn't get run, and
> thus your text domain never gets loaded before you try to start
> translating things. You need to load the text domain before you use
> translation functions.
>
>
> -Otto
> Sent from Memphis, TN, United States
> _______________________________________________
> wp-hackers mailing list
> wp-hackers@...
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>
_______________________________________________
wp-hackers mailing list
wp-hackers@...
http://lists.automattic.com/mailman/listinfo/wp-hackers