1 """SQLObject-based version of gettext
2 """
3 import turbogears
4 from turbogears.i18n.sogettext.model import TG_Message, TG_Domain
5 from turbogears.i18n.utils import get_locale
6
7 from sqlobject import SQLObjectNotFound
8
9 from gettext import translation
10 import codecs
11
12 _catalogs = {}
13
14 -def so_gettext(key, locale=None, domain=None):
15 """
16 SQLObject-based version of gettext. Messages are stored in
17 database instead.
18 """
19
20 locale = get_locale(locale)
21
22 messages = get_so_catalog(domain).get(locale)
23 if not messages:
24 messages = get_so_catalog(domain).get(locale[:2], {})
25
26 return unicode(messages.get(key, key))
27
57
59 """create the tables if needed
60 """
61 TG_Message.dropTable(ifExists=True)
62 TG_Domain.dropTable(ifExists=True)
63
64 TG_Domain.createTable(ifNotExists=True)
65 TG_Message.createTable(ifNotExists=True)
66
90
92 """Takes all domains and messages and creates message catalogs
93 """
94 localedir = turbogears.config.get("i18n.locale_dir", "locales")
95
96 for locale in locales:
97
98 messages_dir = os.path.join(localedir, locale, "LC_MESSAGES")
99
100 for domain in TG_Domain.select():
101 pofile=os.path.join(messages_dir, "%s.po" %domain.name)
102 f = codecs.open(pofile, "w", "UTF-8")
103 f.write("""
104 # SOME DESCRIPTIVE TITLE.
105 # Copyright (C) YEAR ORGANIZATION
106 # FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
107 #
108 msgid ""
109 msgstr ""
110 "Project-Id-Version: PACKAGE VERSION\\n"
111 "POT-Creation-Date: CREATION_DATE\\n"
112 "PO-Revision-Date: REVISION_DATE\\n"
113 "Last-Translator: TRANSLATOR <EMAIL@ADDRESS>\\n"
114 "Language-Team: LANGUAGE <LL@li.org>\\n"
115 "MIME-Version: 1.0\\n"
116 "Content-Type: text/plain; charset=UTF-8\\n"
117 "Content-Transfer-Encoding: 8bit\\n"
118 "Generated-By: turbogears\\n"
119
120 """)
121
122 for message in TG_Message.selectBy(domain=domain, locale=locale):
123
124 if message.name == "":
125 continue
126
127 f.write(u"""
128 msgid "%s"
129 msgstr "%s"
130 """%(message.name, message.text))
131
132 f.close()
133