LP#1775466 XLIFF->PO->XLIFF crosswalk repair tool
authorBill Erickson <berickxx@gmail.com>
Mon, 13 Aug 2018 21:45:20 +0000 (17:45 -0400)
committerBill Erickson <berickxx@gmail.com>
Mon, 13 Aug 2018 21:47:36 +0000 (17:47 -0400)
Script to repair message-id chaos caused by crosswalking between PO and
XLIFF files for translations.  Script duplicates the message context value
into the id attribute on the translation unit, to match Angular's
expectations.

Signed-off-by: Bill Erickson <berickxx@gmail.com>
Open-ILS/src/eg2/src/locale/.gitkeep [deleted file]
Open-ILS/src/eg2/src/locale/fix-po2xliff.pl [new file with mode: 0755]

diff --git a/Open-ILS/src/eg2/src/locale/.gitkeep b/Open-ILS/src/eg2/src/locale/.gitkeep
deleted file mode 100644 (file)
index e69de29..0000000
diff --git a/Open-ILS/src/eg2/src/locale/fix-po2xliff.pl b/Open-ILS/src/eg2/src/locale/fix-po2xliff.pl
new file mode 100755 (executable)
index 0000000..2c3eee2
--- /dev/null
@@ -0,0 +1,88 @@
+#!/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.
+#
+#  <trans-unit xml:space="preserve" id="52" approved="no">
+#    <source>Sort Priority</source>
+#    <target state="needs-translation">Sort Priority</target>
+#    <context-group name="po-reference" purpose="location">
+#      <context context-type="sourcefile">1f93d86f7ad773b5f4232b60fff10567179f28f4</context>
+#    </context-group>
+#  </trans-unit>
+#
+#  Angular expaect the trans-unit id attribute to match the "sourcefile"
+#  hash from the context group.  This script makes that happen.
+#  Result is:
+#
+#  <trans-unit xml:space="preserve" id="1f93d86f7ad773b5f4232b60fff10567179f28f4" approved="no">
+#    <source>Sort Priority</source>
+#    <target state="needs-translation">Sort Priority</target>
+#    <context-group name="po-reference" purpose="location">
+#      <context context-type="sourcefile">1f93d86f7ad773b5f4232b60fff10567179f28f4</context>
+#    </context-group>
+#  </trans-unit>
+#
+#  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 <<DOCS;
+        Synopsis:
+
+            $0 messages.fr-CA.from-PO.xlf messages.fr-CA.xlf
+
+            1. First param is input file.
+            2. Second param is output file.  Defaults to STDOUT.
+DOCS
+    exit(0);
+}
+
+# Returns the first element by tag name that's a child of $node.
+sub gbn {
+    my ($node, $name) = @_;
+    return $node->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";
+}
+
+
+
+
+