Skip to content

Instantly share code, notes, and snippets.

@kgoess
Created June 18, 2019 17:20
Show Gist options
  • Save kgoess/beac70c2d906e3263c01ce5e87235a97 to your computer and use it in GitHub Desktop.
Save kgoess/beac70c2d906e3263c01ce5e87235a97 to your computer and use it in GitHub Desktop.
Adds support for concatenation and template literals
diff -u -Nar -U20 Locale-TextDomain-OO-Extract-2.015.orig/lib/Locale/TextDomain/OO/Extract/JavaScript.pm Locale-TextDomain-OO-Extract-2.015/lib/Locale/TextDomain/OO/Extract/JavaScript.pm
--- Locale-TextDomain-OO-Extract-2.015.orig/lib/Locale/TextDomain/OO/Extract/JavaScript.pm 2017-09-28 01:02:36.000000000 -0700
+++ Locale-TextDomain-OO-Extract-2.015/lib/Locale/TextDomain/OO/Extract/JavaScript.pm 2019-06-17 16:54:46.000000000 -0700
@@ -34,70 +34,98 @@
return $condition ? @list : ();
};
my $with_bracket = join "\n| ", (
$list_if->('Gettext', '__? n? p? x?',
'n? p? gettext'),
$list_if->('Gettext::DomainAndCategory', '__? d? c? n? p? x?',
'd? c? n? p? gettext'),
$list_if->('Gettext::Loc', 'loc_ n? p? x?'),
$list_if->('Gettext::Loc::DomainAndCategory', 'loc_ d? c? n? p? x?'),
$list_if->('BabelFish::Loc', 'loc_b p?'),
$list_if->('BabelFish::Loc::DomainAndCategory', 'loc_b d? c? p?'),
);
$with_bracket ||= '(?!)';
return qr{
\b N?
(?: $with_bracket ) \s* [(]
}xms;
}
+my $string_regex =
+ # Qtext with 0 .. n escaped charsQ
+ q{
+ \s* [Q]
+ (?<NUMNAME>
+ [^\\\\Q]* # normal text
+ (?: \\\\ . [^\\\\Q]* )* # maybe followed by escaped char and normal text
+ )
+ [Q]
+ };
+
+my $double_quote_string = $string_regex;
+$double_quote_string =~ s/Q/"/g;
+$double_quote_string =~ s/NUM/first/g;
+$double_quote_string =~ s/NAME/doubleq/g;
+$double_quote_string = qr{$double_quote_string}x;
+my $double_quote_string_2 = $string_regex;
+$double_quote_string_2 =~ s/Q/"/g;
+$double_quote_string_2 =~ s/NUM/second/g;
+$double_quote_string_2 =~ s/NAME/doubleq/g;
+$double_quote_string_2 = qr{$double_quote_string_2}x;
+
+
+my $single_quote_string = $string_regex;
+$single_quote_string =~ s/Q/'/g;
+$single_quote_string =~ s/NUM/first/g;
+$single_quote_string =~ s/NAME/singleq/g;
+$single_quote_string = qr{$single_quote_string}x;
+my $single_quote_string_2 = $string_regex;
+$single_quote_string_2 =~ s/Q/'/g;
+$single_quote_string_2 =~ s/NUM/second/g;
+$single_quote_string_2 =~ s/NAME/singleq/g;
+$single_quote_string_2 = qr{$single_quote_string_2}x;
+
+my $backtick_quote_string = $string_regex;
+$backtick_quote_string =~ s/Q/`/g;
+$backtick_quote_string =~ s/NUM/first/g;
+$backtick_quote_string =~ s/NAME/backtickq/g;
+$backtick_quote_string = qr{$backtick_quote_string}x;
+my $backtick_quote_string_2 = $string_regex;
+$backtick_quote_string_2 =~ s/Q/`/g;
+$backtick_quote_string_2 =~ s/NUM/second/g;
+$backtick_quote_string_2 =~ s/NAME/backtickq/g;
+$backtick_quote_string_2 = qr{$backtick_quote_string_2}x;
+
+
my $category_rule
= my $context_rule
= my $domain_rule
= my $plural_rule
= my $singular_rule
= my $text_rule
= [
- [
- # "text with 0 .. n escaped chars"
- qr{
- \s* ["]
- (
- [^\\"]* # normal text
- (?: \\ . [^\\"]* )* # maybe followed by escaped char and normal text
- )
- ["]
- }xms,
- ],
+ [ $double_quote_string ],
'or',
- [
- # 'text with 0 .. n escaped chars'
- qr{
- \s* [']
- (
- [^\\']* # normal text
- (?: \\ . [^\\']* )* # maybe followed by escaped char and normal text
- )
- [']
- }xms,
- ],
+ [ $single_quote_string ],
+ 'or',
+ [ $backtick_quote_string ],
];
my $comma_rule = qr{ \s* [,] }xms;
my $count_rule = qr{ \s* ( [^,)]+ ) }xms;
my $close_rule = qr{ \s* [,]? \s* ( [^)]* ) [)] }xms;
my $rules = [
# loc_, _, __
[
'begin',
qr{ \b N? (?: loc_ | __? ) ( x? ) \s* [(] }xms,
'and',
$text_rule,
'and',
$close_rule,
'end',
],
'or',
[
'begin',
qr{ \b N? (?: loc_ | __? ) ( n x? ) \s* [(] }xms,
@@ -754,53 +782,65 @@
$singular_rule,
'and',
$comma_rule,
'and',
$plural_rule,
'and',
$comma_rule,
'and',
$count_rule,
'and',
$comma_rule,
'and',
$category_rule,
'and',
$close_rule,
'end',
],
];
# remove comment code
+# concatenate any strings
sub preprocess {
my $self = shift;
my $content_ref = $self->content_ref;
${$content_ref} =~ s{ // [^\n]* $ }{}xmsg;
${$content_ref} =~ s{
/ [*] ( .*? ) [*] /
}
{
join q{}, $1 =~ m{ ( \n ) }xmsg;
}xmsge;
+ no warnings 'uninitialized';
+ 1 while ($$content_ref =~ s{
+ (?:$double_quote_string|$single_quote_string|$backtick_quote_string)
+ \s* \+ \s*
+ (?:$double_quote_string_2|$single_quote_string_2$backtick_quote_string_2)
+ }
+ {
+ "$+{firstdoubleq}$+{firstsingleq}$+{firstbacktickq}$+{seconddoubleq}$+{secondsingleq}$+{secondbacktickq}"
+ }gx);
+
+
return $self;
}
sub interpolate_escape_sequence {
my ( undef, $string ) = @_;
# nothing to interpolate
defined $string
or return $string;
my %char_of = (
b => "\b",
f => "\f",
n => "\n",
r => "\r",
t => "\t",
);
## no critic (ComplexRegexes)
$string =~ s{
\\
diff -u -Nar -U20 Locale-TextDomain-OO-Extract-2.015.orig/t/13_extract_js.t Locale-TextDomain-OO-Extract-2.015/t/13_extract_js.t
--- Locale-TextDomain-OO-Extract-2.015.orig/t/13_extract_js.t 2017-08-21 01:31:45.000000000 -0700
+++ Locale-TextDomain-OO-Extract-2.015/t/13_extract_js.t 2019-06-17 16:51:08.000000000 -0700
@@ -218,40 +218,41 @@
automatic => 'count',
reference => {
'jsgettext.js:22' => undef,
},
},
'some string' => {
reference => {
'jsgettext.js:3' => undef,
'jsgettext.js:4' => undef,
'jsgettext.js:5' => undef,
},
},
text => {
reference => {
'jsgettext.js:7' => undef,
},
},
'this will get translated' => {
reference => {
'jsgettext.js:6' => undef,
+ 'jsgettext.js:93' => undef,
},
},
},
'i-default::TEXTDOMAIN' => {
q{} => {
msgstr => {
nplurals => 2,
plural => 'n != 1',
},
},
'MSGID d' => {
reference => {
'jsgettext.js:76' => undef,
},
},
"MSGID dn\x00PLURAL dn" => {
automatic => 'COUNT',
reference => {
'jsgettext.js:77' => undef,
},
diff -u -Nar -U20 Locale-TextDomain-OO-Extract-2.015.orig/t/files_to_extract/jsgettext.js Locale-TextDomain-OO-Extract-2.015/t/files_to_extract/jsgettext.js
--- Locale-TextDomain-OO-Extract-2.015.orig/t/files_to_extract/jsgettext.js 2017-07-21 09:06:58.000000000 -0700
+++ Locale-TextDomain-OO-Extract-2.015/t/files_to_extract/jsgettext.js 2019-06-17 16:50:35.000000000 -0700
@@ -70,20 +70,27 @@
gettext ( 'MSGID %0 %1', 'placeholder 0', 'placeholder 1' );
ngettext ( 'MSGID n', 'PLURAL n', COUNT );
pgettext ( 'MSGCTXT', 'MSGID p' );
npgettext ( 'MSGCTXT', 'MSGID np', 'PLURAL np', COUNT );
dgettext ( 'TEXTDOMAIN', 'MSGID d' );
dngettext ( 'TEXTDOMAIN', 'MSGID dn', 'PLURAL dn', COUNT );
dpgettext ( 'TEXTDOMAIN', 'MSGCTXT', 'MSGID dp' );
dnpgettext ( 'TEXTDOMAIN', 'MSGCTXT', 'MSGID dpn', 'PLURAL dpn', COUNT );
cgettext ( 'MSGID c', 'CATEGORY' );
cngettext ( 'MSGID cn', 'PLURAL cn', COUNT, 'CATEGORY' );
cpgettext ( 'MSGCTXT', 'MSGID cp', 'CATEGORY' );
cnpgettext ( 'MSGCTXT', 'MSGID cnp', 'PLURAL cnp', COUNT, 'CATEGORY' );
dcgettext ( 'TEXTDOMAIN', 'MSGID dc', 'CATEGORY' );
dcngettext ( 'TEXTDOMAIN', 'MSGID dcn', 'PLURAL dcn', COUNT, 'CATEGORY' );
dcpgettext ( 'TEXTDOMAIN', 'MSGCTXT', 'MSGID dcp', 'CATEGORY' );
dcnpgettext( 'TEXTDOMAIN', 'MSGCTXT', 'MSGID dcnp', 'PLURAL dcnp', COUNT, 'CATEGORY' );
+
+// adding this down here at the bottom just to avoid changing the
+// rest of the reported line numbers
+var myString = this._("this " +
+ 'will ' +
+ `get ` +
+ "translated");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment