# Created by Log::Report's __ functions
Any used of a translation function, like Log::Report::__() or Log::Report::__x() will result in this object. It will capture some environmental information, and delay the translation until it is needed.
Creating an object first, and translating it later, is slower than translating it immediately. However, on the location where the message is produced, we do not yet know to what language to translate: that depends on the front-end, the log dispatcher.
See DETAILS section below, for an in-depth description.
The Log::Report functions which define translation request can all
have OPTIONS. Some can have VARIABLES to be interpolated in the string as
well. To distinguish between the OPTIONS and VARIABLES (both a list
of key-value pairs), the keys of the OPTIONS start with an underscore _
.
As result of this, please avoid the use of keys which start with an
underscore in variable names. On the other hand, you are allowed to
interpolate OPTION values in your strings.
With the __x()
or __nx()
, interpolation will take place on the
translated MSGID string. The translation can contain the VARIABLE
and OPTION names between curly brackets. Text between curly brackets
which is not a known parameter will be left untouched.
Next to the name, you can specify a format code. With gettext()
,
you often see this:
printf gettext("approx pi: %.6f\n"), PI;
Locale::TextDomain has two ways.
printf __"approx pi: %.6f\n", PI; print __x"approx pi: {approx}\n", approx => sprintf("%.6f", PI);
The first does not respect the wish to be able to reorder the
arguments during translation. The second version is quite long.
With Log::Report
, above syntaxes do work, but you can also do
print __x"approx pi: {pi%.6f}\n", pi => PI;
So: the interpolation syntax is { name [format] }
. Other
examples:
print __x "{perms} {links%2d} {user%-8s} {size%10d} {fn}\n" , perms => '-rw-r--r--', links => 1, user => 'me' , size => '12345', fn => $filename;
An additional advantage is the fact that not all languages produce comparable length strings. Now, the translators can take care that the layout of tables is optimal.
You are permitted the interpolate OPTION values in your string. This may simplify your coding. The useful names are:
With Locale::TextDomain, you have to do
use Locale::TextDomain; print __nx ( "One file has been deleted.\n" , "{num} files have been deleted.\n" , $num_files , num => $num_files );
With Log::Report
, you can do
use Log::Report; print __nx ( "One file has been deleted.\n" , "{_count} files have been deleted.\n" , $num_files );
Of course, you need to be aware that the name used to reference the
counter is fixed to _count
. The first example works as well, but
is more verbose.
There is no way of checking beforehand whether you have provided all required values, to be interpolated in the translated string. A translation could be specified like this:
my @files = @ARGV; local $" = ', '; my $s = __nx "One file specified ({files})" , "{_count} files specified ({files})" , scalar @files # actually, 'scalar' is not needed , files => \@files;
For interpolating, the following rules apply:
Log::Report::Message
object which is created with
the __xn
can be seen as a closure. The translation can be reused.
See example below.
Log::Report::Message
object which is being handled is passed as
only argument. This is a hash in which all OPTIONS and VARIABLES
can be found.
$"
between the elements.
This way of translating is somewhat expensive, because an object to
handle the __x()
is created each time.
for my $i (1..100_000) { print __x "Hello World {i}\n", $i; }
The suggestion that Locale::TextDomain makes to improve performance, is to get the translation outside the loop, which only works without interpolation:
use Locale::TextDomain; my $i = 42; my $s = __x("Hello World {i}\n", i => $i); foreach $i (1..100_000) { print $s; }
Oops, not what you mean. With Log::Report, you can do
use Log::Report; my $i; my $s = __x("Hello World {i}", i => \$i); foreach $i (1..100_000) { print $s; }
Mind you not to write: for my $i
in this case!!!!
You can also write an incomplete translation:
use Log::Report; my $s = __x "Hello World {i}"; foreach my $i (1..100_000) { print $s->(i => $i); }
In either case, the translation will be looked-up only once.