Commits

Simon Meers  committed fddc058

Carriage-return fixes, project-level settings, updated docs

  • Participants
  • Parent commits ea991df

Comments (0)

Files changed (10)

File dbgettext/__init__.py

 
 def autodiscover():
     """
-    Auto-discover INSTALLED_APPS dbgettext.py modules and fail silently when
-    not present. This forces an import on them to register any admin bits they
-    may want.
+    Auto-discover INSTALLED_APPS gettext.py modules and fail silently when
+    not present. This forces an import on them to register any dbggettext bits 
+    they may want.
     """
     global LOADING
     if LOADING:
 
     import imp
     from django.conf import settings
-
     for app in settings.INSTALLED_APPS:
         try:
             app_path = import_module(app).__path__
             continue
 
         import_module("%s.gettext" % app)
+    
+    # import project-level options
+    if hasattr(settings, 'DBGETTEXT_PROJECT_OPTIONS'):
+        import_module(settings.DBGETTEXT_PROJECT_OPTIONS)
 
     LOADING = False
 

File dbgettext/lexicons/html.py

     ignored = [
         (r'<!--.*?-->', ignore),
         (r'<script.*?/script>', ignore),
+        (r'\r', ignore), # forbidden in gettext, must split on these
     ]
 
     custom = getattr(options, 'custom_lexicon_rules', [])
     ]
 
     text = [
-        (r'[^<>]*[^\s<>]', text),
+        (r'[^\r<>]*[^\s<>]', text),
     ]
     
     lexicon = ignored + custom + tags + whitespace + text

File dbgettext/parser.py

         if self.type == 'text':
             return Token.ALWAYS_TRANSLATE
         elif self.type == 'whitespace':
-            return Token.MAYBE_TRANSLATE
+            if self.raw.find('\r') >= 0: # carriage-returns forbidden
+                return Token.NEVER_TRANSLATE
+            else:
+                return Token.MAYBE_TRANSLATE
         else:
             return Token.NEVER_TRANSLATE
 

File dbgettext/templatetags/__init__.py

+

File dbgettext/templatetags/dbgettext_tags.py

+from dbgettext.parser import parsed_gettext as _parsed_gettext
+from django import template
+
+register = template.Library()
+
+@register.filter
+def parsed_gettext(obj, attribute):
+    return _parsed_gettext(obj, attribute)

File docs/flatpages.rst

+.. _flatpages:
+
+Example Usage: ``django.contrib.flatpages``
+===========================================
+
+Settings
+--------
+``settings.py``::
+
+    DBGETTEXT_PROJECT_OPTIONS = 'my_project.dbgettext_options'
+
+Registration
+------------
+``my_project/dbgettext_options.py``:
+
+    from dbgettext.registry import registry, Options
+    from dbgettext.lexicons import html
+    from django.contrib.flatpages.models import FlatPage
+
+    class FlatPageOptions(Options):
+	attributes = ('title',)
+	parsed_attributes = {'content': html.lexicon}
+
+    registry.register(FlatPage, FlatPageOptions)
+
+
+Template
+--------
+``templates/flatpages/default.html``::
+
+    {% load dbgettext_tags i18n %}
+    <html>
+    <head>
+    <title>{% trans flatpage.title %}</title>
+    </head>
+    <body>
+    {{ flatpage|parsed_gettext:"content"|safe }}
+    </body>
+    </html>

File docs/index.rst

    settings
    parsing
    nesting
+   templatetags
+   flatpages
    FAQ
 

File docs/registration.rst

 
 That's it. Your ``gettext.py`` files will be automatically imported by django-dbgettext, and registered models will be included when running :doc:`dbgettext_export <dbgettext_export>`.
 
+.. _project-level:
+
+--------------------------
+Project-level Registration
+--------------------------
+
+To register models from third-party applications, you can specify a module containing dbgettext registration in the ``DBGETTEXT_PROJECT_OPTIONS`` :ref:`setting <settings>`. For example, the following could be stored in ``my_project/dbgettext_options.py`` and ``DBGETTEXT_PROJECT_OPTIONS`` set to ``my_project.dbgettext_options`` to register ``flatpages`` ::
+
+    from dbgettext.registry import registry, Options
+    from dbgettext.lexicons import html
+    from django.contrib.flatpages.models import FlatPage
+
+    class FlatPageOptions(Options):
+	attributes = ('title',)
+	parsed_attributes = {'content': html.lexicon}
+
+    registry.register(FlatPage, FlatPageOptions)
 
 .. _options:
 

File docs/settings.rst

 
 * ``DBGETTEXT_PATH``: path (absolute or relative to project root) where :doc:`dbgettext_export <dbgettext_export>` should store its output. Defaults to ``locale``.
 * ``DBGETTEXT_ROOT``: name of directory within ``DBGETTEXT_PATH`` (redundancy to provide protection from auto-purging upon export). Defaults to ``dbgettext``.
+* ``DBGETTEXT_PROJECT_OPTIONS``: dotted path to python module containing project-level registration code (e.g. ``my_project.dbgettext_options`` if you have a ``dbgettext_options.py`` file in your ``my_project`` directory).
 * ``DBGETTEXT_INLINE_HTML_TAGS``: tuple of tag names allowed to appear inline within strings parsed with ``dbgettext.lexicons.html``. Defaults to ``('b','i','u','em','strong',)``.
 * ``DBGETTEXT_SPLIT_SENTENCES``: split chunks of text into separate sentences for translation where appropriate. Defaults to ``True``.
 * ``DBGETTEXT_SENTENCE_RE``: compiled regular expression for splitting sentences. Defaults to ``re.compile(r'^(.*?\S[\!\?\.])(\s+)(\S+.*)$', re.DOTALL)``.

File docs/templatetags.rst

+.. _templatetags:
+
+Template Tags and Filters
+=========================
+
+To use dbgettext template tags, simply::
+
+    {% load dbgettext_tags %} 
+
+The following template tags/filters are provided
+
+- ``parsed_gettext``: 
+    usage - ``object|parsed_gettext:"attribute_name"``
+