diff --git a/lodgeit/application.py b/lodgeit/application.py index 9a1ea73..2905431 100644 --- a/lodgeit/application.py +++ b/lodgeit/application.py @@ -16,6 +16,9 @@ from datetime import datetime, timedelta from werkzeug import SharedDataMiddleware, ClosingIterator from werkzeug.exceptions import HTTPException, NotFound +from babel import Locale + +from lodgeit import i18n from lodgeit.utils import _local_manager, ctx, jinja_environment, \ Request from lodgeit.database import metadata, session, Paste @@ -29,17 +32,38 @@ class LodgeIt(object): def __init__(self, dburi, secret_key): #: the secret key used by the captcha self.secret_key = secret_key + #: name of the error handler self.not_found = ('static/not_found', {}) self.engine = sqlalchemy.create_engine(dburi, convert_unicode=True) + #: make sure all tables exist. metadata.create_all(self.engine) + + #: 18n setup + #TODO: load from user cookie + self.locale = None + self.set_locale('de_DE') + + #: jinja_environment update + jinja_environment.filters.update( + datetimeformat=i18n.format_datetime + ) + #: bind the application to the current context local self.bind_to_context() def bind_to_context(self): ctx.application = self + def set_locale(self, locale): + if not self.locale or self.locale.language != locale: + self.locale = Locale(locale) + self.translations = i18n.load_translations(self.locale) + + #: update gettext translations + jinja_environment.install_gettext_translations(self.translations) + def __call__(self, environ, start_response): """Minimal WSGI application for request dispatching.""" #: bind the application to the new context local diff --git a/lodgeit/controllers/pastes.py b/lodgeit/controllers/pastes.py index 772c532..674876d 100644 --- a/lodgeit/controllers/pastes.py +++ b/lodgeit/controllers/pastes.py @@ -12,6 +12,7 @@ from werkzeug import redirect, Response from werkzeug.exceptions import NotFound from lodgeit.utils import ctx, render_template +from lodgeit.i18n import list_languages, _ from lodgeit.controllers import BaseController from lodgeit.database import session, Paste from lodgeit.lib import antispam @@ -41,13 +42,13 @@ class PasteController(BaseController): spam = ctx.request.form.get('webpage') or antispam.is_spam(code) if spam: - error = 'your paste contains spam' + error = _('your paste contains spam') captcha = getform('captcha') if captcha: if check_hashed_solution(captcha): error = None else: - error += ' and the CAPTCHA solution was incorrect' + error += _(' and the CAPTCHA solution was incorrect') show_captcha = True if code and language and not error: paste = Paste(code, language, parent, ctx.request.user_hash, @@ -163,6 +164,15 @@ class PasteController(BaseController): if style_name in STYLES: resp.set_cookie('style', style_name) return resp + + def set_language(self): + """Minimal view that set's a different language. Redirects + back to the page the user is coming from.""" + lang = ctx.request.form.get('language') + resp = redirect(ctx.request.environ.get('HTTP_REFERER') or '/') + if lang in list_languages(): + ctx.application.set_locale(lang) + return resp def show_captcha(self): """Show a captcha.""" diff --git a/lodgeit/controllers/static.py b/lodgeit/controllers/static.py index 5e6349f..765fb9c 100644 --- a/lodgeit/controllers/static.py +++ b/lodgeit/controllers/static.py @@ -11,15 +11,16 @@ from werkzeug.exceptions import NotFound from lodgeit.utils import ctx, render_template +from lodgeit.i18n import _ from lodgeit.controllers import BaseController from lodgeit.lib.xmlrpc import xmlrpc HELP_PAGES = [ - ('pasting', 'Pasting'), - ('advanced', 'Advanced Features'), - ('xmlrpc', 'Using the XMLRPC Interface'), - ('integration', 'Scripts and Editor Integration') + ('pasting', _('Pasting')), + ('advanced', _('Advanced Features')), + ('xmlrpc', _('Using the XMLRPC Interface')), + ('integration', _('Scripts and Editor Integration')) ] known_help_pages = set(x[0] for x in HELP_PAGES) diff --git a/lodgeit/lib/highlighting.py b/lodgeit/lib/highlighting.py index 01e9d08..362a7c1 100644 --- a/lodgeit/lib/highlighting.py +++ b/lodgeit/lib/highlighting.py @@ -8,6 +8,7 @@ :copyright: 2007 by Armin Ronacher. :license: BSD """ +from lodgeit.i18n import _ import pygments from pygments.util import ClassNotFound from pygments.lexers import get_lexer_by_name @@ -19,51 +20,51 @@ from pygments.formatters import HtmlFormatter #: we use a hardcoded list here because we want to keep the interface #: simple LANGUAGES = { - 'text': 'Text', - 'python': 'Python', - 'pycon': 'Python Console Sessions', - 'pytb': 'Python Tracebacks', - 'html+php': 'PHP', - 'html+django': 'Django / Jinja Templates', - 'html+mako': 'Mako Templates', - 'html+myghty': 'Myghty Templates', - 'apache': 'Apache Config (.htaccess)', - 'bash': 'Bash', - 'bat': 'Batch (.bat)', - 'c': 'C', - 'cpp': 'C++', - 'csharp': 'C#', - 'css': 'CSS', - 'd': 'D', - 'minid': 'MiniD', - 'smarty': 'Smarty', - 'html': 'HTML', - 'html+php': 'PHP', - 'html+genshi': 'Genshi Templates', - 'js': 'JavaScript', - 'java': 'Java', - 'jsp': 'JSP', - 'lua': 'Lua', - 'haskell': 'Haskell', - 'scheme': 'Scheme', - 'ruby': 'Ruby', - 'irb': 'Interactive Ruby', - 'perl': 'Perl', - 'rhtml': 'eRuby / rhtml', - 'tex': 'TeX / LaTeX', - 'xml': 'XML', - 'rst': 'reStructuredText', - 'irc': 'IRC Logs', - 'diff': 'Unified Diff', - 'vim': 'Vim Scripts', - 'ocaml': 'OCaml', - 'sql': 'SQL', - 'squidconf': 'SquidConf', - 'sourceslist': 'sources.list', - 'erlang': 'Erlang', - 'vim': 'Vim', - 'dylan': 'Dylan', - 'gas': 'GAS' + 'text': _('Text'), + 'python': _('Python'), + 'pycon': _('Python Console Sessions'), + 'pytb': _('Python Tracebacks'), + 'html+php': _('PHP'), + 'html+django': _('Django / Jinja Templates'), + 'html+mako': _('Mako Templates'), + 'html+myghty': _('Myghty Templates'), + 'apache': _('Apache Config (.htaccess)'), + 'bash': _('Bash'), + 'bat': _('Batch (.bat)'), + 'c': _('C'), + 'cpp': _('C++'), + 'csharp': _('C#'), + 'css': _('CSS'), + 'd': _('D'), + 'minid': _('MiniD'), + 'smarty': _('Smarty'), + 'html': _('HTML'), + 'html+php': _('PHP'), + 'html+genshi': _('Genshi Templates'), + 'js': _('JavaScript'), + 'java': _('Java'), + 'jsp': _('JSP'), + 'lua': _('Lua'), + 'haskell': _('Haskell'), + 'scheme': _('Scheme'), + 'ruby': _('Ruby'), + 'irb': _('Interactive Ruby'), + 'perl': _('Perl'), + 'rhtml': _('eRuby / rhtml'), + 'tex': _('TeX / LaTeX'), + 'xml': _('XML'), + 'rst': _('reStructuredText'), + 'irc': _('IRC Logs'), + 'diff': _('Unified Diff'), + 'vim': _('Vim Scripts'), + 'ocaml': _('OCaml'), + 'sql': _('SQL'), + 'squidconf': _('SquidConf'), + 'sourceslist': _('sources.list'), + 'erlang': _('Erlang'), + 'vim': _('Vim'), + 'dylan': _('Dylan'), + 'gas': _('GAS') } STYLES = dict((x, x.title()) for x in get_all_styles()) diff --git a/lodgeit/lib/pagination.py b/lodgeit/lib/pagination.py index fdd0cac..d2ef0f2 100644 --- a/lodgeit/lib/pagination.py +++ b/lodgeit/lib/pagination.py @@ -9,6 +9,7 @@ :license: BSD """ import math +from lodgeit.i18n import _ def generate_pagination(page, per_page, total, link_builder=None, @@ -71,15 +72,15 @@ def generate_pagination(page, per_page, total, link_builder=None, if next_link: if next is not None: - result.append(u' Next »' % + result.append(_(u' Next »') % link_builder(next)) elif gray_next_link: - result.append(u' Next »') + result.append(_(u' Next »')) if prev_link: if prev is not None: - result.insert(0, u'« Prev ' % + result.insert(0, _(u'« Prev ') % link_builder(prev)) elif gray_prev_link: - result.insert(0, u'« Prev ') + result.insert(0, _(u'« Prev ')) return u''.join(result) diff --git a/lodgeit/utils.py b/lodgeit/utils.py index 7d686a9..6a4ef98 100644 --- a/lodgeit/utils.py +++ b/lodgeit/utils.py @@ -29,17 +29,18 @@ _local_manager = LocalManager(ctx) #: jinja environment for all the templates jinja_environment = Environment(loader=FileSystemLoader( - path.join(path.dirname(__file__), 'views'))) - + path.join(path.dirname(__file__), 'views')), + extensions=['jinja2.ext.i18n']) _word_only = partial(re.compile(r'[^a-zA-Z0-9]').sub, '') -def datetimeformat(obj): - """Helper filter for the template""" - return obj.strftime('%Y-%m-%d @ %H:%M') +def get_application(): + return getattr(ctx, 'application', None) -jinja_environment.filters['datetimeformat'] = datetimeformat + +def get_request(): + return getattr(ctx, 'request', None) def generate_user_hash(): diff --git a/lodgeit/views/about.html b/lodgeit/views/about.html index 5b363c9..f475504 100644 --- a/lodgeit/views/about.html +++ b/lodgeit/views/about.html @@ -1,64 +1,64 @@ {% extends "layout.html" %} -{% set page_title = 'About LodgeIt' %} +{% set page_title = _('About LodgeIt') %} {% set active_page = 'about' %} {% block body %}
-

