[jifty-devel] understanding Jifty::Param::Schema::schema() !

Edmund von der Burg evdb at ecclestoad.co.uk
Tue Mar 6 03:53:21 EST 2007


On 01/03/07, mraptor <mraptor at gmail.com> wrote:
> In Jifty::Param::Schema::schema()
>
> there is this code :
>
>     local *_ = sub { my $args = \@_; defer { _(@$args) } };
>
> but later I don't see $_, @_, %_ or &_ to be used ? What is the purpose
> of this ?

In jifty there is a sub called '_' that handles localisation:

  my $foreign = _( 'translate me' );

When creating the schema it is not possible to know what language the
user wants so it is not possible to translate the string. This lets
you say to jifty that you want this string translated but puts off the
translation until later on when the language is known.

'local *_' creates a localised subroutine - so it does not interfere
with the main '_' subroutine

'my $args = \@args;' this creates a copy of the arguments.

'defer { _(@$args) }' this passes a code block to the 'defer'
subroutine. This code block is a closure so it is not actually
executed straight away. It is executed later when the users language
is known so that the translation can be done correctly.

It is important to get the _('text to translate') into the code so
that the extraction tools can find the string to add it to the .po
file to be translated.

A rather elegant solution.

Hope this is all correct.

Cheers,
  Edmund.


More information about the jifty-devel mailing list