Package turbogears :: Package i18n :: Package sagettext

Source Code for Package turbogears.i18n.sagettext

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