You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've been searching on the "Best Practice" of Hexo's Multitilingual (i18n) feature in recent days, and here are some of the results I've got so far, with detailed usages of each feature.
Multilingual Support for Pages
For built-in pages like archive and category, I chose hexo-generator-i18n. It performs well.
For custom pages, the best way is to place files in different language-identifier folders under the source folder, excluding default language. Here's an example tree:
├─about
│ index.md
│
├─zh-CN
│ └─about
│ index.md
The reason for this is that most language-switcher components will roll back to the normal path (which does not contain language-ids) when switching to default languages, such as theme-next:
This will change path from /zh-CN/about/ to /about/.
Multilingual Support for Posts
For posts, the best way is to place files in different language-identifier folders (same as custom pages) under _posts folder, including default language. Here's an example tree:
language:
- en # Default language
- zh-CNi18n_dir: :langi18n: # hexo-generator-i18n settingstype:
- page# - post # Disable direct copygenerator:
- archive
- category
- tag
- indexpermalink_defaults:
lang: enpermalink: :lang/:title/new_post_name: :lang/:title.md
And you can see your posts in different languages in /en/my-post/ and /zh-CN/my-post/.
The reason we need to place all posts in language folder is, the i18n_diruse permalink to resolve language info, and the permalinkuse new_post_name to resolve page meta. I'm also confused about this, but that's really what I tested. If you don't configure like this, you may find something strange with the generated path and page info.
So I forked hexo-theme-next and made some changes on the language-switcher. It now looks like this:
/** * Get page path given a certain language tag */hexo.extend.helper.register('i18n_path',function(language){const{ path, lang, layout }=this.page;constbase=path.startsWith(lang) ? path.slice(lang.length+1) : path;returnthis.url_for(`${this.languages.indexOf(language)===0&&layout!=='post' ? '' : '/'+language}/${base}`);});
It will rollback to default path without language only when layout !== 'post'. This performs well when testing.
And there are still some small issues:
The index page will show all posts with all languages. I think maybe someone can combine hexo-generator-index-multi-lang with hexo-generator-i18n to let hexo-generator-i18n also generate correct indexes with posts of each language.
The language of sidebar won't change when navigating across languages, not using language-switcher. (maybe this is an issue for hexo-theme-next.
Hope that Hexo will eventually be able to fully support i18n for all pages.
The text was updated successfully, but these errors were encountered:
Check List
I've been searching on the "Best Practice" of Hexo's Multitilingual (i18n) feature in recent days, and here are some of the results I've got so far, with detailed usages of each feature.
Multilingual Support for Pages
For built-in pages like
archive
andcategory
, I chose hexo-generator-i18n. It performs well.For custom pages, the best way is to place files in different language-identifier folders under the
source
folder, excluding default language. Here's an example tree:The reason for this is that most
language-switcher
components will roll back to the normal path (which does not contain language-ids) when switching to default languages, such as theme-next:For config like this:
This will change path from
/zh-CN/about/
to/about/
.Multilingual Support for Posts
For posts, the best way is to place files in different language-identifier folders (same as custom pages) under
_posts
folder, including default language. Here's an example tree:Then, set config like this:
And you can see your posts in different languages in
/en/my-post/
and/zh-CN/my-post/
.The reason we need to place all posts in language folder is, the
i18n_dir
usepermalink
to resolve language info, and thepermalink
usenew_post_name
to resolve page meta. I'm also confused about this, but that's really what I tested. If you don't configure like this, you may find something strange with the generated path and page info.So I forked
hexo-theme-next
and made some changes on thelanguage-switcher
. It now looks like this:It will rollback to default path without language only when
layout !== 'post'
. This performs well when testing.And there are still some small issues:
The index page will show all posts with all languages. I think maybe someone can combine hexo-generator-index-multi-lang with hexo-generator-i18n to let hexo-generator-i18n also generate correct indexes with posts of each language.
The language of sidebar won't change when navigating across languages, not using language-switcher. (maybe this is an issue for hexo-theme-next.
Hope that Hexo will eventually be able to fully support i18n for all pages.
The text was updated successfully, but these errors were encountered: