4 This class enables translation of Evergreen's seed database strings.
6 Requires polib from http://polib.googlecode.com
8 # Copyright 2007-2008 Dan Scott <dscott@laurentian.ca>
10 # This program is free software; you can redistribute it and/or
11 # modify it under the terms of the GNU General Public License
12 # as published by the Free Software Foundation; either version 2
13 # of the License, or (at your option) any later version.
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
26 class SQL(basel10n.BaseL10N):
28 This class provides methods for extracting translatable strings from
29 Evergreen's database seed values, generating a translatable POT file,
30 reading translated PO files, and generating SQL for inserting the
31 translated values into the Evergreen database.
36 basel10n.BaseL10N.__init__(self)
39 def getstrings(self, source):
41 Each INSERT statement contains 0 or more oils_i18n_gettext()
42 markers for the en-US string that identify the string (which
43 we push into the POEntry.occurrences attribute), class hint,
44 and property. We concatenate the class hint and property and
45 use that for our msgid attribute.
47 A sample INSERT string that we'll scan is as follows:
49 INSERT INTO foo.bar (key, value) VALUES
50 (99, oils_i18n_gettext(99, 'string', 'class hint', 'property'));
55 findi18n = re.compile(r'.*?oils_i18n_gettext\((.*?)\'\)')
56 intkey = re.compile(r'\s*(?P<id>\d+),\s*\'(?P<string>.+?)\',\s*\'(?P<class>.+?)\',\s*\'(?P<property>.+?)$')
57 textkey = re.compile(r'\s*\'(?P<id>.*?)\',\s*\'(?P<string>.+?)\',\s*\'(?P<class>.+?)\',\s*\'(?P<property>.+?)$')
59 # Iterate through the source SQL grabbing table names and l10n strings
60 sourcefile = open(source)
61 for line in sourcefile:
64 entry = findi18n.search(line)
67 for parms in entry.groups():
68 # Try for an integer-based primary key parameter first
69 fi18n = intkey.search(parms)
71 # Otherwise, it must be a text-based primary key parameter
72 fi18n = textkey.search(parms)
73 fq_field = "%s.%s" % (fi18n.group('class'), fi18n.group('property'))
75 poe.occurrences = [(fq_field, num)]
76 poe.tcomment = 'id::' + fi18n.group('id')
77 # Unescape escaped SQL single-quotes for translators' sanity
78 poe.msgid = re.compile(r'\'\'').sub("'", fi18n.group('string'))
81 print "Error in line %d of SQL source file" % (num)
83 def create_sql(self, locale):
85 Creates a set of INSERT statements that place translated strings
86 into the config.i18n_core table.
89 insert = "INSERT INTO config.i18n_core (fq_field, identity_value," \
90 " translation, string) VALUES ('%s', '%s', '%s', '%s');"
91 for entry in self.pot:
92 for fq_field in entry.occurrences:
93 # Escape SQL single-quotes to avoid b0rkage
94 msgid = re.compile(r'\'').sub("''", entry.tcomment)
95 msgstr = re.compile(r'\'').sub("''", entry.msgstr)
96 msgid = re.compile(r'^id::').sub('', msgid)
98 # Don't generate a stmt for an untranslated string
100 self.sql.append(insert % (fq_field[0], msgid, locale, msgstr))
104 Determine what action to take
106 opts = optparse.OptionParser()
107 opts.add_option('-p', '--pot', action='store', \
108 help='Generate POT from the specified source SQL file', metavar='FILE')
109 opts.add_option('-s', '--sql', action='store', \
110 help='Generate SQL from the specified source POT file', metavar='FILE')
111 opts.add_option('-l', '--locale', \
112 help='Locale of the SQL file that will be generated')
113 opts.add_option('-o', '--output', dest='outfile', \
114 help='Write output to FILE (defaults to STDOUT)', metavar='FILE')
115 (options, args) = opts.parse_args()
119 pot.getstrings(options.pot)
121 pot.savepot(options.outfile)
123 sys.stdout.write(pot.pot.__str__())
125 if not options.locale:
126 opts.error('Must specify an output locale')
128 pot.loadpo(options.sql)
129 pot.create_sql(options.locale)
130 if not options.outfile:
133 outfile = open(options.outfile, 'w')
134 for insert in pot.sql:
135 outfile.write(insert + "\n")
139 if __name__ == '__main__':