Package turbogears :: Package i18n :: Package sogettext

Source Code for Package turbogears.i18n.sogettext

  1  """SQLObject-based version of gettext 
  2  """ 
  3  import turbogears 
  4  from turbogears.i18n.sogettext.model import TG_Message, TG_Domain 
  5  from turbogears.i18n.utils import get_locale 
  6   
  7  from sqlobject import SQLObjectNotFound 
  8   
  9  from gettext import translation 
 10  import codecs 
 11   
 12  _catalogs = {} 
 13   
14 -def so_gettext(key, locale=None, domain=None):
15 """ 16 SQLObject-based version of gettext. Messages are stored in 17 database instead. 18 """ 19 20 locale = get_locale(locale) 21 22 messages = get_so_catalog(domain).get(locale) 23 if not messages: 24 messages = get_so_catalog(domain).get(locale[:2], {}) 25 26 return unicode(messages.get(key, key))
27
28 -def get_so_catalog(domain):
29 """ 30 Retrieves all translations for locale and domain from database 31 and stores in thread data. 32 """ 33 34 if domain is None: 35 domain = turbogears.config.get("i18n.domain", "messages") 36 37 catalog = _catalogs.get(domain) 38 if not catalog: 39 40 catalog = {} 41 42 try: 43 domain = TG_Domain.byName(domain) 44 except SQLObjectNotFound: 45 return catalog 46 47 results = TG_Message.selectBy(domain=domain) 48 for message in results: 49 locale = message.locale 50 messages = catalog.get(locale, {}) 51 messages[message.name] = message.text 52 catalog[locale] = messages 53 54 _catalogs[domain.name] = catalog 55 56 return catalog
57
58 -def create_so_catalog_tables():
59 """create the tables if needed 60 """ 61 TG_Message.dropTable(ifExists=True) 62 TG_Domain.dropTable(ifExists=True) 63 64 TG_Domain.createTable(ifNotExists=True) 65 TG_Message.createTable(ifNotExists=True)
66
67 -def create_so_catalog(locales, domain):
68 """ 69 Creates a message catalog based on list of locales from existing 70 GNU message catalog 71 """ 72 # first try to create the table if we need to... 73 create_so_catalog_tables() 74 75 localedir = turbogears.config.get("i18n.locale_dir", "locales") 76 77 try: 78 domain = TG_Domain.byName(domain) 79 return 80 except SQLObjectNotFound: 81 domain = TG_Domain(name=domain) 82 83 for locale in locales: 84 translations = translation(domain=domain.name, localedir=localedir, 85 languages=[locale]) 86 catalog = translations._catalog 87 88 for k, v in catalog.items(): 89 TG_Message(domain=domain, locale=locale, name=k, text=v)
90
91 -def dump_so_catalogs(locales):
92 """Takes all domains and messages and creates message catalogs 93 """ 94 localedir = turbogears.config.get("i18n.locale_dir", "locales") 95 96 for locale in locales: 97 98 messages_dir = os.path.join(localedir, locale, "LC_MESSAGES") 99 100 for domain in TG_Domain.select(): 101 pofile=os.path.join(messages_dir, "%s.po" %domain.name) 102 f = codecs.open(pofile, "w", "UTF-8") 103 f.write(""" 104 # SOME DESCRIPTIVE TITLE. 105 # Copyright (C) YEAR ORGANIZATION 106 # FIRST AUTHOR <EMAIL@ADDRESS>, YEAR. 107 # 108 msgid "" 109 msgstr "" 110 "Project-Id-Version: PACKAGE VERSION\\n" 111 "POT-Creation-Date: CREATION_DATE\\n" 112 "PO-Revision-Date: REVISION_DATE\\n" 113 "Last-Translator: TRANSLATOR <EMAIL@ADDRESS>\\n" 114 "Language-Team: LANGUAGE <LL@li.org>\\n" 115 "MIME-Version: 1.0\\n" 116 "Content-Type: text/plain; charset=UTF-8\\n" 117 "Content-Transfer-Encoding: 8bit\\n" 118 "Generated-By: turbogears\\n" 119 120 """) 121 122 for message in TG_Message.selectBy(domain=domain, locale=locale): 123 124 if message.name == "": 125 continue # descriptive text 126 127 f.write(u""" 128 msgid "%s" 129 msgstr "%s" 130 """%(message.name, message.text)) 131 132 f.close()
133