NAME
Perl::Critic::Policy::Miscellanea::TextDomainPlaceholders - check placeholder names in Locale::TextDomain calls
DESCRIPTION
This policy is part of the Perl::Critic::Pulp addon. It checks the placeholder arguments in format strings to the __x
, __nx
and __xn
functions from Locale::TextDomain
. Any formats with a key missing from the args, or args which are unused by the format, are reported.
print __x('Searching for {data}', # bad
datum => 123);
print __nx('Read one file', # bad
'Read {num} files',
$n,
count => 123);
This sort of thing is usually a mistake, so this policy is under the bugs
theme. An error can fairly easily go unnoticed since (as of TextDomain version 1.16) a placeholder without a corresponding arg merely goes through unexpanded and any extra args are ignored.
The way TextDomain is setup actually allows anything between "{ }
" as a key string, but for the purposes of this policy only symbol characters "a-zA-Z0-9_" are taken to be a key. This is almost certainly what you'll want to use anyway, and it makes it possible to include literal braces in the string without tickling this policy all the time.
LIMITATIONS
If the format string is not a literal then it might use any args, so all are considered used.
# ok, 'datum' might be used
__x($my_format, datum => 123);
Literal portions of the format are still checked.
# bad, 'foo' not present in args
__x("{foo} $bar, datum => 123);
Conversely, if the args have some non-literals then they could be anything, so everything in the format string is considered present.
# ok, $something might be 'world'
__x('hello {world}', $something => 123);
Literal args are still checked.
# bad, 'blah' is not used
__x('hello {world}', $something => 123, blah => 456);
If there's both a non-literal in the format and in the args then nothing is checked, since it could match up fine at runtime.
SEE ALSO
Perl::Critic::Pulp, Perl::Critic, Locale::TextDomain
HOME PAGE
http://www.geocities.com/user42_kevin/perl-critic-pulp/index.html
COPYRIGHT
Copyright 2008 Kevin Ryde
Perl-Critic-Pulp is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3, or (at your option) any later version.
Perl-Critic-Pulp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with Perl-Critic-Pulp. If not, see http://www.gnu.org/licenses.