forked from github/pelican
Articles translations are enabled. To use this feature, you need to
add a Lang meta into the each article and set DEFAULT_LANG setting which is 'en' by default. Than, only articles in the default language (or without variant in default language) will be shown in the index of the blog. And each Article will have translations list. The same is applicable to the static Pages. Also, separate feeds are generated for each language except default.
This commit is contained in:
parent
1a31c74d5c
commit
3afdb8fcff
4 changed files with 79 additions and 8 deletions
|
|
@ -1,11 +1,13 @@
|
|||
from operator import attrgetter
|
||||
from itertools import chain
|
||||
from datetime import datetime
|
||||
from collections import defaultdict
|
||||
import os
|
||||
|
||||
from jinja2 import Environment, FileSystemLoader
|
||||
from jinja2.exceptions import TemplateNotFound
|
||||
|
||||
from pelican.utils import update_dict, copytree
|
||||
from pelican.utils import update_dict, copytree, process_translations
|
||||
from pelican.contents import Article, Page, is_valid_content
|
||||
from pelican.readers import read_file
|
||||
|
||||
|
|
@ -75,7 +77,8 @@ class ArticlesGenerator(Generator):
|
|||
|
||||
def __init__(self, *args, **kwargs):
|
||||
"""initialize properties"""
|
||||
self.articles = []
|
||||
self.articles = [] # only articles in default language
|
||||
self.translations = []
|
||||
self.dates = {}
|
||||
self.tags = {}
|
||||
self.categories = {}
|
||||
|
|
@ -100,6 +103,15 @@ class ArticlesGenerator(Generator):
|
|||
self.settings['CATEGORY_FEED_RSS'] % cat,
|
||||
feed_type='rss')
|
||||
|
||||
translations_feeds = defaultdict(list)
|
||||
for article in self.translations:
|
||||
translations_feeds[article.lang].append(article)
|
||||
|
||||
for lang, items in translations_feeds.items():
|
||||
items.sort(key=attrgetter('date'), reverse=True)
|
||||
writer.write_feed(items, self.context,
|
||||
self.settings['TRANSLATION_FEED'] % lang)
|
||||
|
||||
|
||||
def generate_pages(self, writer):
|
||||
"""Generate the pages on the disk
|
||||
|
|
@ -116,7 +128,7 @@ class ArticlesGenerator(Generator):
|
|||
for cat in self.categories:
|
||||
write('category/%s.html' % cat, templates['category'], self.context,
|
||||
category=cat, articles=self.categories[cat])
|
||||
for article in self.articles:
|
||||
for article in chain(self.translations, self.articles):
|
||||
write('%s' % article.url,
|
||||
templates['article'], self.context, article=article,
|
||||
category=article.category)
|
||||
|
|
@ -126,6 +138,7 @@ class ArticlesGenerator(Generator):
|
|||
|
||||
# return the list of files to use
|
||||
files = self.get_files(self.path, exclude=['pages',])
|
||||
all_articles = []
|
||||
for f in files:
|
||||
content, metadatas = read_file(f)
|
||||
|
||||
|
|
@ -149,11 +162,17 @@ class ArticlesGenerator(Generator):
|
|||
if not is_valid_content(article, f):
|
||||
continue
|
||||
|
||||
update_dict(self.categories, article.category, article)
|
||||
if hasattr(article, 'tags'):
|
||||
for tag in article.tags:
|
||||
update_dict(self.tags, tag, article)
|
||||
self.articles.append(article)
|
||||
all_articles.append(article)
|
||||
|
||||
self.articles, self.translations = process_translations(all_articles)
|
||||
|
||||
for article in self.articles:
|
||||
# only main articles are listed in categories, not translations
|
||||
update_dict(self.categories, article.category, article)
|
||||
|
||||
|
||||
# sort the articles by date
|
||||
self.articles.sort(key=attrgetter('date'), reverse=True)
|
||||
|
|
@ -175,20 +194,23 @@ class PagesGenerator(Generator):
|
|||
super(PagesGenerator, self).__init__(*args, **kwargs)
|
||||
|
||||
def generate_context(self):
|
||||
all_pages = []
|
||||
for f in self.get_files(os.sep.join((self.path, 'pages'))):
|
||||
content, metadatas = read_file(f)
|
||||
page = Page(content, metadatas, settings=self.settings,
|
||||
filename=f)
|
||||
if not is_valid_content(page, f):
|
||||
continue
|
||||
self.pages.append(page)
|
||||
all_pages.append(page)
|
||||
|
||||
self.pages, self.translations = process_translations(all_pages)
|
||||
|
||||
self._update_context(('pages', ))
|
||||
self.context['PAGES'] = self.pages
|
||||
|
||||
def generate_output(self, writer):
|
||||
templates = self.get_templates()
|
||||
for page in self.pages:
|
||||
for page in chain(self.translations, self.pages):
|
||||
writer.write_file('pages/%s' % page.url, templates['page'],
|
||||
self.context, page=page)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue