[wp-hackers] Why strings aren't translated here?

Otto otto at ottodestruct.com
Fri Oct 30 15:58:11 UTC 2009


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 at srpski.biz> wrote:
> Thank you for quick answer. So when should I load textdomain?
>
> 2009/10/29 Peter Westwood <peter.westwood at ftwr.co.uk>
>
>>
>> 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 at lists.automattic.com
>> http://lists.automattic.com/mailman/listinfo/wp-hackers
>>
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>


More information about the wp-hackers mailing list