Package turbogears :: Package i18n :: Package sogettext

Source Code for Package turbogears.i18n.sogettext

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