From: Bill Erickson Date: Tue, 14 Aug 2018 16:51:25 +0000 (-0400) Subject: LP#1775466 XLIFF->PO->XLIFF merge tool X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=8885e1ce64105d10112dd0d0123f12520daf620f;p=working%2FEvergreen.git LP#1775466 XLIFF->PO->XLIFF merge tool Signed-off-by: Bill Erickson --- diff --git a/Open-ILS/src/eg2/src/locale/fix-po2xliff.pl b/Open-ILS/src/eg2/src/locale/fix-po2xliff.pl deleted file mode 100755 index 2c3eee2b3c..0000000000 --- a/Open-ILS/src/eg2/src/locale/fix-po2xliff.pl +++ /dev/null @@ -1,88 +0,0 @@ -#!/usr/bin/perl -use XML::LibXML; -# ------------------------------------------------------------ -# The xliff2po and po2xliff crosswalk results in message IDs -# getting stored in an unexpected location as far as Angular is -# concerned. -# -# E.g. -# -# -# Sort Priority -# Sort Priority -# -# 1f93d86f7ad773b5f4232b60fff10567179f28f4 -# -# -# -# Angular expaect the trans-unit id attribute to match the "sourcefile" -# hash from the context group. This script makes that happen. -# Result is: -# -# -# Sort Priority -# Sort Priority -# -# 1f93d86f7ad773b5f4232b60fff10567179f28f4 -# -# -# -# NOTES: -# npm run build-translations -# xliff2po -i src/locale/messages.fr-CA.xlf -o src/locale/messages.fr-CA.po -# # add translations to .po file ... -# po2xliff -i src/locale/messages.fr-CA.po -o src/locale/messages.fr-CA.from-PO.xlf -# perl src/locale/fix-po2xliff.pl src/locale/messages.fr-CA.from-PO.xlf src/locale/messages.fr-CA.xlf -# ng build --configuration=production-fr-CA ... -# ------------------------------------------------------------ - -sub usage { - print <getElementsByTagName($name)->[0]; -} - -my $infile = shift; -my $outfile = shift; - -usage() unless $infile; - -my $doc = XML::LibXML->new->parse_file($infile); - -my $body = gbn(gbn($doc->documentElement, 'file'), 'body'); - -for my $trans ($body->getElementsByTagName('trans-unit')) { - - my $source = gbn($trans, 'source'); - my $target = gbn($trans, 'target'); - my $context_group = gbn($trans, 'context-group'); - next unless $context_group; - - my $context = gbn($context_group, 'context'); - my $msg_id = $context->textContent; - $trans->setAttribute('id', $msg_id); -} - -if ($outfile) { - open OUTFILE, '>', $outfile; - print OUTFILE $doc->toString(1); -} else { - print $doc->toString(1) . "\n"; -} - - - - - diff --git a/Open-ILS/src/eg2/src/locale/merge-po2xliff-messages.pl b/Open-ILS/src/eg2/src/locale/merge-po2xliff-messages.pl new file mode 100755 index 0000000000..f650909ebf --- /dev/null +++ b/Open-ILS/src/eg2/src/locale/merge-po2xliff-messages.pl @@ -0,0 +1,97 @@ +#!/usr/bin/perl +use strict; +use warnings; +use XML::LibXML; +# --------------------------------------------------------------------- +# The xliff2po -> po2xliff round trip results in files Angular is +# not able to use for localization. The message ID / context data +# is out of sorts. +# This script extracts translations from the round-tripped xliff file +# and merges them back into the original Angular-approved xliff file. +# --------------------------------------------------------------------- + +sub usage { + print <getElementsByTagName($name)->[0]; +} + +# Create a map of message-id to translated strings. +# In the round-tripped file, the message ID is stored as file context data. +sub extract_source_translations { + + my $doc = XML::LibXML->new->parse_file($xlf_source); + + my $body = gbn(gbn($doc->documentElement, 'file'), 'body'); + + for my $trans ($body->getElementsByTagName('trans-unit')) { + + my $source = gbn($trans, 'source'); + my $target = gbn($trans, 'target'); + + # No translation, nothing to merge; + next if $source->textContent eq $target->textContent; + + my $context_group = gbn($trans, 'context-group'); + next unless $context_group; + + my $context = gbn($context_group, 'context'); + my $msg_id = $context->textContent; + $translations{$msg_id} = $target->textContent; + } +} + +# Merged strings extracted above into the xliff by matching on message id. +sub merge_target_translations { + + my $doc = XML::LibXML->new->parse_file($xlf_target); + + my $body = gbn(gbn($doc->documentElement, 'file'), 'body'); + + for my $trans ($body->getElementsByTagName('trans-unit')) { + my $msg_id = $trans->getAttribute('id'); + + my $translation = $translations{$msg_id}; + + next unless $translation; + + my $source = gbn($trans, 'source'); + my $target = gbn($trans, 'target'); + + # print "Merging translation [$msg_id] ". + # $source->textContent . " : $translation\n"; + + $target->removeChildNodes(); + $target->appendTextNode($translation); + } + + open TARGET, '>', $xlf_target + or die "Cannot open for writing: $xlf_target : $!\n"; + + print TARGET $doc->toString(1); + close TARGET; +} + +extract_source_translations(); +merge_target_translations(); +