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
59
100
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
139
140 f.write(u"""
141 msgid "%s"
142 msgstr "%s"
143 """%(message.name, message.text))
144
145 f.close()
146