def get_region_name(countrycode):
""" return translated region name from countrycode using iso3166 """
# find translated name
if countrycode:
for iso in ["iso_3166", "iso_3166_2"]:
path = os.path.join("/usr/share/xml/iso-codes/", iso + ".xml")
if os.path.exists(path):
root = xml.etree.ElementTree.parse(path)
xpath = ".//%s_entry[@alpha_2_code='%s']" % (iso, countrycode)
match = root.find(xpath)
if match is not None:
name = match.attrib.get("common_name")
if not name:
name = match.attrib["name"]
return dgettext(iso, name)
return ""
# the first parameter of SetRequirements
python类dgettext()的实例源码
def _(txt):
t = gettext.dgettext('messages', txt)
if t == txt:
t = gettext.gettext(txt)
return t
def _(txt):
t = gettext.dgettext('messages', txt)
if t == txt:
t = gettext.gettext(txt)
return t
def _(txt):
t = gettext.dgettext("XTPanel", txt)
if t == txt:
print "[XTPanel] fallback to default translation for", txt
t = gettext.gettext(txt)
return t
###############################
# Coded by PCD, February 2008 #
###############################
def _(txt):
t = gettext.dgettext("CCcamInfo", txt)
if t == txt:
t = gettext.gettext(txt)
return t
def _(txt):
t = gettext.dgettext("MountManager", txt)
if t == txt:
t = gettext.gettext(txt)
return t
def check_incorrect_password(self, b_output):
b_incorrect_password = to_bytes(gettext.dgettext(self._play_context.become_method, C.BECOME_ERROR_STRINGS[self._play_context.become_method]))
return b_incorrect_password and b_incorrect_password in b_output
def check_missing_password(self, b_output):
b_missing_password = to_bytes(gettext.dgettext(self._play_context.become_method, C.BECOME_MISSING_STRINGS[self._play_context.become_method]))
return b_missing_password and b_missing_password in b_output
def doSettingsBackup(self):
backup = None
from Plugins.SystemPlugins.OBH.BackupManager import BackupFiles
self.BackupFiles = BackupFiles(self.session, True)
Components.Task.job_manager.AddJob(self.BackupFiles.createBackupJob())
Components.Task.job_manager.in_background = False
for job in Components.Task.job_manager.getPendingJobs():
if job.name == dgettext('obh', 'Backup manager'):
break
self.showJobView(job)
def doImageBackup(self):
backup = None
from Plugins.SystemPlugins.OBH.ImageManager import ImageBackup
self.ImageBackup = ImageBackup(self.session, True)
Components.Task.job_manager.AddJob(self.ImageBackup.createBackupJob())
Components.Task.job_manager.in_background = False
for job in Components.Task.job_manager.getPendingJobs():
if job.name == dgettext('obh', 'Image manager'):
break
self.showJobView(job)
def showJobView(self, job):
if job.name == dgettext('OBH', 'Image manager'):
self.ImageBackupDone = True
elif job.name == dgettext('obh', 'Backup manager'):
self.SettingsBackupDone = True
from Screens.TaskView import JobView
Components.Task.job_manager.in_background = False
if not self.autobackuprunning:
self.session.openWithCallback(self.startActualUpgrade(("menu", "menu")), JobView, job, cancelable = False, backgroundable = False, afterEventChangeable = False, afterEvent="close")
else:
self.session.openWithCallback(self.doAutoBackup, JobView, job, cancelable = False, backgroundable = False, afterEventChangeable = False, afterEvent="close")
def _(txt):
if gettext.dgettext(PluginLanguageDomain, txt):
return gettext.dgettext(PluginLanguageDomain, txt)
else:
print "[" + PluginLanguageDomain + "] fallback to default translation for " + txt
return gettext.gettext(txt)
def text(message, gtk30=False, context=None):
"""Return a translated message and cache it for reuse"""
if message not in localized_messages:
if gtk30:
# Get a message translated from GTK+ 3 domain
full_message = message if not context else '%s\04%s' % (
context, message)
localized_messages[message] = dgettext('gtk30', full_message)
# Fix for untranslated messages with context
if context and localized_messages[message] == full_message:
localized_messages[message] = dgettext('gtk30', message)
else:
localized_messages[message] = gettext(message)
return localized_messages[message]
def check_incorrect_password(self, b_output):
b_incorrect_password = to_bytes(gettext.dgettext(self._play_context.become_method, C.BECOME_ERROR_STRINGS[self._play_context.become_method]))
return b_incorrect_password and b_incorrect_password in b_output
def check_missing_password(self, b_output):
b_missing_password = to_bytes(gettext.dgettext(self._play_context.become_method, C.BECOME_MISSING_STRINGS[self._play_context.become_method]))
return b_missing_password and b_missing_password in b_output
def langcode_to_name(langcode):
import xml.etree.ElementTree
from gettext import dgettext
for iso in ["iso_639_3", "iso_639"]:
path = os.path.join("/usr/share/xml/iso-codes/", iso + ".xml")
if os.path.exists(path):
root = xml.etree.ElementTree.parse(path)
xpath = ".//%s_entry[@part1_code='%s']" % (iso, langcode)
match = root.find(xpath)
if match is not None:
return dgettext(iso, match.attrib["name"])
return langcode
def _init_submit(self):
self.submit_window.set_title(_("Review %s") %
gettext.dgettext("app-install-data", self.app.name))
def _init_modify(self):
self._populate_review()
self.submit_window.set_title(_("Modify Your %(appname)s Review") % {
'appname': gettext.dgettext("app-install-data", self.app.name)})
self.button_post.set_label(_("Modify"))
self.SUBMIT_MESSAGE = _("Updating your review")
self.FAILURE_MESSAGE = _("Failed to edit review")
self.SUCCESS_MESSAGE = _("Review updated")
self._enable_or_disable_post_button()
def _setup_details(self, widget, app, iconname, version, display_name):
# icon shazam
try:
icon = self.icons.load_icon(iconname, self.APP_ICON_SIZE, 0)
except:
icon = self.icons.load_icon(Icons.MISSING_APP, self.APP_ICON_SIZE,
0)
self.review_appicon.set_from_pixbuf(icon)
# title
app = utf8(gettext.dgettext("app-install-data", app.name))
version = utf8(version)
self.review_title.set_markup(
'<b><span size="x-large">%s</span></b>\n%s' % (app, version))
# review label
self.review_label.set_markup(_('Review by: %s') %
display_name.encode('utf8'))
# review summary label
self.review_summary_label.set_markup(_('Summary:'))
#rating label
self.rating_label.set_markup(_('Rating:'))
#error detail link label
self.label_expander.set_markup('<small><u>%s</u></small>' %
(_('Error Details')))
def get_desktop(self, key, translated=True):
" get generic option under 'Desktop Entry'"
# never translate the pkgname
if key == "X-AppInstall-Package":
return self.get(self.DE, key)
# shortcut
if not translated:
return self.get(self.DE, key)
# first try dgettext
if self.has_option_desktop("X-Ubuntu-Gettext-Domain"):
value = self.get(self.DE, key)
if value:
domain = self.get(self.DE, "X-Ubuntu-Gettext-Domain")
translated_value = gettext.dgettext(domain, value)
if value != translated_value:
return translated_value
# then try app-install-data
value = self.get(self.DE, key)
if value:
translated_value = gettext.dgettext("app-install-data", value)
if value != translated_value:
return translated_value
# then try the i18n version of the key (in [de_DE] or
# [de]) but ignore errors and return the untranslated one then
try:
locale = getdefaultlocale(('LANGUAGE', 'LANG', 'LC_CTYPE',
'LC_ALL'))[0]
if locale:
if self.has_option_desktop("%s[%s]" % (key, locale)):
return self.get(self.DE, "%s[%s]" % (key, locale))
if "_" in locale:
locale_short = locale.split("_")[0]
if self.has_option_desktop("%s[%s]" % (key, locale_short)):
return self.get(self.DE, "%s[%s]" %
(key, locale_short))
except ValueError:
pass
# and then the untranslated field
return self.get(self.DE, key)
def langcode_to_name(langcode):
import xml.etree.ElementTree
from gettext import dgettext
for iso in ["iso_639_3", "iso_639"]:
path = os.path.join("/usr/share/xml/iso-codes/", iso + ".xml")
if os.path.exists(path):
root = xml.etree.ElementTree.parse(path)
xpath = ".//%s_entry[@part1_code='%s']" % (iso, langcode)
match = root.find(xpath)
if match is not None:
return dgettext(iso, match.attrib["name"])
return langcode
def _init_submit(self):
self.submit_window.set_title(_("Review %s") %
gettext.dgettext("app-install-data", self.app.name))
def _init_modify(self):
self._populate_review()
self.submit_window.set_title(_("Modify Your %(appname)s Review") % {
'appname': gettext.dgettext("app-install-data", self.app.name)})
self.button_post.set_label(_("Modify"))
self.SUBMIT_MESSAGE = _("Updating your review")
self.FAILURE_MESSAGE = _("Failed to edit review")
self.SUCCESS_MESSAGE = _("Review updated")
self._enable_or_disable_post_button()
def _setup_details(self, widget, app, iconname, version, display_name):
# icon shazam
try:
icon = self.icons.load_icon(iconname, self.APP_ICON_SIZE, 0)
except:
icon = self.icons.load_icon(Icons.MISSING_APP, self.APP_ICON_SIZE,
0)
self.review_appicon.set_from_pixbuf(icon)
# title
app = utf8(gettext.dgettext("app-install-data", app.name))
version = utf8(version)
self.review_title.set_markup(
'<b><span size="x-large">%s</span></b>\n%s' % (app, version))
# review label
self.review_label.set_markup(_('Review by: %s') %
display_name.encode('utf8'))
# review summary label
self.review_summary_label.set_markup(_('Summary:'))
#rating label
self.rating_label.set_markup(_('Rating:'))
#error detail link label
self.label_expander.set_markup('<small><u>%s</u></small>' %
(_('Error Details')))
def get_desktop(self, key, translated=True):
# strip away bogus prefixes
if key.startswith("X-AppInstall-"):
key = key[len("X-AppInstall-"):]
# shortcut
if not translated:
return self.tag_section[key]
# FIXME: make i18n work similar to get_desktop
# first try dgettext
if "Gettext-Domain" in self.tag_section:
value = self.tag_section.get(key)
if value:
domain = self.tag_section["Gettext-Domain"]
translated_value = gettext.dgettext(domain, value)
if value != translated_value:
return translated_value
# then try the i18n version of the key (in [de_DE] or
# [de]) but ignore errors and return the untranslated one then
try:
locale = getdefaultlocale(('LANGUAGE', 'LANG', 'LC_CTYPE',
'LC_ALL'))[0]
if locale:
if self.has_option_desktop("%s-%s" % (key, locale)):
return self.tag_section["%s-%s" % (key, locale)]
if "_" in locale:
locale_short = locale.split("_")[0]
if self.has_option_desktop("%s-%s" % (key, locale_short)):
return self.tag_section["%s-%s" % (key, locale_short)]
except ValueError:
pass
# and then the untranslated field
return self.tag_section[key]
def get_desktop(self, key, translated=True):
" get generic option under 'Desktop Entry'"
# never translate the pkgname
if key == "X-AppInstall-Package":
return self.get(self.DE, key)
# shortcut
if not translated:
return self.get(self.DE, key)
# first try dgettext
if self.has_option_desktop("X-Ubuntu-Gettext-Domain"):
value = self.get(self.DE, key)
if value:
domain = self.get(self.DE, "X-Ubuntu-Gettext-Domain")
translated_value = gettext.dgettext(domain, value)
if value != translated_value:
return translated_value
# then try app-install-data
value = self.get(self.DE, key)
if value:
translated_value = gettext.dgettext("app-install-data", value)
if value != translated_value:
return translated_value
# then try the i18n version of the key (in [de_DE] or
# [de]) but ignore errors and return the untranslated one then
try:
locale = getdefaultlocale(('LANGUAGE', 'LANG', 'LC_CTYPE',
'LC_ALL'))[0]
if locale:
if self.has_option_desktop("%s[%s]" % (key, locale)):
return self.get(self.DE, "%s[%s]" % (key, locale))
if "_" in locale:
locale_short = locale.split("_")[0]
if self.has_option_desktop("%s[%s]" % (key, locale_short)):
return self.get(self.DE, "%s[%s]" %
(key, locale_short))
except ValueError:
pass
# and then the untranslated field
return self.get(self.DE, key)
def _(txt):
if gettext.dgettext(PluginLanguageDomain, txt):
return gettext.dgettext(PluginLanguageDomain, txt)
else:
print "[" + PluginLanguageDomain + "] fallback to default translation for " + txt
return gettext.gettext(txt)
def _dugettext(domain, message):
'''
:param domain: translation domain
:param message: message to translate
:returns: the translated message
Unicode version of :func:`gettext.dgettext`.
'''
try:
t = gettext.translation(domain, gettext._localedirs.get(domain, None),
codeset=gettext._localecodesets.get(domain))
except IOError:
return message
else:
return t.ugettext(message)
def __(catalog,str):
return utf8(gettext.dgettext(catalog, str))
def TranslateTXT(txt):
t = gettext.dgettext(PluginLanguageDomain, txt)
if t == txt:
t = gettext.gettext(txt)
return t