Skip to content

Commit

Permalink
Issue #3658: Added generalized sub to build chained translations.
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanhaerter committed Aug 1, 2024
1 parent 676f535 commit 12be21a
Showing 1 changed file with 86 additions and 3 deletions.
89 changes: 86 additions & 3 deletions Kernel/System/Translations.pm
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ use Kernel::System::ModuleRefresh ();
our @ObjectDependencies = (
'Kernel::Config',
'Kernel::System::DateTime',
'Kernel::System::DB',
'Kernel::System::DynamicField',
'Kernel::System::DynamicField::Backend',
'Kernel::System::Encode',
'Kernel::System::Main',
'Kernel::System::Log',
'Kernel::System::DB',
'Kernel::System::DynamicField',
'Kernel::System::Main',
'Kernel::System::Queue',
'Kernel::System::Service',
);

=head1 NAME
Expand Down Expand Up @@ -909,4 +911,85 @@ sub GetTranslationUniqueValues {

}

=head2 TranslateChainedElements()
generate chained translations automatically based on translations of single elements
my $Success = $TranslationsObject->TranslateChainedElements(
LanguageID => 'en',
Strings => [
'Test1',
'Test1::Test2',
],
);
=cut

sub TranslateChainedElements {
my ( $Self, %Param ) = @_;

# check needed parameters
for my $Needed (qw(LanguageID Strings)) {
if ( !$Param{$Needed} ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message => "Need $Needed!",
);
return;
}
}

# create local language object
my $LocalLanguageObject = $Kernel::OM->Create(
'Kernel::Language',
ObjectParams => {
UserLanguage => $Param{LanguageID},
},
);
my $DeployLanguage = 0;

STRING:
for my $String ( $Param{Strings}->@* ) {

# split chained strings into individual elements
my @NameElements = split /::/, $String;
my @TranslatedElements;
for my $NameElement (@NameElements) {

# translate individual elements
push @TranslatedElements, $LocalLanguageObject->Translate($NameElement);
}
my $TranslatedString = join( '::', @TranslatedElements );

# check if translation has changed to prevent recursive deployment
if ( $TranslatedString ne $LocalLanguageObject->Translate($String) ) {

my $Success = $Self->DraftTranslationsAdd(
Language => $Param{LanguageID},
Content => $String,
Translation => $TranslatedString,
UserID => 1,
Edit => 1,
);
if ( !$Success ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message => "Not able to add translation for string $String in language $Param{LanguageID}!",
);
next STRING;
}
$DeployLanguage = 1;
}
}

# check if language need to be deployed to prevent recursive deployment
if ($DeployLanguage) {
my $DeploySuccess = $Self->WriteTranslationFile(
UserLanguage => $Param{LanguageID},
);
}

return 1;
}

1;

0 comments on commit 12be21a

Please sign in to comment.