implemented gettext instead of old hdf style

This commit is contained in:
lars 2006-11-28 14:24:53 +00:00
parent 27b61640bc
commit 557d3e8f8c
3 changed files with 46 additions and 28 deletions

View File

@ -207,6 +207,9 @@ class CryptoBoxProps(CryptoBox):
'''reads all files in path LangDir and returns a list of
basenames from existing hdf files, that should are all available
languages'''
# TODO: for now we hardcode it - change this!
return "en fr si de".split(" ")
# TODO: old implementation (before gettext) - remove it
languages = [ f.rstrip(".hdf")
for f in os.listdir(self.prefs["Locations"]["LangDir"])
if f.endswith(".hdf") ]

View File

@ -81,28 +81,15 @@ class CryptoBoxPlugin:
return None
def getLanguageData(self, lang="en"):
try:
import neo_cgi, neo_util
except:
raise CryptoBoxExceptions.CBEnvironmentError("couldn't import 'neo_*'! Try 'apt-get install python-clearsilver'.")
langdir = os.path.abspath(os.path.join(self.pluginDir, "lang"))
## first: the default language file (english)
langFiles = [os.path.join(langdir, "en.hdf")]
## maybe we have to load a translation afterwards
if lang != "en":
langFiles.append(os.path.join(langdir, lang + ".hdf"))
file_found = False
def getLanguageData(self):
import neo_cgi, neo_util
lang_hdf = neo_util.HDF()
for langFile in langFiles:
if os.access(langFile, os.R_OK):
lang_hdf.readFile(langFile)
file_found = True
if file_found:
return lang_hdf
else:
self.cbox.log.debug("Couldn't find a valid plugin language file (%s)" % str(langFiles))
return None
langFile = os.path.join(self.pluginDir, 'language.hdf')
try:
lang_hdf.readFile(langFile)
except (neo_util.Error, neo_util.ParseError), errMsg:
self.cbox.log.error("failed to load language file (%s) of plugin (%s):" % (langFile,self.getName()))
return lang_hdf
def loadDataSet(self, hdf):

View File

@ -16,6 +16,8 @@ except ImportError:
raise ImportError, errorMsg
GETTEXT_DOMAIN = 'cryptobox-server'
class PluginIconHandler:
@ -385,7 +387,40 @@ class WebInterfaceSites:
return hdf.getValue(value, "")
def __substituteGettext(self, languages, textDomain, hdf):
import gettext
try:
translator = gettext.translation(textDomain, languages=languages)
except IOError, errMsg:
## no translation found
self.cbox.log.warn("unable to load language file: %s" % errMsg)
return hdf
def walk_tree(hdf_node):
def translate_node(node):
for (key,value) in node.attrs():
if key == 'LINK': return
node.setValue("",translator.ugettext(node.value()))
while hdf_node:
translate_node(hdf_node)
walk_tree(hdf_node.child())
hdf_node = hdf_node.next()
walk_tree(hdf)
def __getLanguageData(self, web_lang="en"):
hdf = neo_util.HDF()
hdf.readFile(os.path.join(self.prefs["Locations"]["TemplateDir"],"language.hdf"))
self.__substituteGettext([web_lang], GETTEXT_DOMAIN, hdf)
## load the language data of all plugins
for p in self.pluginList.getPlugins():
pl_lang = p.getLanguageData()
self.__substituteGettext([web_lang], "%s-feature-%s" % (GETTEXT_DOMAIN, p.getName()), pl_lang)
hdf.copy("Plugins.%s" % p.getName(), pl_lang)
self.cbox.log.debug("language data for plugin loaded: %s" % p.getName())
return hdf
def __getLanguageData2(self, web_lang="en"):
default_lang = "en"
conf_lang = self.prefs["WebSettings"]["Language"]
hdf = neo_util.HDF()
@ -427,12 +462,6 @@ class WebInterfaceSites:
## first: assume, that the template file is in the global template directory
self.dataset["Settings.TemplateFile"] = os.path.abspath(os.path.join(self.prefs["Locations"]["TemplateDir"], template + ".cs"))
## load the language data of all plugins
for p in self.pluginList.getPlugins():
pl_lang = p.getLanguageData(self.dataset["Settings.Language"])
if pl_lang:
hdf.copy("Lang.Plugins.%s" % p.getName(), pl_lang)
if plugin:
## check, if the plugin provides the template file -> overriding
plugin_cs_file = plugin.getTemplateFileName(template)
@ -472,4 +501,3 @@ class WebInterfaceSites:
else:
yield line + "\n"