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
64
65
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
102
103
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
138
139 f.write(u"""
140 msgid "%s"
141 msgstr "%s"
142 """ % (message.name, message.text))
143
144 f.close()
145