Re: [PATCH v2 03/11] i18n: add--interactive: mark strings with interpolation for translation
- From
Jakub Narębski <jnareb@gmail.com>
- Date
- Sep 30, 2016, 17:52 UTC
- Message-ID
- <b8c25ae3-143c-3e9b-0c7b-115a9b4756ae@gmail.com>
- In-Reply-To
- <1472646690-9699-4-git-send-email-vascomalmeida@sapo.pt>
W dniu 31.08.2016 o 14:31, Vasco Almeida pisze:
> Use of sprintf following die or error_msg is necessary for placeholder > substitution take place.
No, it is not. Though I don't think that we have in out Git::I18N the support for Perl i18n placeholder substitution.
From gettext manual: https://www.gnu.org/software/gettext/manual/gettext.html#perl_002dformat
15.3.16 Perl Format Strings
There are two kinds format strings in Perl: those acceptable to the Perl built-in function printf, labelled as ‘perl-format’, and those acceptable to the libintl-perl function __x, labelled as ‘perl-brace-format’.
Perl printf format strings are described in the sprintf section of ‘man perlfunc’.
Perl brace format strings are described in the Locale::TextDomain(3pm)
manual page of the CPAN package libintl-perl. In brief, Perl format uses
placeholders put between braces (‘{’ and ‘}’). The placeholder must have
the syntax of simple identifiers.
Git doesn't use Locale::TextDomain, from what I understand, to provide
fallback in no-gettext case. Also, Locale::TextDomain is not in core.The syntax, with the help of shorthand helper function, looks like this: http://search.cpan.org/dist/libintl-perl/lib/Locale/TextDomain.pm#EXPORTED_FUNCTIONS https://metacpan.org/pod/Locale::TextDomain#EXPORTED-FUNCTIONS
__x MSGID, ID1 => VAL1, ID2 => VAL2, ... ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ One of the nicest features in Perl is its capability to interpolate variables into strings:
print "This is the $color $thing.\n";
This nice feature might con you into thinking that you could now write
print __"This is the $color $thing.\n";
[But this doesn't work...]
[...] The Perl backend to GNU gettext has defined an alternative format [to using printf / sprintf] for interpolatable strings:
"This is the {color} {thing}.\n";Instead of Perl variables you use place-holders (legal Perl variables are also legal place-holders) in curly braces, and then you call
print __x ("This is the {color} {thing}.\n",
thing => $thang,
color => $color);Show 15 quoted lines
> Signed-off-by: Vasco Almeida <vascomalmeida@sapo.pt>
> ---
> git-add--interactive.perl | 26 ++++++++++++++------------
> 1 file changed, 14 insertions(+), 12 deletions(-)
>
> diff --git a/git-add--interactive.perl b/git-add--interactive.perl
> index e11a33d..4e1e857 100755
> --- a/git-add--interactive.perl
> +++ b/git-add--interactive.perl
> @@ -612,12 +612,12 @@ sub list_and_choose {
> else {
> $bottom = $top = find_unique($choice, @stuff);
> if (!defined $bottom) {
> - error_msg "Huh ($choice)?\n";
> + error_msg sprintf(__("Huh (%s)?\n"), $choice);So this would be, self explained without need of comment for translators:
+ error_msg __x ("Huh ({choice})?\n"), choice => $choice);> next TOPLOOP; > }
Though this is probably more work that you wanted to do. The __x might be defined like this (borrowing from Locale::TextDomain), which needs to be put into perl/Git/I18N.pm
sub __ ($); sub __expand ($%);
# With interpolation.
sub __x ($@)
{
my ($msgid, %vars) = @_; return __expand (__($msgid), %vars);
}
sub __expand ($%)
{
my ($translation, %args) = @_;
my $re = join '|', map { quotemeta $_ } keys %args;
$translation =~ s/\{($re)\}/defined $args{$1} ? $args{$1} : "{$1}"/ge;return $translation; }
Best regards,
-- Jakub Narębski