SYNOPSIS

 # Objects created by Log::Report's __ functions
 # Full feature description in the DETAILS section

 # no interpolation
 __"Hello, World";

 # with interpolation
 __x"age {years}", years => 12;

 # interpolation for one or many
 my $nr_files = @files;
 __nx"one file", "{_count} files", $nr_files;
 __nx"one file", "{_count} files", \@files;

 # interpolation of arrays
 __x"price-list: {prices%.2f}", prices => \@prices, _join => ', ';

 # white-spacing on msgid preserved
 print __"\tCongratulations,\n";
 print "\t", __("Congratulations,"), "\n";  # same

DESCRIPTION

Any use of a translation function exported by Log::Report, like __() (the function is named underscore-underscore) or __x() (underscore-underscore-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 in what language to translate it to: that depends on the front-end, the log dispatcher.

DETAILS

OPTIONS and VARIABLES

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.

Interpolating

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.

 fault __x"cannot open open {filename}", filename => $fn;

 print __xn"directory {dir} contains one file"
          ,"directory {dir} contains {nr_files} files"
          , scalar(@files)            # (1) (2)
          , nr_files => scalar @files # (3)
          , dir      => $dir;

(1) this required third parameter is used to switch between the different plural forms. English has only two forms, but some languages have many more.

(2) the "scalar" keyword is not needed, because the third parameter is in SCALAR context. You may also pass \@files there, because ARRAYs will be converted into their length. A HASH will be converted into the number of keys in the HASH.

(3) the scalar keyword is required here, because it is LIST context: otherwise all filenames will be filled-in as parameters to __xn(). See below for the available _count valure, to see how the nr_files parameter can disappear.

Interpolation of VARIABLES

Log::Report uses String::Print to interpolate values in(translated) messages. This is a very powerful syntax, and you should certainly read that manual-page. Here, we only described additional features, specific to the usage of String::Print in Log::Report::Message objects.

There is no way of checking beforehand whether you have provided all required values, to be interpolated in the translated string.

For interpolating, the following rules apply:

 local $" = ', ';
 error __x"matching files: {files}", files => \@files;

 error __x"matching files: {files}", files => \@files, _join => ', ';

Interpolation of OPTIONS

You are permitted the interpolate OPTION values in your string. This may simplify your coding. The useful names are:

» Example: using the _count

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.

Handling white-spaces

In above examples, the msgid and plural form have a trailing new-line. In general, it is much easier to write

   print __x"Hello, World!\n";

than

   print __x("Hello, World!") . "\n";

For the translation tables, however, that trailing new-line is "over information"; it is an layout issue, not a translation issue.

Therefore, the first form will automatically be translated into the second. All leading and trailing white-space (blanks, new-lines, tabs, ...) are removed from the msgid before the look-up, and then added to the translated string.

Leading and trailing white-space on the plural form will also be removed. However, after translation the spacing of the msgid will be used.

Avoiding repetative translations

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 => $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 because the first value of $i is captured in the initial message object. With Log::Report, you can do it (except when you use contexts)

 use Log::Report;
 my $i;
 my $s = __x("Hello World {i}\n", i => \$i);
 foreach $i (1..100_000)
 {   print $s;
 }

Mind you not to write: for my $i in above case!!!!

You can also write an incomplete translation:

 use Log::Report;
 my $s = __x "Hello World {i}\n";
 foreach my $i (1..100_000)
 {   print $s->(i => $i);
 }

In either case, the translation will be looked-up only once.