Skip to content

Commit

Permalink
Item14080: implement argN parameters in MAKETEXT
Browse files Browse the repository at this point in the history
These are an alternative to the existing 'args' parameter which is split at
commas, so that you can't use commas in the individual parameters themselves.
This introduces 'arg1', 'arg2', ... as an optional alternative you can use if
you absolutely do need commas, or just prefer separate arguments.
  • Loading branch information
jast committed May 31, 2016
1 parent 0b5401d commit 0d38c5f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
10 changes: 9 additions & 1 deletion core/data/System/VarMAKETEXT.txt
@@ -1,4 +1,4 @@
%META:TOPICINFO{author="ProjectContributor" date="1434650530" format="1.1" version="1"}%
%META:TOPICINFO{author="ProjectContributor" date="1464681281" format="1.1" version="1"}%
%META:TOPICPARENT{name="Macros"}%
---+ MAKETEXT -- creates text using Foswiki's <nop>I18N infrastructure
Strings captured in the =MAKETEXT= macro are automatically mapped to the
Expand All @@ -8,6 +8,7 @@ current user's selected language via =locale/*.po= translation files.
| *Parameter* | *Description* | *Default* |
| ="text"= <br /> =string="text"= | The text to be displayed (the _translatable string_). | |
| =args= | a comma-separated list of arguments to be interpolated in the string, replacing =[_N]= placeholders in it. | |
| =arg1, arg2, ...= | separate arguments that may contain commas, for use instead of =args= if necessary | |
---++ Examples
* =%<nop>MAKETEXT{string="Edit"}%= expands to =%MAKETEXT{string="Edit"}%=
* =%<nop>MAKETEXT{"If you have any questions, please contact [_1]." args="%<nop>WIKIWEBMASTER%"}%= expands to =%MAKETEXT{"If you have any questions, please contact [_1]."
Expand All @@ -17,9 +18,16 @@ current user's selected language via =locale/*.po= translation files.
"Did you want to [[[_1]][reset [_2]'s password]]?"
args="%SYSTEMWEB%.ResetPassword,%WIKIUSERNAME%"
}%=
* =%<nop>MAKETEXT{"Did you want to [<nop>[<nop>[_1]][reset the password of [_2]]]?" arg1="%<nop>SYSTEMWEB%.ResetPassword" arg2="%<nop>WIKIUSERNAME%, master of the universe"}%= expands to =%MAKETEXT{
"Did you want to [[[_1]][reset the password of [_2]]]?"
arg1="%SYSTEMWEB%.ResetPassword"
arg2="%WIKIUSERNAME%, master of the universe"
}%=
---+++ Notes
* =[_n]= brackets are validated to a positive integer from 1 to 100.
* Missing arguments are replaced with an empty string ''.
* If you use the =args= parameter, any =argN= parameters will be
ignored.
* An ampersand (<code>&amp;</code>) followed by one ascii alphabetic
character (a...z, A...Z) in the translatable string will be expanded
to an access key string. For example, =&amp;X= will expand to
Expand Down
19 changes: 14 additions & 5 deletions core/lib/Foswiki/Macros/MAKETEXT.pm
Expand Up @@ -46,11 +46,20 @@ s/~\[(\*,\_(\d+),[^,]+(,([^,]+))?)~\]/ _validate($1, $2, $max, $min, $param_erro
&& $Locale::Maketext::VERSION
&& $Locale::Maketext::VERSION < 1.23 ); # escape any escapes

my @args = split( /\s*,\s*/, $argsStr );

# fill omitted args with empty strings
while ( ( scalar(@args) ) < $max ) {
push( @args, '' );
my @args;
if ( defined $params->{args} ) {
@args = split( /\s*,\s*/, $argsStr );

# fill omitted args with empty strings
while ( ( scalar(@args) ) < $max ) {
push( @args, '' );
}
}
else {
my $idx = 0;
while ( ++$idx <= $max ) {
push( @args, $params->{"arg$idx"} || '' );
}
}

# do the magic:
Expand Down

0 comments on commit 0d38c5f

Please sign in to comment.