File: Message.pod

package info (click to toggle)
liblog-report-perl 0.94-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 568 kB
  • sloc: perl: 3,649; makefile: 7
file content (400 lines) | stat: -rw-r--r-- 11,439 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
=head1 NAME

Log::Report::Message - a piece of text to be translated

=head1 SYNOPSIS

 # Created by Log::Report's __ functions

=head1 DESCRIPTION

Any used of a translation function, like L<Log::Report::__()|Log::Report/"Language Translations"> or 
L<Log::Report::__x()|Log::Report/"Language Translations"> 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.

=head1 METHODS

=head2 Constructors

=over 4

=item $obj-E<gt>B<clone>(OPTIONS, VARIABLES)

Returns a new object which copies info from original, and updates it
with the specified OPTIONS and VARIABLES.  The advantage is that the
cached translations are shared between the objects.

example: use of clone()

 my $s = __x "found {nr} files", nr => 5;
 my $t = $s->clone(nr => 3);
 my $t = $s->(nr => 3);      # equivalent
 print $s;     # found 5 files
 print $t;     # found 3 files

=item Log::Report::Message-E<gt>B<new>(OPTIONS, VARIABLES)

B<Do not use this method directly>, but use L<Log::Report::__()|Log::Report/"Language Translations"> and
friends.

 -Option   --Default
  _append    undef
  _category  undef
  _class     []
  _classes   []
  _count     undef
  _domain    from use
  _expand    false
  _msgid     undef
  _plural    undef
  _prepend   undef
  _to        <undef>

=over 2

=item _append => STRING

=item _category => INTEGER

=item _class => STRING|ARRAY

When messages are used for exception based programming, you add
C<_class> parameters to the argument list.  Later, with for instance
L<Log::Report::Dispatcher::Try::wasFatal(class)|Log::Report::Dispatcher::Try/"Status">, you can check the
category of the message.

One message can be part of multiple classes.  The STRING is used as
comma- and/or blank separated list of class tokens, the ARRAY lists all
tokens separately. See L<classes()|Log::Report::Message/"Accessors">.

=item _classes => STRING|ARRAY

Alternative for C<_class>, which cannot be used at the same time.

=item _count => INTEGER

When defined, then C<_plural> need to be defined as well.

=item _domain => STRING

The textdomain in which this msgid is defined.

=item _expand => BOOLEAN

Indicates whether variables are filled-in.

=item _msgid => MSGID

The message label, which refers to some translation information.  Usually
a string which is close the English version of the error message.  This
will also be used if there is no translation possible

=item _plural => MSGID

Can be specified when a C<_count> is specified.  This plural form of
the message is used to simplify translation, and as fallback when no
translations are possible: therefore, this can best resemble an English
message.

=item _prepend => STRING

=item _to => NAME

Specify the NAME of a dispatcher as destination explicitly. Short
for  C<< report {to => NAME}, ... >>  See L<to()|Log::Report::Message/"Accessors">

=back

=back

=head2 Accessors

=over 4

=item $obj-E<gt>B<append>

Returns the string or L<Log::Report::Message|Log::Report::Message> object which is appended
after this one.  Usually C<undef>.

=item $obj-E<gt>B<classes>

Returns the LIST of classes which are defined for this message; message
group indicators, as often found in exception-based programming.

=item $obj-E<gt>B<count>

Returns the count, which is used to select the translation
alternatives.

=item $obj-E<gt>B<domain>

Returns the domain of the first translatable string in the structure.

=item $obj-E<gt>B<msgid>

Returns the msgid which will later be translated.

=item $obj-E<gt>B<prepend>

Returns the string which is prepended to this one.  Usually C<undef>.

=item $obj-E<gt>B<to>([NAME])

Returns the NAME of a dispatcher if explicitly specified with
the '_to' key. Can also be used to set it.  Usually, this will
return undef, because usually all dispatchers get all messages.

=item $obj-E<gt>B<valueOf>(PARAMETER)

Lookup the named PARAMETER for the message.  All pre-defined names
have their own method, and should be used with preference.

example: 

When the message was produced with
  my @files = qw/one two three/;
  my $msg = __xn "found one file: {files}"
               , "found {_count} files: {files}"
               , scalar @files, files => \@files
               , _class => 'IO, files';

then the values can be takes from the produced message as
  my $files = $msg->valueOf('files');  # returns ARRAY reference
  print @$files;              # 3
  my $count = $msg->count;    # 3
  my @class = $msg->classes;  # 'IO', 'files'
  if($msg->inClass('files'))  # true

=back

=head2 Processing

=over 4

=item $obj-E<gt>B<concat>(STRING|OBJECT, [PREPEND])

This method implements the overloading of concatenation, which is needed
to delay translations even longer.  When PREPEND is true, the STRING
or OBJECT (other C<Log::Report::Message>) needs to prepended, otherwise
it is appended.

example: of concatenation

 print __"Hello" . ' ' . __"World!";
 print __("Hello")->concat(' ')->concat(__"World!")->concat("\n");

=item $obj-E<gt>B<inClass>(CLASS|REGEX)

Returns true if the message is in the specified CLASS (string) or
matches the REGEX.  The trueth value is the (first matching) class.

=item $obj-E<gt>B<toString>([LOCALE])

Translate a message.  If not specified, the default locale is used.

=item $obj-E<gt>B<untranslated>

Return the concatenation of the prepend, msgid, and append strings.  Variable
expansions within the msgid is not performed.

=back

=head1 DETAILS

=head2 OPTIONS and VARIABLES

=head3 Interpolating

With the C<__x()> or C<__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 C<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 C<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 C< { 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.

=head3 Interpolation of OPTIONS

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

=over 4

=item _msgid

The MSGID as provided with L<Log::Report::__()|Log::Report/"Language Translations"> and L<Log::Report::__x()|Log::Report/"Language Translations">

=item _msgid, _plural, _count

The single MSGID and PLURAL MSGIDs, respectively the COUNT as used with
L<Log::Report::__n()|Log::Report/"Language Translations"> and L<Log::Report::__nx()|Log::Report/"Language Translations">

=item _textdomain

The label of the textdomain in which the translation takes place.

=item _class or _classes

Are to be used to group reports, and can be queried with L<inClass()|Log::Report::Message/"Processing">,
L<Log::Report::Exception::inClass()|Log::Report::Exception/"Processing">, or
L<Log::Report::Dispatcher::Try::wasFatal()|Log::Report::Dispatcher::Try/"Status">.

=back

=head3 Interpolation of VARIABLES

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:

=over 4

=item .

Simple scalar values are interpolated "as is"

=item .

References to SCALARs will collect the value on the moment that the
output is made.  The C<Log::Report::Message> object which is created with
the C<__xn> can be seen as a closure.  The translation can be reused.
See example below.

=item .

Code references can be used to create the data "under fly".  The
C<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.

=item .

When the value is an ARRAY, all members will be interpolated with C<$">
between the elements.

=back

=head3 Avoiding repetative translations

This way of translating is somewhat expensive, because an object to
handle the C<__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: C<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.

The L<Log::Report|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 C<_>.
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.

=head1 OVERLOADING

=over 4

=item overload: B<as function>

When the object is used to call as function, a new object is
created with the data from the original one but updated with the
new parameters.  Implemented in C<clone()>.

=item overload: B<concatenation>

An (accidental) use of concatenation (a dot where a comma should be
used) would immediately stringify the object.  This is avoided by
overloading that operation.

=item overload: B<stringification>

When the object is used in string context, it will get translated.
Implemented as L<toString()|Log::Report::Message/"Processing">.

=back

=head1 SEE ALSO

This module is part of Log-Report distribution version 0.94,
built on August 23, 2011. Website: F<http://perl.overmeer.net/log-report/>

=head1 LICENSE

Copyrights 2007-2011 by Mark Overmeer. For other contributors see ChangeLog.

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
See F<http://www.perl.com/perl/misc/Artistic.html>