diff options
Diffstat (limited to 'Pootle-2.0.0/pootle/scripts/convert')
-rw-r--r-- | Pootle-2.0.0/pootle/scripts/convert/__init__.py | 0 | ||||
-rw-r--r-- | Pootle-2.0.0/pootle/scripts/convert/lang.py | 44 | ||||
-rw-r--r-- | Pootle-2.0.0/pootle/scripts/convert/lang2po.py | 59 | ||||
-rw-r--r-- | Pootle-2.0.0/pootle/scripts/convert/po2lang.py | 49 |
4 files changed, 152 insertions, 0 deletions
diff --git a/Pootle-2.0.0/pootle/scripts/convert/__init__.py b/Pootle-2.0.0/pootle/scripts/convert/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/Pootle-2.0.0/pootle/scripts/convert/__init__.py diff --git a/Pootle-2.0.0/pootle/scripts/convert/lang.py b/Pootle-2.0.0/pootle/scripts/convert/lang.py new file mode 100644 index 0000000..5fd8ff9 --- /dev/null +++ b/Pootle-2.0.0/pootle/scripts/convert/lang.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python + +# lang.py +# Defines standard translation-toolkit structions for .lang files + +# Author: Dan Schafer <dschafer@mozilla.com> +# Date: 10 Jun 2008 + +from translate.storage import base +from translate.storage import txt + +class LangUnit(base.TranslationUnit): + """This is just a normal unit with a weird string output""" + def __str__(self): + return ";%s\n%s" % (str(self.source), str(self.target)) + +class LangStore(txt.TxtFile): + """We extend TxtFile, since that has a lot of useful stuff for encoding""" + UnitClass = LangUnit + + def parse(self, lines): + #Have we just seen a ';' line, and so are ready for a translation + readyTrans = False + + if not isinstance(lines, list): + lines = lines.split("\n") + for linenum in range(len(lines)): + line = lines[linenum].rstrip("\n").rstrip("\r") + + if len(line) == 0: #Skip blank lines + continue + + if readyTrans: #If we are expecting a translation, set the target + u.settarget(line) + readyTrans = False #We already have our translation + continue + + if line[0] == ';': + u = self.addsourceunit(line[1:]) + readyTrans = True # We're now expecting a translation on the next line + u.addlocation("%s:%d" % (self.filename, linenum+1)) + + def __str__(self): + return "\n\n".join([str(unit) for unit in self.units]) diff --git a/Pootle-2.0.0/pootle/scripts/convert/lang2po.py b/Pootle-2.0.0/pootle/scripts/convert/lang2po.py new file mode 100644 index 0000000..f1e1f2b --- /dev/null +++ b/Pootle-2.0.0/pootle/scripts/convert/lang2po.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python + +# lang2po.py +# Converts .lang files to .po files using standard translation-tookit methods + +# Author: Dan Schafer <dschafer@mozilla.com> +# Date: 10 Jun 2008 + +from translate.storage import po +import lang + +class lang2po: + def __init__(self, duplicatestyle="msgctxt"): + self.duplicatestyle = duplicatestyle + + def convertstore(self, thelangfile): + """converts a file to .po format""" + thetargetfile = po.pofile() + + # Set up the header + targetheader = thetargetfile.makeheader(charset="UTF-8", encoding="8bit") + targetheader.addnote("extracted from %s" % thelangfile.filename, "developer") + thetargetfile.addunit(targetheader) + + # For each lang unit, make the new po unit accordingly + for langunit in thelangfile.units: + newunit = thetargetfile.addsourceunit(langunit.source) + newunit.settarget(langunit.target) + newunit.addlocations(langunit.getlocations()) + + # Remove duplicates, because we can + thetargetfile.removeduplicates(self.duplicatestyle) + return thetargetfile + +def convertlang(inputfile, outputfile, templates, duplicatestyle="msgctxt", encoding="utf-8"): + """reads in stdin using fromfileclass, converts using convertorclass, writes to stdout""" + inputstore = lang.LangStore(inputfile, encoding=encoding) + convertor = lang2po(duplicatestyle=duplicatestyle) + outputstore = convertor.convertstore(inputstore) + if outputstore.isempty(): + return 0 + outputfile.write(str(outputstore)) + return 1 + +def main(argv=None): + from translate.convert import convert + from translate.misc import stdiotell + import sys + sys.stdout = stdiotell.StdIOWrapper(sys.stdout) + formats = {"lang":("po",convertlang), "*":("po",convertlang)} + parser = convert.ConvertOptionParser(formats, usepots=True, description=__doc__) + parser.add_option("", "--encoding", dest="encoding", default='utf-8', type="string", + help="The encoding of the input file (default: UTF-8)") + parser.passthrough.append("encoding") + parser.add_duplicates_option() + parser.run(argv) + +if __name__ == '__main__': + main() diff --git a/Pootle-2.0.0/pootle/scripts/convert/po2lang.py b/Pootle-2.0.0/pootle/scripts/convert/po2lang.py new file mode 100644 index 0000000..adcc9b5 --- /dev/null +++ b/Pootle-2.0.0/pootle/scripts/convert/po2lang.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python + +# po2lang.py +# Converts .po files to .lang files using standard translation-tookit methods + +# Author: Dan Schafer <dschafer@mozilla.com> +# Date: 10 Jun 2008 + +from translate.storage import po +import lang + +class po2lang: + def __init__(self, duplicatestyle="msgctxt"): + self.duplicatestyle = duplicatestyle + + def convertstore(self, inputstore): + """converts a file to .lang format""" + thetargetfile = lang.LangStore() + + # Run over the po units + for pounit in inputstore.units: + # Skip the header + if pounit.isheader(): + continue + newunit = thetargetfile.addsourceunit(pounit.source) + newunit.settarget(pounit.target) + return thetargetfile + +def convertlang(inputfile, outputfile, templates): + """reads in stdin using fromfileclass, converts using convertorclass, writes to stdout""" + inputstore = po.pofile(inputfile) + if inputstore.isempty(): + return 0 + convertor = po2lang() + outputstore = convertor.convertstore(inputstore) + outputfile.write(str(outputstore)) + return 1 + +def main(argv=None): + from translate.convert import convert + from translate.misc import stdiotell + import sys + sys.stdout = stdiotell.StdIOWrapper(sys.stdout) + formats = {"po":("lang",convertlang)} + parser = convert.ConvertOptionParser(formats, usepots=True, description=__doc__) + parser.run(argv) + +if __name__ == '__main__': + main() |