2 # -*- coding: utf-8 -*-
4 # Copyright 2004-2006 Zuza Software Foundation
6 # This file is part of translate.
8 # translate is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # translate is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with translate; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 """convert Qt Linguist (.ts) files to Gettext PO localization files
25 See: http://translate.sourceforge.net/wiki/toolkit/ts2po for examples and
30 from translate
.storage
import po
31 from translate
.storage
import ts
34 def __init__(self
, duplicatestyle
="msgctxt"):
35 self
.duplicatestyle
= duplicatestyle
37 def convertmessage(self
, contextname
, messagenum
, source
, target
, msgcomments
, transtype
):
38 """makes a pounit from the given message"""
39 thepo
= po
.pounit(encoding
="UTF-8")
40 thepo
.addlocation("%s#%d" % (contextname
, messagenum
))
43 if len(msgcomments
) > 0:
44 thepo
.addnote(msgcomments
)
45 if transtype
== "unfinished" and thepo
.istranslated():
47 if transtype
== "obsolete":
48 # This should use the Gettext obsolete method but it would require quite a bit of work
49 thepo
.addnote("(obsolete)", origin
="developer")
50 # using the fact that -- quote -- "(this is nonsense)"
53 def convertfile(self
, inputfile
):
54 """converts a .ts file to .po format"""
55 tsfile
= ts
.QtTsParser(inputfile
)
56 thetargetfile
= po
.pofile()
57 targetheader
= thetargetfile
.makeheader(charset
="UTF-8", encoding
="8bit")
58 thetargetfile
.addunit(targetheader
)
59 for contextname
, messages
in tsfile
.iteritems():
61 for message
in messages
:
63 source
= tsfile
.getmessagesource(message
)
64 translation
= tsfile
.getmessagetranslation(message
)
65 comment
= tsfile
.getmessagecomment(message
)
66 transtype
= tsfile
.getmessagetype(message
)
67 thepo
= self
.convertmessage(contextname
, messagenum
, source
, translation
, comment
, transtype
)
68 thetargetfile
.addunit(thepo
)
69 thetargetfile
.removeduplicates(self
.duplicatestyle
)
72 def convertts(inputfile
, outputfile
, templates
, duplicatestyle
="msgctxt"):
73 """reads in stdin using fromfileclass, converts using convertorclass, writes to stdout"""
74 convertor
= ts2po(duplicatestyle
=duplicatestyle
)
75 outputstore
= convertor
.convertfile(inputfile
)
76 if outputstore
.isempty():
78 outputfile
.write(str(outputstore
))
82 from translate
.convert
import convert
83 formats
= {"ts":("po", convertts
)}
84 parser
= convert
.ConvertOptionParser(formats
, usepots
=True, description
=__doc__
)
85 parser
.add_duplicates_option()