def pofiles_getLocaleNames(cls, code): pofile = dirs.get_readable_i18n_files("messages.po", code, localized_item=False, only_last=True) po = polib.pofile(pofile) return (po.metadata['Language-Name'], po.metadata['Language-Native-Name'])
def load_messages(lang=None): if lang is None: lang = language g.messages.clear() files = dirs.get_readable_i18n_files("messages.po", lang, default_language=False) for lang, pofile in files: try: po = polib.pofile(pofile) for entry in po.translated_entries(): g.messages[entry.msgid] = entry.msgstr except IOError: pass # silently ignore non-existing files
def __init__(self, code=None, source=None): """Initialize a Locale of the given code from the given source If source is blank or omitted, the default one from by getAvailableSources() is used. code must be string with ll or ll_CC format. If blank or omitted, only getAvailableLocales() method will work, all other attributes will be None. """ # Attributes self.source = source self.code = code self.english_name = None self.native_name = None # Handle source if not self.source: self.source = self.getAvailableSources()[0] if self.source not in self.getAvailableSources(): raise ValueError("{0} is not a valid source." " Available sources are: {1}".format( source, ", ".join(self.getAvailableSources()))) self.source = self.source.lower() # Override default attributes methods according to the given source if self.source == 'babel': try: import babel except ImportError: raise ImportError("'{0}' requires babel module".format(source)) self.getAvailableLocales = babel.localedata.list if self.code: locale = babel.Locale.parse(self.code) self.english_name = locale.english_name self.native_name = locale.get_display_name() if self.source == 'icu': try: import icu # new module name, 1.0 onwards except ImportError: try: import PyICU as icu # old module name, up to 0.9 except ImportError: ImportError("'{0}' requires icu" " or PyICU module".format(source)) self.getAvailableLocales = \ lambda: icu.Locale.getAvailableLocales().keys() if self.code: locale = icu.Locale(self.code) self.english_name = locale.getDisplayName() self.native_name = locale.getDisplayName(locale) if self.source == 'pofiles': import code.polib as polib import os self.getAvailableLocales = \ lambda: [osp.splitext(filename)[0].split("_", 1)[1] for filename in os.listdir(g.data_dir) if filename.split("_", 1)[0] == "messages" and osp.splitext(filename)[1] == ".po" and osp.isfile(osp.join(g.data_dir, filename))] if self.code: po = polib.pofile(osp.join(g.data_dir, "messages_{0}.po".format(self.code))) self.english_name = po.metadata['Language-Name'] self.native_name = po.metadata['Language-Native-Name']
def __init__(self, code=None, source=None): """Initialize a Locale of the given code from the given source If source is blank or omitted, the default one from by getAvailableSources() is used. code must be string with ll or ll_CC format. If blank or omitted, only getAvailableLocales() method will work, all other attributes will be None. """ # Attributes self.source = source self.code = code self.english_name = None self.native_name = None # Handle source if not self.source: self.source = self.getAvailableSources()[0] if self.source not in self.getAvailableSources(): raise ValueError("{0} is not a valid source." " Available sources are: {1}".format( source, ", ".join(self.getAvailableSources()))) self.source = self.source.lower() # Override default attributes methods according to the given source if self.source == 'babel': try: import babel except ImportError: raise ImportError("'{0}' requires babel module".format(source)) self.getAvailableLocales = babel.localedata.list if self.code: locale = babel.Locale.parse(self.code) self.english_name = locale.english_name self.native_name = locale.get_display_name() if self.source == 'icu': try: import icu # new module name, 1.0 onwards except ImportError: try: import PyICU as icu # old module name, up to 0.9 except ImportError: raise ImportError("'{0}' requires icu" " or PyICU module".format(source)) self.getAvailableLocales = icu.Locale.getAvailableLocales().keys if self.code: locale = icu.Locale(self.code) self.english_name = locale.getDisplayName() self.native_name = locale.getDisplayName(locale) if self.source == 'pofiles': import code.polib as polib import os self.getAvailableLocales = \ lambda: [osp.splitext(filename)[0].split("_", 1)[1] for filename in os.listdir(g.data_dir) if filename.split("_", 1)[0] == "messages" and osp.splitext(filename)[1] == ".po" and osp.isfile(osp.join(g.data_dir, filename))] if self.code: po = polib.pofile( osp.join(g.data_dir, "messages_{0}.po".format(self.code))) self.english_name = po.metadata['Language-Name'] self.native_name = po.metadata['Language-Native-Name']