Why the hell another pastebin?

-

+

{% trans %} Why the hell another pastebin?{% endtrans %}

+

{% trans %} Good question. Basically the world doesn't need another pastebin. There is pastie and dpaste.com which both use kick-ass highlighting libraries for highlighting the code and both have an intuitive user interface. Nevertheless there - are some features which are unique to LodgeIt. + are some features which are unique to LodgeIt.{% endtrans %}

-

Features

+

{% trans %}Features{% endtrans %}

-

Request More Languages

-

+

{% trans %}Request More Languages{% endtrans %}

+

{% trans %} A language is missing in the list? File a ticket in the Pygments trac and we add that as soon - as possible. + as possible.{% endtrans %}

-

Software Used

+

{% trans %}Software Used{% endtrans %}

-

Who?

-

+

{% trans %}Who?{% endtrans %}

+

{% trans %} This paste was founded by Armin Ronacher from the pocoo team and is now maintained by Christopher Grebs. - Pygments is a pocoo project led by Georg Brandl. + Pygments is a pocoo project led by Georg Brandl.{% endtrans %}

-

Privacy

-

+

{% trans %}Privacy{% endtrans %}

+

{% trans %} LodgeIt does not use user accounts because it's logging in for using a pastebin is useles. However this pastebin creates unique user ids for you and for 31 days. Whenever you return to the pastebin it will notify you about replies to your pastes. If you don't want to have that feature you can let lodgeit forget about you by removing the cookie. - Please note that on the next request you will get a new id. + Please note that on the next request you will get a new id.{% endtrans %}

{% endblock %} diff --git a/lodgeit/views/layout.html b/lodgeit/views/layout.html index 96ca38c..4341b73 100644 --- a/lodgeit/views/layout.html +++ b/lodgeit/views/layout.html @@ -19,9 +19,9 @@