[Libreoffice-commits] .: 2 commits - l10ntools/prj l10ntools/scripts

Andras Timar timar at kemper.freedesktop.org
Thu Sep 15 02:07:41 PDT 2011


 l10ntools/prj/d.lst             |    1 
 l10ntools/scripts/fast_merge.pl |   10 +
 l10ntools/scripts/po2lo         |  205 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 212 insertions(+), 4 deletions(-)

New commits:
commit 1fff8f2576faf3faa141471790ff5d18e0740b0d
Author: Miklos Vajna <vmiklos at frugalware.org>
Date:   Wed Sep 7 23:39:15 2011 +0200

    Add po2lo tool

diff --git a/l10ntools/prj/d.lst b/l10ntools/prj/d.lst
index f2051d7..ad2d715 100644
--- a/l10ntools/prj/d.lst
+++ b/l10ntools/prj/d.lst
@@ -40,6 +40,7 @@ mkdir: %_DEST%\bin\help\com\sun\star\help
 ..\scripts\localize %_DEST%\bin\localize
 ..\scripts\fast_merge.pl %_DEST%\bin\fast_merge.pl
 ..\scripts\keyidGen.pl %_DEST%\bin\keyidGen.pl
+..\scripts\po2lo %_DEST%\bin\po2lo
 ..\inc\export.hxx %_DEST%\inc\l10ntools\export.hxx
 ..\inc\l10ntools\directory.hxx %_DEST%\inc\l10ntools\directory.hxx
 ..\inc\l10ntools\file.hxx %_DEST%\inc\l10ntools\file.hxx
diff --git a/l10ntools/scripts/po2lo b/l10ntools/scripts/po2lo
new file mode 100755
index 0000000..0f81ebc
--- /dev/null
+++ b/l10ntools/scripts/po2lo
@@ -0,0 +1,205 @@
+#!/usr/bin/env python
+# Version: MPL 1.1 / GPLv3+ / LGPLv3+
+#
+# The contents of this file are subject to the Mozilla Public License Version
+# 1.1 (the "License"); you may not use this file except in compliance with
+# the License or as specified alternatively below. You may obtain a copy of
+# the License at http://www.mozilla.org/MPL/
+#
+# Software distributed under the License is distributed on an "AS IS" basis,
+# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+# for the specific language governing rights and limitations under the
+# License.
+#
+# The Initial Developer of the Original Code is
+#       Miklos Vajna <vmiklos at frugalware.org>
+# Portions created by the Initial Developer are Copyright (C) 2011 the
+# Initial Developer. All Rights Reserved.
+#
+# Major Contributor(s):
+#
+# For minor contributions see the git repository.
+#
+# Alternatively, the contents of this file may be used under the terms of
+# either the GNU General Public License Version 3 or later (the "GPLv3+"), or
+# the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
+# in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
+# instead of those above.
+
+import getopt, sys, os, re
+
+class Options:
+    """Options of this script."""
+
+    def __init__(self):
+        self.input = None
+        self.output = None
+        self.language = None
+        self.template = None
+
+class Entry:
+    """Represents a single line in an SDF file."""
+
+    def __init__(self, items):
+        self.items = items # list of 15 fields
+        path = self.items[1].split('\\')
+        self.po = "%s/%s/%s.po" % (options.input.replace('\\', '/'), self.items[0], "/".join(path[:-1]))
+        prefix = ""
+        if len(self.items[5]):
+            prefix += "%s." % self.items[5]
+        if len(self.items[3]):
+            prefix += "%s." % self.items[3]
+        self.keys = []
+        # 10..13 are translation types
+        for idx in range(10, 14):
+            if len(self.items[idx]):
+                t = {10:'text', 12:'quickhelptext', 13:'title'}[idx]
+                self.keys.append((idx, self.sdf2po("%s#%s.%s%s" % (path[-1], self.items[4], prefix, t))))
+
+    def translate(self, translations):
+        """Translates text in the entry based on translations."""
+
+        self.items[9] = options.language
+        for idx, key in self.keys:
+            try:
+                self.items[idx] = translations.data[(self.po, key)]
+
+                self.items[14] = "2002-02-02 02:02:02"
+            except KeyError:
+                pass
+        self.items[14] = self.items[14].strip()
+
+    def sdf2po(self, s):
+        """Escapes special chars in po key names."""
+
+        return s.translate(normalizetable)
+
+class Template:
+    """Represents a reference template in SDF format."""
+
+    def __init__(self, path):
+        sock = open(path)
+        self.lines = []
+        for line in sock:
+            entry = Entry(line.split('\t'))
+            if os.path.exists(entry.po):
+                self.lines.append(entry)
+
+    def translate(self, translations):
+        """Translates entires in the template based on translations."""
+
+        sock = open(options.output, "w")
+        for line in self.lines:
+            line.translate(translations)
+            sock.write("\t".join(line.items)+"\r\n")
+        sock.close()
+
+class Translations:
+    """Represents a set of .po files, containing translations."""
+
+    def __init__(self):
+        key = None
+        self.data = {}
+        for root, dirs, files in os.walk(options.input):
+            for file in files:
+                path = "%s/%s" % (root, file)
+                sock = open(path)
+                buf = []
+                multiline = False
+                fuzzy = False
+                for line in sock:
+                    if line.startswith("#: "):
+                        key = line.strip()[3:]
+                    elif line.startswith("#, fuzzy"):
+                        fuzzy = True
+                    elif line.startswith("msgstr "):
+                        trans = line.strip()[8:-1]
+                        if len(trans):
+                            if fuzzy:
+                                fuzzy = False
+                            else:
+                                self.setdata(path, key, trans)
+                                multiline = False
+                        else:
+                            buf = []
+                            buf.append(trans)
+                            multiline = True
+                    elif multiline and line.startswith('"'):
+                        buf.append(line.strip()[1:-1])
+                    elif multiline and not len(line.strip()) and len("".join(buf)):
+                        if fuzzy:
+                            fuzzy = False
+                        else:
+                            self.setdata(path, key, "".join(buf))
+                        buf = []
+                        multiline = False
+                if multiline and len("".join(buf)) and not fuzzy:
+                    self.setdata(path, key, "".join(buf))
+
+    def setdata(self, path, key, s):
+        """Sets the translation for a given path and key, handling (un)escaping
+        as well."""
+        if key:
+            # unescape the po special chars
+            s = s.replace('\\"', '"')
+            if key.split('#')[0].endswith(".xhp"):
+                s = self.escape_help_text(s)
+            else:
+                s = s.replace('\\\\', '\\')
+            self.data[(path.replace('\\', '/'), key)] = s
+
+    def escape_help_text(self, text):
+        """Escapes the help text as it would be in an SDF file."""
+
+        for tag in helptagre.findall(text):
+            # <, >, " are only escaped in <[[:lower:]]> tags. Some HTML tags make it in in
+            # lowercase so those are dealt with. Some LibreOffice help tags are not
+            # escaped.
+            escapethistag = False
+            for escape_tag in ["ahelp", "link", "item", "emph", "defaultinline", "switchinline", "caseinline", "variable", "bookmark_value", "image", "embedvar", "alt"]:
+                if tag.startswith("<%s" % escape_tag) or tag == "</%s>" % escape_tag:
+                    escapethistag = True
+            if tag in ["<br/>", "<help-id-missing/>"]:
+                escapethistag = True
+            if escapethistag:
+                escaped_tag = ("\\<" + tag[1:-1] + "\\>").replace('"', '\\"')
+                text = text.replace(tag, escaped_tag)
+        return text
+
+def main():
+    """Main function of this script."""
+
+    opts, args = getopt.getopt(sys.argv[1:], "si:o:l:t:", ["skipsource", "input=", "output=", "language=", "template="])
+    for opt, arg in opts:
+        if opt in ("-s", "--skipsource"):
+            pass
+        elif opt in ("-i", "--input"):
+            options.input = arg.strip('/')
+        elif opt in ("-o", "--output"):
+            options.output = arg
+        elif opt in ("-l", "--language"):
+            options.language = arg
+        elif opt in ("-t", "--template"):
+            options.template = arg
+    template = Template(options.template)
+    translations = Translations()
+    template.translate(translations)
+
+# used by ecape_help_text
+helptagre = re.compile('''<[/]??[a-z_\-]+?(?:| +[a-z]+?=".*?") *[/]??>''')
+
+options = Options()
+
+# used by sdf2po()
+normalfilenamechars = "/#.0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
+normalizetable = ""
+for i in map(chr, range(256)):
+    if i in normalfilenamechars:
+        normalizetable += i
+    else:
+        normalizetable += "_"
+
+if __name__ == "__main__":
+    main()
+
+# vim:set filetype=python shiftwidth=4 softtabstop=4 expandtab:
commit 47a8454f5f76030203ce6458cf63d4348bdacffe
Author: Miklos Vajna <vmiklos at frugalware.org>
Date:   Thu Sep 15 01:13:05 2011 +0200

    fast_merge: fix mis-merge of first module's strings
    
    The problem was that in write_lines() when $first_run was true, the
    add_to_buffer() call already set the current module's name to the second
    module.
    
    A fix for this is to use let make_paths() take a parameter, and in case
    of the first run, pass the really first module name, not the current
    one.

