From c4150e875a0531fe2ff5de4f18c53d68088cc211 Mon Sep 17 00:00:00 2001 From: David Planella Date: Mon, 12 Mar 2012 20:20:59 +0100 Subject: First shot at adding extraction support for Qt Designer .ui files --- intltool-extract.in | 31 ++++- tests/cases/extract-qtdesigner.ui | 210 ++++++++++++++++++++++++++++++++++ tests/results/extract-qtdesigner.ui.h | 13 +++ 3 files changed, 253 insertions(+), 1 deletion(-) create mode 100644 tests/cases/extract-qtdesigner.ui create mode 100644 tests/results/extract-qtdesigner.ui.h diff --git a/intltool-extract.in b/intltool-extract.in index 5afc292..33bb447 100644 --- a/intltool-extract.in +++ b/intltool-extract.in @@ -165,7 +165,7 @@ XML files. Read manpage (man ${PROGRAM}) for more info. "gettext/glade", "gettext/ini", "gettext/keys" "gettext/rfc822deb", "gettext/schemas", "gettext/gsettings", "gettext/xml", "gettext/quoted", - "gettext/quotedxml", "gettext/tlk" + "gettext/quotedxml", "gettext/tlk", "gettext/qtdesigner" -l, --local Writes output into current working directory (conflicts with --update) --update Writes output into the same directory the source file @@ -225,6 +225,7 @@ sub convert { &type_quoted if $gettext_type eq "quoted"; &type_quotedxml if $gettext_type eq "quotedxml"; &type_tlk if $gettext_type eq "tlk"; + &type_qtdesigner if $gettext_type eq "qtdesigner"; } sub entity_decode_minimal @@ -828,6 +829,34 @@ sub type_quotedxml { } } +sub type_qtdesigner { + ### For translatable Qt Designer XML files ### + # From http://qt-project.org/doc/designer-ui-file-format.html and + # http://qt-project.org/doc/linguist-ts-file-format.html + # + # tag attributes: + # + # notr="true" means the string is not translatable + # extracomment maps to a developer comment in gettext + # comment corresponds to "disambiguation" in the Qt Linguist API, and maps + # to msgctxt in gettext + + # $1 $2 $3 $4 + while ($input =~ /]*notr\s*=\s*"([^"]*)")?(?:\s+[^>]*\bcomment\b\s*=\s*"([^"]*)")?(?:\s+[^>]*extracomment\s*=\s*"([^"]*)")?[^>]*>([^<]+)<\/string>/sg) { + if (defined($1)){ + next; + } + my $message = entity_decode($4); + if (defined($2)) { + $message = entity_decode($2) . "\004" . $message; + } + add_message($message); + if (defined($3)) { + $comments{$message} = entity_decode($3) ; + } + } +} + sub type_glade { ### For translatable Glade XML files ### diff --git a/tests/cases/extract-qtdesigner.ui b/tests/cases/extract-qtdesigner.ui new file mode 100644 index 0000000..043e006 --- /dev/null +++ b/tests/cases/extract-qtdesigner.ui @@ -0,0 +1,210 @@ + + + main + + + + 0 + 0 + 640 + 457 + + + + + 0 + 0 + + + + Form + + + + 0 + + + 0 + + + + + 0 + + + + Tab 1 + + + + 0 + + + 0 + + + + + 0 + + + + + + 0 + 0 + + + + + 0 + 100 + + + + + 0 + + + 0 + + + + + 0 + + + + + + + 30 + 20 + 330 + 51 + + + + + Ubuntu + 21 + + + + Intltool extract text + + + + + + + + + + + + + + 0 + 0 + + + + + 0 + 0 + + + + + + 30 + 20 + 571 + 181 + + + + QFrame::NoFrame + + + QFrame::Plain + + + true + + + <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd"> +<html><head><meta name="qrichtext" content="1" /><title>HTML document extraction test</title><style type="text/css"> +p, li { white-space: pre-wrap; } +</style></head><body style=" font-family:'Ubuntu'; font-size:11pt; font-weight:400; font-style:normal;"> +<p style=" margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">Once upon a time, in a galaxy far, far away... </p></body></html> + + + + + + 30 + 210 + 571 + 31 + + + + + 9 + + + + Ok + + + + + + 30 + 280 + 321 + 20 + + + + + Ubuntu + + + + Don't show me this message in the future + + + + + + 30 + 260 + 321 + 20 + + + + + Ubuntu + + + + Require authentication + + + + + + + + + + + + + + + diff --git a/tests/results/extract-qtdesigner.ui.h b/tests/results/extract-qtdesigner.ui.h new file mode 100644 index 0000000..6f5bed8 --- /dev/null +++ b/tests/results/extract-qtdesigner.ui.h @@ -0,0 +1,13 @@ +char *s = N_("Form"); +/* This is a comment (extracomment) */ +char *s = N_("Intltool extract text"); +char *s = N_("\n" + "HTML document extraction test\n" + "

Once upon a time, in a galaxy far, far away...

"); +/* Refers to the action of accepting something */ +char *s = C_("Button", "Ok"); +char *s = C_("WelcomeDialog", "Don't show me this message in the future"); +/* Authentication is required to perform this action */ +char *s = N_("Require authentication"); -- cgit v1.2.1 From 9c69b7fc7966f92a1da20e8a18abcf1ebff6c97e Mon Sep 17 00:00:00 2001 From: David Planella Date: Mon, 12 Mar 2012 20:28:51 +0100 Subject: Properly added the qtdesigner test to be run with make check now --- tests/selftest.pl.in | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/selftest.pl.in b/tests/selftest.pl.in index e27d965..5f9c7c3 100755 --- a/tests/selftest.pl.in +++ b/tests/selftest.pl.in @@ -289,6 +289,11 @@ $case = "gsettings.gschema.xml"; system("$INTLTOOL_EXTRACT --type=gettext/gsettings --quiet --update cases/$case") == 0 or $failed = 1; check_extract_result($case); +print "39. Extract messages from a Qt Designer UI file: "; +$case = "extract-qtdesigner.ui"; +system("$INTLTOOL_EXTRACT --type=gettext/qtdesigner --quiet --update cases/$case") == 0 or $failed = 1; +check_extract_result($case); + system("rm -f cases/*.*") if $srcdir ne "."; system("rm -rf C az extract9 fr fr_BE fr_FR merge6 schemasmerge1 schemasmerge2 spacepreserve test test-quoted merge6.xml"); -- cgit v1.2.1 From bdc3acd8ac486617245d9bb4de7d5f4e801e4ebe Mon Sep 17 00:00:00 2001 From: David Planella Date: Mon, 28 Jan 2013 00:45:01 +0100 Subject: Cleaned up qtdesigner code, added comments, updated test case and results --- intltool-extract.in | 33 +++++++++++++++++++++++++++------ tests/cases/extract-qtdesigner.ui | 8 ++++---- tests/results/extract-qtdesigner.ui.h | 8 ++++---- 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/intltool-extract.in b/intltool-extract.in index 33bb447..610b5e3 100644 --- a/intltool-extract.in +++ b/intltool-extract.in @@ -840,19 +840,40 @@ sub type_qtdesigner { # extracomment maps to a developer comment in gettext # comment corresponds to "disambiguation" in the Qt Linguist API, and maps # to msgctxt in gettext + # + # Example: + # + # Ok + + # Read all tags one by one and extract the translatable messages + # within them + while ($input =~ /]*notr\s*=\s*"([^"]*)")? + (?:\s+[^>]*\bcomment\b\s*=\s*"([^"]*)")? + (?:\s+[^>]*extracomment\s*=\s*"([^"]*)")? + [^>]*>([^<]+) + <\/string>/xsg) { + + # Skip string if notr="true" (i.e. string marked as non-translatable) + if (defined($1) && $1 eq "true") { + next; + } - # $1 $2 $3 $4 - while ($input =~ /]*notr\s*=\s*"([^"]*)")?(?:\s+[^>]*\bcomment\b\s*=\s*"([^"]*)")?(?:\s+[^>]*extracomment\s*=\s*"([^"]*)")?[^>]*>([^<]+)<\/string>/sg) { - if (defined($1)){ - next; - } + # Read translatable message my $message = entity_decode($4); + + # Add msgctxt to the translatable message if present if (defined($2)) { $message = entity_decode($2) . "\004" . $message; } + + # Add message to the array of translatable messages (msgids) add_message($message); + + # Add developer comment to the comments hash if (defined($3)) { - $comments{$message} = entity_decode($3) ; + $comments{$message} = entity_decode($3); } } } diff --git a/tests/cases/extract-qtdesigner.ui b/tests/cases/extract-qtdesigner.ui index 043e006..7c16560 100644 --- a/tests/cases/extract-qtdesigner.ui +++ b/tests/cases/extract-qtdesigner.ui @@ -33,7 +33,7 @@ - Tab 1 + Do not translate this @@ -91,7 +91,7 @@ - Intltool extract text + This is a translatable title @@ -156,7 +156,7 @@ p, li { white-space: pre-wrap; } - Ok + Ok @@ -192,7 +192,7 @@ p, li { white-space: pre-wrap; } - Require authentication + Require authentication diff --git a/tests/results/extract-qtdesigner.ui.h b/tests/results/extract-qtdesigner.ui.h index 6f5bed8..a9b997c 100644 --- a/tests/results/extract-qtdesigner.ui.h +++ b/tests/results/extract-qtdesigner.ui.h @@ -1,13 +1,13 @@ char *s = N_("Form"); -/* This is a comment (extracomment) */ -char *s = N_("Intltool extract text"); +/* TRANSLATORS: this is the main title of the authentication dialog */ +char *s = N_("This is a translatable title"); char *s = N_("\n" "HTML document extraction test\n" "

Once upon a time, in a galaxy far, far away...

"); -/* Refers to the action of accepting something */ +/* TRANSLATORS: refers to the action of accepting something */ char *s = C_("Button", "Ok"); char *s = C_("WelcomeDialog", "Don't show me this message in the future"); -/* Authentication is required to perform this action */ +/* TRANSLATORS: authentication is required to perform this action */ char *s = N_("Require authentication"); -- cgit v1.2.1 From 0b6d9c44c7e68c83476886307a609de871705476 Mon Sep 17 00:00:00 2001 From: David Planella Date: Thu, 9 May 2013 22:17:55 +0200 Subject: Updated links to Qt translation format specs --- intltool-extract.in | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/intltool-extract.in b/intltool-extract.in index caf7f7b..779f268 100644 --- a/intltool-extract.in +++ b/intltool-extract.in @@ -831,8 +831,11 @@ sub type_quotedxml { sub type_qtdesigner { ### For translatable Qt Designer XML files ### - # From http://qt-project.org/doc/designer-ui-file-format.html and - # http://qt-project.org/doc/linguist-ts-file-format.html + # + # Specs: + # + # - http://qt-project.org/doc/qt-5.0/qtlinguist/linguist-ts-file-format.html + # - http://qt-project.org/doc/qt-5.0/qtdesigner/designer-ui-file-format.html # # tag attributes: # -- cgit v1.2.1 From 751b16f314e021f84c0a6fe554c33aa3ec07c3f1 Mon Sep 17 00:00:00 2001 From: David Planella Date: Fri, 10 May 2013 03:36:38 +0200 Subject: Updated test case for Qt Designer extraction: no escapes --- tests/results/extract-qtdesigner.ui.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/results/extract-qtdesigner.ui.h b/tests/results/extract-qtdesigner.ui.h index a9b997c..dd85ea1 100644 --- a/tests/results/extract-qtdesigner.ui.h +++ b/tests/results/extract-qtdesigner.ui.h @@ -1,11 +1,11 @@ char *s = N_("Form"); /* TRANSLATORS: this is the main title of the authentication dialog */ char *s = N_("This is a translatable title"); -char *s = N_("\n" - "HTML document extraction test\n" - "

Once upon a time, in a galaxy far, far away...

"); + "</style></head><body style=" font-family:'Ubuntu'; font-size:11pt; font-weight:400; font-style:normal;">\n" + "<p style=" margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">Once upon a time, in a galaxy far, far away... </p></body></html>"); /* TRANSLATORS: refers to the action of accepting something */ char *s = C_("Button", "Ok"); char *s = C_("WelcomeDialog", "Don't show me this message in the future"); -- cgit v1.2.1 From b809141c079d0f5c2629a7b6d2b136fd022d3c55 Mon Sep 17 00:00:00 2001 From: David Planella Date: Fri, 10 May 2013 03:58:40 +0200 Subject: Added Qt Designer .ui file extraction support using the internal XML parser --- intltool-extract.in | 77 +++++++++++++++++++++++++++++++++-------------------- 1 file changed, 48 insertions(+), 29 deletions(-) diff --git a/intltool-extract.in b/intltool-extract.in index 779f268..45a969c 100644 --- a/intltool-extract.in +++ b/intltool-extract.in @@ -829,6 +829,45 @@ sub type_quotedxml { } } +# Parse the tree as returned by readXml() for Qt Designer .ui files. +sub traverse_qtdesigner { + my $nodename = shift; + my $content = shift; + my @list = @{ $content }; + my $attrs_ref = shift @list; + my %attrs = %{ $attrs_ref }; + if ($nodename eq 'string' and !exists $attrs{"notr"}) { + # Preserve whitespace. Deal with it ourselves, below. + my $message = getXMLstring($content, 1); + + # We strip leading and trailing whitespace but + # preserve whitespace within (e.g. newlines) + $message =~ s/^\s+//; + $message =~ s/\s+$//; + + my $context = $attrs{'comment'}; + # Remove enclosing quotes from msgctxt + $context =~ s/^["'](.*)["']/$1/ if $context; + $message = $context . "\004" . $message if $context; + add_message($message); + my $comment = $attrs{'extracomment'}; + # Remove enclosing quotes from developer comments + $comment =~ s/^["'](.*)["']/$1/ if $comment; + $comments{$message} = $comment if $comment; + } else { + my $index = 0; + while (scalar(@list) > 1) { + my $type = shift @list; + my $content = shift @list; + if (!$type || "$type" eq "1") { + next; + } else { + traverse_qtdesigner($type, $content); + } + } + } +} + sub type_qtdesigner { ### For translatable Qt Designer XML files ### # @@ -849,36 +888,16 @@ sub type_qtdesigner { # Ok - # Read all tags one by one and extract the translatable messages - # within them - while ($input =~ /]*notr\s*=\s*"([^"]*)")? - (?:\s+[^>]*\bcomment\b\s*=\s*"([^"]*)")? - (?:\s+[^>]*extracomment\s*=\s*"([^"]*)")? - [^>]*>([^<]+) - <\/string>/xsg) { - - # Skip string if notr="true" (i.e. string marked as non-translatable) - if (defined($1) && $1 eq "true") { - next; - } - - # Read translatable message - my $message = entity_decode($4); - - # Add msgctxt to the translatable message if present - if (defined($2)) { - $message = entity_decode($2) . "\004" . $message; - } - - # Add message to the array of translatable messages (msgids) - add_message($message); - - # Add developer comment to the comments hash - if (defined($3)) { - $comments{$message} = entity_decode($3); - } + my $tree = readXml($input); + my @tree_nodes = @{ $tree }; + my $node = shift @tree_nodes; + while (!$node || "$node" eq "1") { + shift @tree_nodes; + $node = shift @tree_nodes; } + my $content = shift @tree_nodes; + traverse_qtdesigner($node, $content); + } sub type_glade { -- cgit v1.2.1 From 19d2d8dee15b9dfd70f6e5c94beb3cb3d00bad4e Mon Sep 17 00:00:00 2001 From: David Planella Date: Fri, 10 May 2013 04:02:13 +0200 Subject: Updated man page with the new gettext/qtdesigner type for extraction --- doc/intltool-extract.8 | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/intltool-extract.8 b/doc/intltool-extract.8 index f0a2998..1c064b1 100644 --- a/doc/intltool-extract.8 +++ b/doc/intltool-extract.8 @@ -54,6 +54,8 @@ Specify the type of source file. Currently supported types are: "gettext/scheme" (.scm) .br "gettext/xml" (Generic XML file) +.br +"gettext/qtdesigner" (Qt Designer .ui files) .IP "\fB\-v\fR" 4 .PD 0 .IP "\fB\--version\fR" 4 -- cgit v1.2.1 From 7b401b2ba496fd6a583ad3fdc64ae1f2ff9e01f1 Mon Sep 17 00:00:00 2001 From: David Planella Date: Fri, 10 May 2013 04:13:43 +0200 Subject: Added files for gettext/qtdesigner testcase and result to the corresponding Makefile.am files --- tests/cases/Makefile.am | 2 ++ tests/results/Makefile.am | 1 + 2 files changed, 3 insertions(+) diff --git a/tests/cases/Makefile.am b/tests/cases/Makefile.am index 637e190..3b0ef81 100644 --- a/tests/cases/Makefile.am +++ b/tests/cases/Makefile.am @@ -16,6 +16,7 @@ EXTRA_DIST = \ extract8.glade \ extract9.po \ extract9.xml.in \ + extract-qtdesigner.ui \ fr.po \ fr_BE.po \ fr_FR.po \ @@ -58,6 +59,7 @@ CLEANFILES = \ extract14.xml.in.h \ extract9.xml \ extract9.xml.in.h \ + extract-qtdesigner.ui.h \ gsettings.gschema.xml.h \ iso88591text.xml.in.h \ merge-cdata.xml \ diff --git a/tests/results/Makefile.am b/tests/results/Makefile.am index 4366c6b..6230064 100644 --- a/tests/results/Makefile.am +++ b/tests/results/Makefile.am @@ -17,6 +17,7 @@ EXTRA_DIST = \ extract12.xml.in.h \ extract14.xml.in.h \ extract-gtkbuilder.ui.h \ + extract-qtdesigner.ui.h \ gsettings.gschema.xml.h \ iso88591text.xml.in.h \ merge-cdata.xml \ -- cgit v1.2.1