-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Roberto Alsina
committed
May 2, 2018
1 parent
390c733
commit 42d6e2b
Showing
1 changed file
with
13 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,37 @@ | ||
from nikola.plugin_categories import ConfigPlugin | ||
from nikola import utils | ||
import locale | ||
import datetime | ||
from babel import Locale, dates | ||
import platform | ||
|
||
from babel import dates | ||
|
||
from nikola import utils | ||
from nikola.plugin_categories import ConfigPlugin | ||
|
||
|
||
class BabelDates(ConfigPlugin): | ||
""" | ||
Use Babel to format dates | ||
""" | ||
|
||
def __init__(self): | ||
self.name = 'babelDates' | ||
|
||
def set_site(self, site): | ||
self.site = site | ||
if self.site.config['DATE_FANCINESS'] == 0 and not self.site.config.get('BABEL_DATE_FORMAT') is None: | ||
babel_date_format = utils.TranslatableSetting('BABEL_DATE_FORMAT', self.site.config['BABEL_DATE_FORMAT'], self.site.config['TRANSLATIONS']) | ||
babel_date_format = utils.TranslatableSetting( | ||
'BABEL_DATE_FORMAT', self.site.config['BABEL_DATE_FORMAT'], self.site.config['TRANSLATIONS']) | ||
for lang in self.site.config['TRANSLATIONS']: | ||
try: | ||
self.site.config['DATE_FORMAT'][lang] = babel_date_format(lang) | ||
self.site.config['DATE_FORMAT'][lang] = babel_date_format( | ||
lang) | ||
except KeyError: | ||
self.site.config['DATE_FORMAT'][lang] = 'medium' | ||
utils.LocaleBorg().add_handler(formatted_date_handler = self.babel_date_formatter) | ||
utils.LocaleBorg().add_handler(formatted_date_handler=self.babel_date_formatter) | ||
super(BabelDates, self).set_site(site) | ||
|
||
def babel_date_formatter(self, date_format, date, lang): | ||
if platform.system() == 'Windows': | ||
norm_locale = locale.normalize(lang).split('.')[0] | ||
else: | ||
norm_locale = self.site.config['LOCALES'][lang] | ||
return dates.format_datetime(date, format = date_format, locale = norm_locale) | ||
return dates.format_datetime(date, format=date_format, locale=norm_locale) |