[wp-hackers] Improvements to the WordPress l10n framework

Omry Yadan omry at yadan.net
Sat May 12 10:17:27 GMT 2007


Jamie Talbot wrote:

> > consider the difference between:
> > $str = "There are ".$x." new messages";
> > to:
> > $str = __("There are ").$x.__(" new messages");
>
> > pretty close.
> > now, what does this do the the code readability?
>
> > $str = __("There are ","foobar plugin").$x.__(" new messages","foobar
> > plugin");
>
> > even if plugin authors were aware of this option (and they are not),
> > many will not use it simply because makes the code uglier.
>
> Well firstly, they shouldn't do that anyway, because word order
> between different languages is
> totally different.  What they should do is:
>
> $str = sprintf(__("There are %s new messages", 'foobar_plugin'), $x);
>
> Which I find very easy to read.
You are right of course, but there are other examples (for example when
embedding php translations directly into html code) where it will make a
difference.

>
> > the only problem is that when the platform is calling hooks code, I
> > don't think it knows which plugin "owns" that code, so it can't possibly
> > make the correct translation available for it.
> > maybe the right solution would be to flatten the entire translation
> > space into a single hashtable, and load the plugins translations into it
> > as well.
>
> Exactly - we don't know which plugin 'owns' which string, which means
> we have to search a
> potentially huge translation space for every string.  Why not then
> just do away with domains
> altogether?  I'm not coming at this with no experience, and I'm not
> being deliberately negative -
> just that your solution isn't really a solution at all.  I genuinely
> would be very interested if you
> can supply concrete implementation details for your suggestion.
look at the attached file, it contains a very basic prototype.
extract it into your web server dir and run do-stuff.php.

the idea is the code that initializes the plugins initialize a global
called $plugin_id before calling the plugin init code.
when the plugin adds an action, it will pass that global to the
add_action function, that will use it to re-obtain context when calling
the actions.
it can easily be improved further, so that the plugins does not need to
pass the plugin_id to the register function simply by letting the
add_action function access the global directly. (I didn't do it because
its harder to understand, and this is just an example).

function add_action($action_type, $callback, $plugin_id)
{
   ///
}

function add_action($action_type, $callback)
{
    global $plugin_id;
    ///
}

this makes the fair assumption that add_action is only called from the
plugin init code.



More information about the wp-hackers mailing list