Welcome to PhYsiX Space

Try to set up a place to store notes

0%

Multi-language

Some irrelevant commands

npm update
npm audit
git diff package-lock.json

rm -rf themes/next
npm uninstall hexo-theme-next

npm install hexo-generator-i18n --save
npm install hexo-helper-i18n --save
npm install hexo-renderer-nunjucks --save
npm install moment --save

Modify configuration file

language: [zh-CN, en]
i18n:
type: [page, post]
generator: [archive, category, tag, index]

Added to post.swig

{%- if post.lang %}
{%- set targetLang = 'zh-CN' if post.lang == 'en' else 'en' %}
{%- set targetSlug = post.slug %}
{% set targetPath = post.path | replace(post.slug, 'zh-CN/' + post.slug) if post.lang == 'en' else post.path | replace('zh-CN/', '/') %}

Some wrong commands

{%- if post.lang %}
{%- set targetLang = 'zh-CN' if post.lang == 'en' else 'en' %}
{% set i18nPost = site.posts.findOne({lang: targetLang, title: post.title}) %}
{%- if i18nPost %}
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="fas fa-language"></i>
</span>
<a href="{{ url_for(i18nPost.path) }}" class="lang-switch">
{{ 'Switch to English' if post.lang == 'zh-CN' else '切换到中文' }}
</a>
</span>
{%- endif %}
{%- endif %}

{%- set currentUrl = page.url %}
{%- set targetUrl = currentUrl | replace("/" + post.lang + "/", "/" + targetLang + "/") %}
{%- set targetPath = url_for('/' + targetLang + '/' + page.date.year + '/' + page.date.month + '/' + page.date.day + '/' + targetSlug + '/') %}

Non-exist pages

document.getElementById('lang-switch-link').addEventListener('click', function(event) {
event.preventDefault();
var targetPath = '{{ url_for(targetPath) }}';
fetch(targetPath)
.then(function(response) {
if (response.ok) {
window.location.href = targetPath;
} else {
alert('页面不存在,保持在当前页面');
}
})
.catch(function() {
alert('发生错误,保持在当前页面');
});
});

ultimately used {%- if post.bilingual %} and set

new_post_name: :lang/:title.md
permalink: :lang/:title/