Translation class is generally useful, so put it in the tools library.

This commit is contained in:
Eric S. Raymond 2008-03-31 13:11:59 +00:00
parent 515c68ce4c
commit c6fca2eb00
2 changed files with 41 additions and 32 deletions

View file

@ -584,6 +584,46 @@ class CrossRef:
except KeyError:
return 0
#
# String translations from po files. The advantage of this code is that it
# does not require the gettext binary message catalogs to have been compiled.
# The disavantage is that it eats lots of core!
#
class Translation(dict):
"Parses a po file to create a translation dictionary."
def __init__(self, textdomain, isocode, topdir=""):
self.gettext = {}
self.isocode = isocode
if self.isocode != "en":
fn = "po/wesnoth-%s/%s.po" % (textdomain, isocode)
if topdir:
fn = os.path.join(topdir, fn)
gettext = file(fn).read()
matches = re.compile("""(msgid|msgstr)((\s*".*?")+)""").findall(gettext)
id = ""
for match in matches:
text = "".join(re.compile('"(.*?)"').findall(match[1].replace("\\n", "")))
if match[0] == "msgid":
id = text
else:
self.gettext[id] = text
def get(self, key, dflt):
if self.isocode == "en":
return key
else:
return self.gettext.get(key, dflt)
def __getitem__(self, key):
if self.isocode == "en":
return key
else:
return self.gettext[key]
def __contains__(self, key):
if self.isocode == "en":
return True
else:
return key in self.gettext.keys()
## Namespace management
#
# This is the only part of the code that actually knows about the

View file

@ -18,40 +18,9 @@ import wesnoth.wmldata as wmldata
import wesnoth.wmlparser as wmlparser
import wesnoth.wmltools as wmltools
class translation(dict):
"Parses po files to create a translation dictionary. Eats lots of core!"
def __init__(self, textdomain, isocode):
self.gettext = {}
self.isocode = isocode
if self.isocode != "en":
gettext = file("po/wesnoth-%s/%s.po" % (textdomain, isocode)).read()
matches = re.compile("""(msgid|msgstr)((\s*".*?")+)""").findall(gettext)
id = ""
for match in matches:
text = "".join(re.compile('"(.*?)"').findall(match[1].replace("\\n", "")))
if match[0] == "msgid":
id = text
else:
self.gettext[id] = text
def get(self, key, dflt):
if self.isocode == "en":
return key
else:
return self.gettext.get(key, dflt)
def __getitem__(self, key):
if self.isocode == "en":
return key
else:
return self.gettext[key]
def __contains__(self, key):
if self.isocode == "en":
return True
else:
return key in self.gettext.keys()
def list_units(units_filename, text_to_parse, domain, isocode, campaign):
"List all units in the specified namespace, None = mainline."
tx = translation(domain, isocode)
tx = wmltools.Translation(domain, isocode)
# Create a new parser.
parser = wmlparser.Parser(datadir)