diff --git a/l10ntools/scripts/fast_merge.pl b/l10ntools/scripts/fast_merge.pl
index 5dc63cf..73b824e 100644
--- a/l10ntools/scripts/fast_merge.pl
+++ b/l10ntools/scripts/fast_merge.pl
@@ -91,7 +91,7 @@ while( hasLines() )
 }
 if( $#current+1 ne 0 )
 {
-    ( $path , $localize_file ) = make_paths();
+    ( $path , $localize_file ) = make_paths($current[ 0 ]->module);
     add_to_buffer();
     write_buffer( $path , $localize_file );
 }
@@ -240,7 +240,8 @@ sub hasLines
 
 sub make_paths
 {
-    my $localizeFile = $merge_dir."\\".$current[ 0 ]->module."\\".$current[ 0 ]->file;
+    my $module = shift ;
+    my $localizeFile = $merge_dir."\\".$module."\\".$current[ 0 ]->file;
     my $path = getDir( $localizeFile );
     $path =~ s/\\/\//g;
 
@@ -251,8 +252,9 @@ sub make_paths
 sub write_lines
 {
     if( $first_run ){
+        my $module = $current[ 0 ]->module;
         add_to_buffer();
-        my( $path , $localize_file ) = make_paths();
+        my( $path , $localize_file ) = make_paths($module);
         $last_path = $path;
         $last_localize_file = $localize_file;
         mkpath $path;
@@ -262,7 +264,7 @@ sub write_lines
     else
     {
         return , if ( $#current+1 eq 0 );
-        my( $path , $localize_file ) = make_paths();
+        my( $path , $localize_file ) = make_paths($current[ 0 ]->module);
         if( $path eq $last_path )
         {
             add_to_buffer();


More information about the Libreoffice-commits mailing list