Skip to content

Commit 6cec72c

Browse files
authored
尝试通过插件的方式支持多语言
1 parent 1835eb4 commit 6cec72c

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

_config.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,24 @@ defaults:
113113
hits: true
114114
toc: true
115115
toc_sticky: true
116+
i18n: true
116117
- scope:
117118
path: ""
118119
type: downloads
119120
values:
120121
toc: false
122+
- scope:
123+
path: assets
124+
values:
125+
i18n: false
126+
- scope:
127+
path: 404.md
128+
values:
129+
i18n: false
130+
- scope:
131+
path: robots.txt
132+
values:
133+
i18n: false
121134

122135
# liquid:
123136
# error_mode: warn
@@ -144,6 +157,7 @@ minimal_mistakes_skin_dark: dark
144157

145158
# Site Settings
146159
locale: zh
160+
locales: [zh, zh-TW, en]
147161
# rtl:
148162
title: HMCL 文档
149163
# title_separator: -

_layouts/document.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
<img src="https://hits.zkitefly.eu.org/?tag={{ page.url | absolute_url | url_encode }}" alt="Hits" decoding="async">
77
{% endif %}
88

9+
{% if page.origin_lang and page.lang and page.origin_lang != page.lang %}
10+
<div class="notice--warning">当前页面语言与实际语言不一致</div>
11+
{% endif %}
12+
913
<div class="notice--info">
1014
<p>本文由 {{ page.author | default: '未署名用户' }} 创建{% if page.contributors %},并由 {{ page.contributors | join: ' ' }} 编辑{% endif %}。</p>
1115
{%- if page.note -%}

_plugins/i18n.rb

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
Jekyll::Hooks.register [:site], :pre_render do |site|
2+
default_locale = site.config["locale"] || "en"
3+
locales = Array(site.config["locales"] || default_locale)
4+
locales.unshift(default_locale) unless locales.include?(default_locale)
5+
return if locales.length == 0
6+
7+
doc_map = {}
8+
9+
(site.pages + site.documents).each do |doc|
10+
basename = File.basename(doc.path)
11+
doc_locale = basename[/\.(\w+)\.\w+$/, 1]
12+
next unless doc.data["i18n"] == true
13+
14+
if doc_locale && locales.include?(doc_locale)
15+
default_doc_path = doc.path.sub(/\.#{Regexp.escape(doc_locale)}(?=\.\w+$)/, "")
16+
else
17+
doc_locale = default_locale
18+
default_doc_path = doc.path
19+
end
20+
doc_map[default_doc_path] ||= {}
21+
doc_map[default_doc_path][doc_locale] = doc
22+
end
23+
24+
generate_fallback_doc = lambda do |fallback_doc, locale|
25+
if fallback_doc.is_a?(Jekyll::Page)
26+
base, dir, name = fallback_doc.instance_variable_get(:@base), fallback_doc.instance_variable_get(:@dir), fallback_doc.instance_variable_get(:@name)
27+
new_doc = Jekyll::PageWithoutAFile.new(fallback_doc.site, base, dir, name)
28+
site.pages << new_doc
29+
else
30+
path, collection = fallback_doc.instance_variable_get(:@path), fallback_doc.collection
31+
new_doc = Jekyll::Document.new(path, site: fallback_doc.site, collection: collection)
32+
collection.docs << new_doc
33+
end
34+
35+
new_doc.content = fallback_doc.content
36+
fallback_doc.data.each { |k, v| new_doc.data[k] = v }
37+
new_doc.data["permalink"] = "/#{locale}#{fallback_doc.url}"
38+
new_doc.data["lang"] = locale
39+
end
40+
41+
doc_map.each_value do |docs|
42+
default_doc = docs[default_locale]
43+
next unless default_doc
44+
45+
locales.each do |locale|
46+
if docs[locale]
47+
docs[locale].data["lang"] = locale
48+
docs[locale].data["origin_lang"] = locale
49+
if locale != default_locale
50+
docs[locale].data["permalink"] = "/#{locale}#{default_doc.url}"
51+
docs[locale].instance_variable_set(:@url, nil)
52+
end
53+
else
54+
fallback_doc = default_doc
55+
generate_fallback_doc.call(fallback_doc, locale)
56+
end
57+
end
58+
end
59+
end

0 commit comments

Comments
 (0)