Index: extract-xml.sh =================================================================== --- extract-xml.sh +++ extract-xml.sh @@ -1,44 +1,50 @@ #! /bin/bash podir=${podir:-$PWD/po} -files=`find . -name XmlMessages.sh` -dirs=`for i in $files; do echo \`dirname $i\`; done | sort -u` +files=$(find . -name XmlMessages.sh) +dirs=$(for i in $files; do dirname "$i"; done | sort -u) tmpname="$PWD/messages.log" INTLTOOL_EXTRACT=${INTLTOOL_EXTRACT:-intltool-extract} -INTLTOOL_FLAGS=-q -test -z "$VERBOSE" || INTLTOOL_FLAGS= +INTLTOOL_FLAGS=(-q) +test -z "$VERBOSE" || INTLTOOL_FLAGS=() XGETTEXT=${XGETTEXT:-xgettext} # using xgettext 0.15 or later ### TODO what --flags param should be used? -XGETTEXT_FLAGS="--copyright-holder=This_file_is_part_of_KDE --from-code=UTF-8 -C --kde --msgid-bugs-address=https://bugs.kde.org" +XGETTEXT_FLAGS=(--copyright-holder=This_file_is_part_of_KDE --from-code=UTF-8 -C --kde --msgid-bugs-address=https://bugs.kde.org) export INTLTOOL_EXTRACT XGETTEXT XGETTEXT_FLAGS for subdir in $dirs; do test -z "$VERBOSE" || echo "Making XML messages in $subdir" - (cd $subdir + (cd "$subdir" || echo "Failed to change directory into $subdir" if test -f XmlMessages.sh; then - xml_po_list=`bash -c ". XmlMessages.sh ; get_files"` + xml_po_list=$(bash -c ". XmlMessages.sh ; get_files") for xml_file_relpath in $xml_po_list; do - xml_file_po=`bash -c ". XmlMessages.sh ; po_for_file $xml_file_relpath"` - tags_for_file=`bash -c ". XmlMessages.sh ; tags_for_file $xml_file_relpath"` - if [ "x$tags_for_file" != "xcomment" ]; then - echo "At the moment we only support extraction of the comment tag, not of $tags_for_file" - fi + xml_file_po=$(bash -c ". XmlMessages.sh ; po_for_file $xml_file_relpath") + tags_for_file=$(bash -c ". XmlMessages.sh ; tags_for_file $xml_file_relpath") + xml_podir=${xml_file_relpath}.podir - xml_in_file=$xml_podir/`basename $xml_file_relpath`.in - if [ ! -e $xml_podir ]; then - mkdir $xml_podir - cat $xml_file_relpath | sed -e 's/.*lang=.*//g' | sed -r -e 's/(\<\/?)comment(\>)/\1_comment\2/g' > $xml_in_file - if test -s $xml_in_file ; then - $INTLTOOL_EXTRACT $INTLTOOL_FLAGS --type='gettext/xml' $xml_in_file - $XGETTEXT $XGETTEXT_FLAGS --keyword=N_ -o $podir/${xml_file_po}t ${xml_in_file}.h - else - echo "Empty preprocessed XML file: $xml_in_file !" - fi - rm -rf $xml_podir - $FILLXMLFROMPO $xml_file_relpath $L10NDIR $SUBMODULE $xml_file_po - xmllint $xml_file_relpath --noout + xml_in_file=$xml_podir/$(basename "$xml_file_relpath").in + if [ ! -e "$xml_podir" ]; then + mkdir "$xml_podir" + # intltool-extract does not support message context which we need, so do it one tag at a time plus sed + for tag in $tags_for_file; do + sed -e 's/.*lang=.*//g' < "$xml_file_relpath" | sed -r -e "s/(\<\/?)${tag}(\>)/\1_${tag}\2/g" > "${xml_in_file}.${tag}" + + if test -s "${xml_in_file}.${tag}" ; then + $INTLTOOL_EXTRACT "${INTLTOOL_FLAGS[@]}" --type='gettext/xml' "${xml_in_file}.${tag}" + else + echo "Empty preprocessed XML file: $xml_in_file.${tag} !" + fi + sed -i "s/N_(/NC_(\"${tag}\", /" "${xml_in_file}.${tag}.h" + done + cat "${xml_in_file}".*.h > "${xml_in_file}.h" + + $XGETTEXT "${XGETTEXT_FLAGS[@]}" --keyword=NC_:1c,2 -o "$podir/${xml_file_po}t" "${xml_in_file}.h" + + rm -rf "$xml_podir" + $FILLXMLFROMPO "$xml_file_relpath" "$L10NDIR" "$SUBMODULE" "$xml_file_po" + xmllint "$xml_file_relpath" --noout else echo "$xml_podir exists!" fi @@ -50,8 +56,8 @@ else rm -f rc.cpp fi - ) >& $tmpname - test -s $tmpname && { echo $subdir ; cat "$tmpname"; } + ) >& "$tmpname" + test -s "$tmpname" && { echo "$subdir" ; cat "$tmpname"; } done -rm -f $tmpname +rm -f "$tmpname" Index: fillxmlfrompo.py =================================================================== --- fillxmlfrompo.py +++ fillxmlfrompo.py @@ -2,28 +2,13 @@ # -*- coding: utf-8 -*- """ -Copyright 2014 Burkhard Lück +SPDX-FileCopyrightText: 2014 Burkhard Lück +SPDX-FileCopyrightText: 2020 Andrius Štikonas <аndrius@stikonas.eu> -Permission to use, copy, modify, and distribute this software -and its documentation for any purpose and without fee is hereby -granted, provided that the above copyright notice appear in all -copies and that both that the copyright notice and this -permission notice and warranty disclaimer appear in supporting -documentation, and that the name of the author not be used in -advertising or publicity pertaining to distribution of the -software without specific, written prior permission. - -The author disclaim all warranties with regard to this -software, including all implied warranties of merchantability -and fitness. In no event shall the author be liable for any -special, indirect or consequential damages or any damages -whatsoever resulting from loss of use, data or profits, whether -in an action of contract, negligence or other tortious action, -arising out of or in connection with the use or performance of -this software. +SPDX-License-Identifier: MIT """ -import sys, os, glob, re, codecs +import sys, os, glob, codecs import polib if len(sys.argv) != 5: @@ -30,17 +15,25 @@ print('\nUsage: python %s path/to/xmlfile path/to/l10ndir/ l10nmodulename pofilename.po' %os.path.basename(sys.argv[0])) else: xmlfilepath, xmlfilename, l10ndirpath, l10nmodulename, pofilename = sys.argv[1], sys.argv[1].split("/")[-1], sys.argv[2], sys.argv[3], sys.argv[4] - begincommenttag = "" - endcommenttag = "" - begincommentlangtag = '" for tag in tags] + endtags = ["" for tag in tags] + beginlangtags = ["<" + tag + " xml:lang=" for tag in tags] + pofiledict = {} if not l10ndirpath.endswith("/"): - l10ndirpath+="/" + l10ndirpath += "/" for pofile in xmlpofilelist: langcode = pofile.split(l10ndirpath)[1].split("/")[0] if langcode != "x-test": @@ -47,24 +40,34 @@ po = polib.pofile(pofile) pofiledict[langcode] = po for line in xmllines: - if begincommenttag in line: - indentwidth = line.split(begincommenttag)[0] + presenttags = list(tag in line for tag in begintags) + presenttagindices = list(filter(lambda i: presenttags[i], range(len(presenttags)))) + assert(len(presenttagindices) <= 1), "Too many tags in a single line" + + if any(presenttags): + tagindex = presenttagindices[0] + tag = tags[tagindex] + begintag = begintags[tagindex] + endtag = endtags[tagindex] + beginlangtag = beginlangtags[tagindex] + + indentwidth = line.split(begintag)[0] xmlwithtranslation += line - msgid=line.split(begincommenttag)[1].split(endcommenttag)[0] + msgid = line.split(begintag)[1].split(endtag)[0] for lang, po in sorted(pofiledict.items()): msgstr = '' for entry in pofiledict[lang].translated_entries(): - if entry.msgid == msgid: + if entry.msgid == msgid and entry.msgctxt == tag: msgstr = entry.msgstr break if msgstr != '': - transline = '%s%s"%s">%s%s\n' %(indentwidth, begincommentlangtag, lang, msgstr, endcommenttag) + transline = '%s%s"%s">%s%s\n' %(indentwidth, beginlangtag, lang, msgstr, endtag) xmlwithtranslation += transline - elif begincommentlangtag in line: + elif any(tag in line for tag in beginlangtags): pass else: xmlwithtranslation += line - - modifiedxml = codecs.open("%s" %xmlfilepath,"w","utf8") + + modifiedxml = codecs.open("%s" %xmlfilepath, "w", "utf8") modifiedxml.write(xmlwithtranslation) modifiedxml.close()