Skip to content
This repository has been archived by the owner on Mar 26, 2024. It is now read-only.

Date formatting depends on current locale #28

Merged
merged 1 commit into from Oct 23, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -24,7 +24,7 @@
Tinkerer is also highly customizable through Sphinx extensions.
'''

requires = ["Jinja2>=2.3", "Sphinx>=1.1"]
requires = ["Jinja2>=2.3", "Sphinx>=1.1", "Babel>=1.3"]
if sys.version_info[:2] < (2,7) or (sys.version_info.major == 3 and
sys.version_info.minor < 2):
requires.append("argparse>=1.2")
Expand Down
23 changes: 11 additions & 12 deletions tinkerer/ext/metadata.py
Expand Up @@ -12,7 +12,10 @@
import re
import datetime
import locale
from functools import partial
from sphinx.util.compat import Directive
from babel.core import Locale
from babel.dates import format_date
import tinkerer
from tinkerer.ext.uistr import UIStr
from tinkerer.utils import name_from_title
Expand All @@ -25,10 +28,6 @@ def initialize(app):
'''
app.builder.env.blog_metadata = dict()

# make sure we use system locale for date formatting
locale.setlocale(locale.LC_TIME, '')



class Metadata:
'''
Expand Down Expand Up @@ -82,6 +81,12 @@ def get_metadata(app, docname):
Extracts metadata from a document.
'''
env = app.builder.env
language = app.config.language
locale = Locale.parse(language) if language else Locale.default()
format_ui_date = partial(
format_date, format=UIStr.TIMESTAMP_FMT, locale=locale)
format_short_ui_short = partial(
format_date, format=UIStr.TIMESTAMP_FMT_SHORT, locale=locale)

env.blog_metadata[docname] = Metadata()
metadata = env.blog_metadata[docname]
Expand All @@ -104,14 +109,8 @@ def get_metadata(app, docname):

# we format date here instead of inside template due to localization issues
# and Python2 vs Python3 incompatibility
metadata.formatted_date = metadata.date.strftime(UIStr.TIMESTAMP_FMT)
metadata.formatted_date_short = metadata.date.strftime(UIStr.TIMESTAMP_FMT_SHORT)

if (hasattr(metadata.formatted_date, "decode")):
metadata.formatted_date = metadata.formatted_date.decode("utf-8")
if (hasattr(metadata.formatted_date_short, "decode")):
metadata.formatted_date_short = metadata.formatted_date_short.decode("utf-8")

metadata.formatted_date = format_ui_date(metadata.date)
metadata.formatted_date_short = format_short_ui_short(metadata.date)


def process_metadata(app, env):
Expand Down
9 changes: 4 additions & 5 deletions tinkerer/ext/uistr.py
Expand Up @@ -15,10 +15,10 @@
except:
# Python 2
import __builtin__



# check whether unicode builtin exists, otherwise strings are unicode by

# check whether unicode builtin exists, otherwise strings are unicode by
# default so it can be stubbed
if "unicode" not in __builtin__.__dict__:
def unicode(ret, ignore):
Expand All @@ -39,13 +39,12 @@ def __init__(self, app):
UIStr.TAGS = unicode(_("Tags"), "utf-8")
UIStr.TAGS_CLOUD = unicode(_("Tags Cloud"), "utf-8")
UIStr.CATEGORIES = unicode(_("Categories"), "utf-8")
UIStr.TIMESTAMP_FMT = unicode(_('%B %d, %Y'), "utf-8")
UIStr.TIMESTAMP_FMT_SHORT = unicode(_('%b %d'), "utf-8")
UIStr.TIMESTAMP_FMT = unicode(_('MMMM dd, yyyy'), "utf-8")
UIStr.TIMESTAMP_FMT_SHORT = unicode(_('MMM dd'), "utf-8")
UIStr.TAGGED_WITH_FMT = unicode(_('Posts tagged with <span class="title_tag">%s</span>'), "utf-8")
UIStr.FILED_UNDER_FMT = unicode(_('Filed under <span class="title_category">%s</span>'), "utf-8")
UIStr.NEWER = unicode(_("Newer"), "utf-8")
UIStr.OLDER = unicode(_("Older"), "utf-8")
UIStr.PAGE_FMT = unicode(_("Page %d"), "utf-8")
UIStr.READ_MORE = unicode(_("Read more..."), "utf-8")
UIStr.MAIL_HIDDEN_BY_JAVASCRIPT = unicode(_("Javascript must be enabled to see this e-mail address"), "utf-8")