From b77eec653df14059296fc3185ff9817edfa0825b Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Thu, 12 May 2011 22:33:30 -0500 Subject: [PATCH] Load gettext, and load it into the template environment --- mediagoblin/globals.py | 9 +++++ mediagoblin/templates/mediagoblin/root.html | 4 +-- .../en/LC_MESSAGES/mediagoblin.mo | Bin 0 -> 502 bytes .../en/LC_MESSAGES/mediagoblin.po | 4 +-- mediagoblin/util.py | 33 +++++++++++++++++- 5 files changed, 44 insertions(+), 6 deletions(-) create mode 100644 mediagoblin/translations/en/LC_MESSAGES/mediagoblin.mo diff --git a/mediagoblin/globals.py b/mediagoblin/globals.py index 59a94558..80d1f01d 100644 --- a/mediagoblin/globals.py +++ b/mediagoblin/globals.py @@ -2,6 +2,9 @@ In some places, we need to access the database, public_store, queue_store """ +import gettext +import pkg_resources + ############################# # General mediagoblin globals ############################# @@ -16,6 +19,12 @@ database = None public_store = None queue_store = None +# gettext +translations = gettext.find( + 'mediagoblin', + pkg_resources.resource_filename( + 'mediagoblin', 'translations'), ['en']) + def setup_globals(**kwargs): from mediagoblin import globals as mg_globals diff --git a/mediagoblin/templates/mediagoblin/root.html b/mediagoblin/templates/mediagoblin/root.html index 44d26a65..fa78bda2 100644 --- a/mediagoblin/templates/mediagoblin/root.html +++ b/mediagoblin/templates/mediagoblin/root.html @@ -19,9 +19,7 @@ {% block mediagoblin_content %} -

Welcome to GNU MediaGoblin!

- {# -

{% trans %}Welcome to GNU MediaGoblin!{% endtrans %}

#} +

{% trans %}Welcome to GNU MediaGoblin!{% endtrans %}

{% if request.user %}

diff --git a/mediagoblin/translations/en/LC_MESSAGES/mediagoblin.mo b/mediagoblin/translations/en/LC_MESSAGES/mediagoblin.mo new file mode 100644 index 0000000000000000000000000000000000000000..fb7046cdd069d85b478d03ce838a08b13736f58b GIT binary patch literal 502 zcmaJ;%TB^T6s^Wpmu_`&*=gcn+eCu{CAC;T}Vev5;e zpc`&-l5>)i`#AUW^yJ+#b!>2MaJ3VEs$uwhMGT3jq}$+)FzeM!`M>x9ZqkDKrYR$98QF()g;c*9GIE4(St9C2D`8m!!7$?e<-w lB7sO{#zjt{w&2\n" "Language-Team: LANGUAGE \n" @@ -17,7 +17,7 @@ msgstr "" "Content-Transfer-Encoding: 8bit\n" "Generated-By: Babel 0.9.6\n" -#: mediagoblin/templates/mediagoblin/root.html:21 +#: mediagoblin/templates/mediagoblin/root.html:22 msgid "Welcome to GNU MediaGoblin!" msgstr "" diff --git a/mediagoblin/util.py b/mediagoblin/util.py index ac977bdb..8c6ec6cc 100644 --- a/mediagoblin/util.py +++ b/mediagoblin/util.py @@ -15,6 +15,8 @@ # along with this program. If not, see . from email.MIMEText import MIMEText +import gettext +import pkg_resources import smtplib import sys @@ -56,10 +58,18 @@ def get_jinja_env(template_loader, locale): (In the future we may have another system for providing theming; for now this is good enough.) """ - return jinja2.Environment( + setup_gettext(locale) + + template_env = jinja2.Environment( loader=template_loader, autoescape=True, extensions=['jinja2.ext.i18n']) + template_env.install_gettext_callables( + mgoblin_globals.translations.gettext, + mgoblin_globals.translations.ngettext) + + return template_env + def setup_user_in_request(request): """ @@ -196,6 +206,10 @@ def send_email(from_addr, to_addrs, subject, message_body): ################### +TRANSLATIONS_PATH = pkg_resources.resource_filename( + 'mediagoblin', 'translations') + + def locale_to_lower_upper(locale): """ Take a locale, regardless of style, and format it like "en-us" @@ -246,3 +260,20 @@ def get_locale_from_request(request): target_lang = 'en' return locale_to_lower_upper(target_lang) + + +def setup_gettext(locale): + """ + Setup the gettext instance based on this locale + """ + # Later on when we have plugins we may want to enable the + # multi-translations system they have so we can handle plugin + # translations too + + # TODO: fallback nicely on translations from pt_PT to pt if not + # available, etc. + this_gettext = gettext.translation( + 'mediagoblin', TRANSLATIONS_PATH, [locale], fallback=True) + + mgoblin_globals.setup_globals( + translations=this_gettext) -- 2.25.1