Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new feature to Translate(): Replace/Translate parameters #494

Open
wants to merge 6 commits into
base: dev
Choose a base branch
from
8 changes: 8 additions & 0 deletions Kernel/Language.pm
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,16 @@ sub Translate {

$Text = $Self->{Translation}->{$Text} || $Text;

my $SubRef;

if ( ref $Text && 'ARRAY' eq ref $Text ) {
($Text, $SubRef) = @{ $Text || [] };
}

return $Text if !@Parameters;

@Parameters = $SubRef->( @Parameters ) if $SubRef && ref $SubRef && 'CODE' eq ref $SubRef;

for my $Count ( 0 .. $#Parameters ) {
return $Text if !defined $Parameters[$Count];
$Text =~ s/\%(s|d)/$Parameters[$Count]/;
Expand Down
15 changes: 15 additions & 0 deletions scripts/test/Language.t
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,19 @@ for my $Test (@Tests) {
);
}

{
$LanguageObject->{Translation}->{"This is a test for a new feature in Translate(). - %s %s"} = [ 'A simple Test - %s %s', \&ReplaceParams ];
my $Translated = $LanguageObject->Translate("This is a test for a new feature in Translate(). - %s %s", "Znuny", "rocks!");

$Self->Is(
$Translated,
'A simple Test - Perl is great!',
'Replaced params for translated string',
);

sub ReplaceParams {
return "Perl", "is great!";
}
}

1;
Loading