mirror of
https://github.com/getpelican/pelican.git
synced 2025-10-15 20:28:56 +02:00
Add translation metadata support.
Allows user to control which posts are the translations independently of the DEFAULT_LANG setting.
This commit is contained in:
parent
e14a658672
commit
eb640b09e6
3 changed files with 55 additions and 4 deletions
|
|
@ -392,6 +392,19 @@ identifier. If you'd rather not explicitly define the slug this way, you must
|
||||||
then instead ensure that the translated article titles are identical, since the
|
then instead ensure that the translated article titles are identical, since the
|
||||||
slug will be auto-generated from the article title.
|
slug will be auto-generated from the article title.
|
||||||
|
|
||||||
|
If you do not want the original version of one specific article to be detected
|
||||||
|
by the ``DEFAULT_LANG`` setting, use the ``translation`` metadata to specify
|
||||||
|
which posts are translations::
|
||||||
|
|
||||||
|
Foobar is not dead
|
||||||
|
##################
|
||||||
|
|
||||||
|
:slug: foobar-is-not-dead
|
||||||
|
:lang: en
|
||||||
|
:translation: true
|
||||||
|
|
||||||
|
That's true, foobar is still alive!
|
||||||
|
|
||||||
Syntax highlighting
|
Syntax highlighting
|
||||||
-------------------
|
-------------------
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -85,12 +85,28 @@ class TestUtils(LoggedTestCase):
|
||||||
|
|
||||||
def test_process_translations(self):
|
def test_process_translations(self):
|
||||||
# create a bunch of articles
|
# create a bunch of articles
|
||||||
|
# 1: no translation metadata
|
||||||
fr_article1 = get_article(lang='fr', slug='yay', title='Un titre',
|
fr_article1 = get_article(lang='fr', slug='yay', title='Un titre',
|
||||||
content='en français')
|
content='en français')
|
||||||
en_article1 = get_article(lang='en', slug='yay', title='A title',
|
en_article1 = get_article(lang='en', slug='yay', title='A title',
|
||||||
content='in english')
|
content='in english')
|
||||||
|
# 2: reverse which one is the translation thanks to metadata
|
||||||
|
fr_article2 = get_article(lang='fr', slug='yay2', title='Un titre',
|
||||||
|
content='en français')
|
||||||
|
en_article2 = get_article(lang='en', slug='yay2', title='A title',
|
||||||
|
content='in english',
|
||||||
|
extra_metadata={'translation': 'true'})
|
||||||
|
# 3: back to default language detection if all items have the
|
||||||
|
# translation metadata
|
||||||
|
fr_article3 = get_article(lang='fr', slug='yay3', title='Un titre',
|
||||||
|
content='en français',
|
||||||
|
extra_metadata={'translation': 'yep'})
|
||||||
|
en_article3 = get_article(lang='en', slug='yay3', title='A title',
|
||||||
|
content='in english',
|
||||||
|
extra_metadata={'translation': 'yes'})
|
||||||
|
|
||||||
articles = [fr_article1, en_article1]
|
articles = [fr_article1, en_article1, fr_article2, en_article2,
|
||||||
|
fr_article3, en_article3]
|
||||||
|
|
||||||
index, trans = utils.process_translations(articles)
|
index, trans = utils.process_translations(articles)
|
||||||
|
|
||||||
|
|
@ -99,6 +115,16 @@ class TestUtils(LoggedTestCase):
|
||||||
self.assertNotIn(en_article1, trans)
|
self.assertNotIn(en_article1, trans)
|
||||||
self.assertNotIn(fr_article1, index)
|
self.assertNotIn(fr_article1, index)
|
||||||
|
|
||||||
|
self.assertIn(fr_article2, index)
|
||||||
|
self.assertIn(en_article2, trans)
|
||||||
|
self.assertNotIn(fr_article2, trans)
|
||||||
|
self.assertNotIn(en_article2, index)
|
||||||
|
|
||||||
|
self.assertIn(en_article3, index)
|
||||||
|
self.assertIn(fr_article3, trans)
|
||||||
|
self.assertNotIn(en_article3, trans)
|
||||||
|
self.assertNotIn(fr_article3, index)
|
||||||
|
|
||||||
def test_files_changed(self):
|
def test_files_changed(self):
|
||||||
# Test if file changes are correctly detected
|
# Test if file changes are correctly detected
|
||||||
# Make sure to handle not getting any files correctly.
|
# Make sure to handle not getting any files correctly.
|
||||||
|
|
|
||||||
|
|
@ -395,7 +395,9 @@ def process_translations(content_list):
|
||||||
|
|
||||||
Returns a tuple with two lists (index, translations). Index list includes
|
Returns a tuple with two lists (index, translations). Index list includes
|
||||||
items in default language or items which have no variant in default
|
items in default language or items which have no variant in default
|
||||||
language.
|
language. Items with the `translation` metadata set to something else than
|
||||||
|
`False` or `false` will be used as translations, unless all the items with
|
||||||
|
the same slug have that metadata.
|
||||||
|
|
||||||
For each content_list item, sets the 'translations' attribute.
|
For each content_list item, sets the 'translations' attribute.
|
||||||
"""
|
"""
|
||||||
|
|
@ -406,8 +408,18 @@ def process_translations(content_list):
|
||||||
|
|
||||||
for slug, items in grouped_by_slugs:
|
for slug, items in grouped_by_slugs:
|
||||||
items = list(items)
|
items = list(items)
|
||||||
|
# items with `translation` metadata will be used as translations…
|
||||||
|
default_lang_items = list(filter(
|
||||||
|
lambda i: i.metadata.get('translation', 'false').lower()
|
||||||
|
== 'false',
|
||||||
|
items))
|
||||||
|
# …unless all items with that slug are translations
|
||||||
|
if not default_lang_items:
|
||||||
|
default_lang_items = items
|
||||||
|
|
||||||
# find items with default language
|
# find items with default language
|
||||||
default_lang_items = list(filter(attrgetter('in_default_lang'), items))
|
default_lang_items = list(filter(attrgetter('in_default_lang'),
|
||||||
|
default_lang_items))
|
||||||
len_ = len(default_lang_items)
|
len_ = len(default_lang_items)
|
||||||
if len_ > 1:
|
if len_ > 1:
|
||||||
logger.warning('there are %s variants of "%s"' % (len_, slug))
|
logger.warning('there are %s variants of "%s"' % (len_, slug))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue