Home | Trees | Indices | Help |
|
---|
|
1 """SQLAlchemy-based version of gettext""" 2 3 import codecs 4 import os.path 5 from gettext import translation 6 7 from turbogears import config 8 from turbogears.database import get_engine, metadata, session 9 from turbogears.i18n.sagettext.model import (TG_Message, TG_Domain, 10 tg_message_table, tg_domain_table) 11 from turbogears.i18n.utils import get_locale 12 13 14 _catalogs = {} 15 1618 """SQLAlchemy-based version of gettext. 19 20 Messages are stored in database instead. 21 22 """ 23 24 locale = get_locale(locale) 25 26 messages = get_sa_catalog(domain).get(locale) 27 if not messages: 28 messages = get_sa_catalog(domain).get(locale[:2], {}) 29 30 return unicode(messages.get(key, key))31 3234 """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 tg_domain = session.query(TG_Domain).filter_by( 50 name=unicode(domain)).first() 51 if not tg_domain: 52 return catalog 53 54 tg_messages = session.query(TG_Message).filter_by(domain=tg_domain) 55 56 for tg_message in tg_messages: 57 locale = tg_message.locale 58 messages = catalog.get(locale, {}) 59 messages[tg_message.name] = tg_message.text 60 catalog[locale] = messages 61 62 _catalogs[domain] = catalog 63 64 return catalog65 6668 """Create the tables if needed.""" 69 get_engine() 70 tg_message_table.drop(checkfirst=True) 71 tg_domain_table.drop(checkfirst=True) 72 tg_domain_table.create(checkfirst=True) 73 tg_message_table.create(checkfirst=True)74 7577 """Create message catalog from database. 78 79 Creates a message catalog based on list of locales from existing 80 GNU message catalog. 81 82 """ 83 # first try to create the table if we need to... 84 create_sa_catalog_tables() 85 86 tg_domain = session.query(TG_Domain).filter_by(name=domain).first() 87 if tg_domain: 88 return 89 90 tg_domain = TG_Domain() 91 tg_domain.name = unicode(domain) 92 session.add(tg_domain) 93 session.flush() 94 95 localedir = config.get('i18n.locale_dir', 'locales') 96 97 for locale in locales: 98 translations = translation(domain=domain, 99 localedir=localedir, languages=[locale]) 100 catalog = translations._catalog 101 for name, text in catalog.items(): 102 tg_message = TG_Message() 103 tg_message.domain = tg_domain 104 tg_message.locale = locale 105 tg_message.name = unicode(name) 106 tg_message.text = unicode(text) 107 session.add(tg_message) 108 session.flush()109 110112 """Take all domains and messages and creates message catalogs.""" 113 localedir = config.get('i18n.locale_dir', 'locales') 114 115 for locale in locales: 116 117 messages_dir = os.path.join(localedir, locale, 'LC_MESSAGES') 118 119 for domain in session.query(TG_Domain): 120 pofile = os.path.join(messages_dir, '%s.po' % domain.name) 121 f = codecs.open(pofile, 'w', 'UTF-8') 122 f.write(""" 123 # SOME DESCRIPTIVE TITLE. 124 # Copyright (C) YEAR ORGANIZATION 125 # FIRST AUTHOR <EMAIL@ADDRESS>, YEAR. 126 # 127 msgid "" 128 msgstr "" 129 "Project-Id-Version: PACKAGE VERSION\\n" 130 "POT-Creation-Date: CREATION_DATE\\n" 131 "PO-Revision-Date: REVISION_DATE\\n" 132 "Last-Translator: TRANSLATOR <EMAIL@ADDRESS>\\n" 133 "Language-Team: LANGUAGE <LL@li.org>\\n" 134 "MIME-Version: 1.0\\n" 135 "Content-Type: text/plain; charset=UTF-8\\n" 136 "Content-Transfer-Encoding: 8bit\\n" 137 "Generated-By: turbogears\\n" 138 139 """) 140 141 query = session.query(TG_Message).filter_by( 142 domain=domain, locale=locale) 143 144 for message in query: 145 if not message.name: 146 continue # descriptive text 147 148 f.write(u""" 149 msgid "%s" 150 msgstr "%s" 151 """ % (message.name, message.text)) 152 153 f.close()154
Home | Trees | Indices | Help |
|
---|
Generated by Epydoc 3.0.1 on Fri Jul 19 17:20:03 2019 | http://epydoc.sourceforge.net |