Skip to content

Instantly share code, notes, and snippets.

@navilan
Created September 7, 2012 04:11
Show Gist options
  • Save navilan/3663059 to your computer and use it in GitHub Desktop.
Save navilan/3663059 to your computer and use it in GitHub Desktop.
Locale plugin for multilanguage hyde site
# This file should be placed in the root directory for french language files
# ...
language: fr
lc: fr_CA
# ...
mode: development
# ...
plugins:
- site_defaults.SiteDefaults
- hyde.ext.plugins.meta.MetaPlugin
- hyde.ext.plugins.auto_extend.AutoExtendPlugin
- hyde.ext.plugins.sorter.SorterPlugin
- hyde.ext.plugins.tagger.TaggerPlugin
- hyde.ext.plugins.syntext.SyntextPlugin
- hyde.ext.plugins.textlinks.TextlinksPlugin
- hyde.ext.plugins.urls.UrlCleanerPlugin
context:
data:
# ...
languages:
fr: Français
en: English
meta:
nodemeta: meta.yaml
created: !!timestamp 2012-08-02 14:04:01
encoding: utf-8
lc: 'en_US'
# ...
# -*- coding: utf-8 -*-
from hyde.plugin import Plugin
from jinja2 import contextfunction, Markup
import locale
@contextfunction
def datetime(context, val, fmt=False):
import locale, datetime
fmt = fmt if fmt else locale.nl_langinfo(locale.D_T_FMT)
result = val.strftime(fmt)
return Markup(result.decode('utf-8'))
class SiteDefaults(Plugin):
def template_loaded(self, template):
self.template = template
template.env.globals['datetime'] = datetime
def begin_text_resource(self, resource, text):
if (resource.meta.lc):
self.default_locale = locale.getlocale()
locale.setlocale(locale.LC_ALL,
(resource.meta.lc, resource.meta.encoding))
return text
def end_text_resource(self, resource, text):
if (resource.meta.lc):
locale.setlocale(locale.LC_ALL, self.default_locale)
return text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment