Skip to content

Commit bae4822

Browse files
committed
feed 多语言支持
1 parent bf7c3ba commit bae4822

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed

_layouts/plugins/feed.xml

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
{% if page.xsl %}
3+
<?xml-stylesheet type="text/xml" href="{{ '/feed.xslt.xml' | absolute_url }}"?>
4+
{% endif %}
5+
{%- assign locale = page.locale | default: site.locale | default: "en" %}
6+
{%- assign i18n_text = site.data.plugins.i18n | i18n: page.locale, "ui-text" -%}
7+
{%- assign site_title = i18n_text.data.site.title | default: site.title -%}
8+
{%- assign site_description = i18n_text.data.site.description | default: site.description -%}
9+
{%- capture home_url %}/{% if locale != site.locale %}{{ locale }}/{% endif %}{% endcapture -%}
10+
<feed xmlns="http://www.w3.org/2005/Atom" {% if locale %}xml:lang="{{ locale }}"{% endif %}>
11+
<generator uri="https://jekyllrb.com/" version="{{ jekyll.version }}">Jekyll</generator>
12+
<link href="{{ page.url | absolute_url }}" rel="self" type="application/atom+xml" />
13+
<link href="{{ home_url | absolute_url }}" rel="alternate" type="text/html" {% if locale %}hreflang="{{ locale }}" {% endif %}/>
14+
<updated>{{ site.time | date_to_xmlschema }}</updated>
15+
<id>{{ page.url | absolute_url | xml_escape }}</id>
16+
17+
{% assign title = site_title | default: site.name %}
18+
{% if page.collection != "posts" %}
19+
{% assign collection = page.collection | capitalize %}
20+
{% assign title = title | append: " | " | append: collection %}
21+
{% endif %}
22+
{% if page.category %}
23+
{% assign category = page.category | capitalize %}
24+
{% assign title = title | append: " | " | append: category %}
25+
{% endif %}
26+
27+
{% if title %}
28+
<title type="html">{{ title | smartify | xml_escape }}</title>
29+
{% endif %}
30+
31+
{% if site_description %}
32+
<subtitle>{{ site_description | xml_escape }}</subtitle>
33+
{% endif %}
34+
35+
{% if site.author %}
36+
<author>
37+
<name>{{ site.author.name | default: site.author | xml_escape }}</name>
38+
{% if site.author.email %}
39+
<email>{{ site.author.email | xml_escape }}</email>
40+
{% endif %}
41+
{% if site.author.uri %}
42+
<uri>{{ site.author.uri | xml_escape }}</uri>
43+
{% endif %}
44+
</author>
45+
{% endif %}
46+
47+
{% if page.tags %}
48+
{% assign posts = site.tags[page.tags] %}
49+
{% else %}
50+
{% assign posts = site[page.collection] %}
51+
{% endif %}
52+
{% if page.category %}
53+
{% assign posts = posts | where: "categories", page.category %}
54+
{% endif %}
55+
{% unless site.show_drafts %}
56+
{% assign posts = posts | where_exp: "post", "post.draft != true" %}
57+
{% endunless %}
58+
{% assign posts = posts | where: "locale", locale | sort: "date" | reverse %}
59+
{% assign posts_limit = site.feed.posts_limit | default: 10 %}
60+
{% for post in posts limit: posts_limit %}
61+
<entry{% if post.locale %}{{" "}}xml:lang="{{ post.locale }}"{% endif %}>
62+
{% assign post_title = post.title | smartify | strip_html | normalize_whitespace | xml_escape %}
63+
64+
<title type="html">{{ post_title }}</title>
65+
<link href="{{ post.url | absolute_url }}" rel="alternate" type="text/html" title="{{ post_title }}" />
66+
<published>{{ post.date | date_to_xmlschema }}</published>
67+
<updated>{{ post.last_modified_at | default: post.date | date_to_xmlschema }}</updated>
68+
<id>{{ post.id | absolute_url | xml_escape }}</id>
69+
{% assign excerpt_only = post.feed.excerpt_only | default: site.feed.excerpt_only %}
70+
{% unless excerpt_only %}
71+
<content type="html" xml:base="{{ post.url | absolute_url | xml_escape }}"><![CDATA[{{ post.content | strip }}]]></content>
72+
{% endunless %}
73+
74+
{% assign post_author = post.author | default: post.authors[0] | default: site.author %}
75+
{% assign post_author = site.data.authors[post_author] | default: post_author %}
76+
{% assign post_author_email = post_author.email | default: nil %}
77+
{% assign post_author_uri = post_author.uri | default: nil %}
78+
{% assign post_author_name = post_author.name | default: post_author %}
79+
80+
<author>
81+
<name>{{ post_author_name | default: "" | xml_escape }}</name>
82+
{% if post_author_email %}
83+
<email>{{ post_author_email | xml_escape }}</email>
84+
{% endif %}
85+
{% if post_author_uri %}
86+
<uri>{{ post_author_uri | xml_escape }}</uri>
87+
{% endif %}
88+
</author>
89+
90+
{% if post.category %}
91+
<category term="{{ post.category | xml_escape }}" />
92+
{% elsif post.categories %}
93+
{% for category in post.categories %}
94+
<category term="{{ category | xml_escape }}" />
95+
{% endfor %}
96+
{% endif %}
97+
98+
{% for tag in post.tags %}
99+
<category term="{{ tag | xml_escape }}" />
100+
{% endfor %}
101+
102+
{% assign post_summary = post.description | default: post.excerpt %}
103+
{% if post_summary and post_summary != empty %}
104+
<summary type="html"><![CDATA[{{ post_summary | strip_html | normalize_whitespace }}]]></summary>
105+
{% endif %}
106+
107+
{% assign post_image = post.image.path | default: post.image %}
108+
{% if post_image %}
109+
{% unless post_image contains "://" %}
110+
{% assign post_image = post_image | absolute_url %}
111+
{% endunless %}
112+
<media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="{{ post_image | xml_escape }}" />
113+
<media:content medium="image" url="{{ post_image | xml_escape }}" xmlns:media="http://search.yahoo.com/mrss/" />
114+
{% endif %}
115+
</entry>
116+
{% endfor %}
117+
</feed>

_plugins/i18n.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
module JekyllFeed
2+
class Generator < Jekyll::Generator
3+
def feed_source_path
4+
@feed_source_path ||= File.expand_path "_layouts/plugins/feed.xml", @site.config["source"]
5+
end
6+
end
7+
end
8+
19
module I18nFilter
210
def i18n(hash, locale, key)
311
return nil unless hash.is_a?(Hash) && locale.is_a?(String) && key.is_a?(String)

0 commit comments

Comments
 (0)