目录

Hugo 的多语言设置

或许是闲的,或许是想练练手,本博客开启了中英双语言的模式。本文以简中和英语为例子,记录一下 LoveIt 主题的多语言配置流程。

添加多语言配置

来到博客根目录的config.toml文件,添加如下的代码到其中:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# Multilingual
# 多语言
[languages]
  [languages.en]
    # weight 意为排序权重
    weight = 2
    languageCode = "en"
    languageName = "English"
    hasCJKLanguage = true
    copyright = "This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License."
    [languages.en.menu]
      [[languages.en.menu.main]]
        weight = 1
        identifier = "posts"
        pre = ""
        post = ""
        name = "Posts"
        url = "/posts/"
        title = ""
      [[languages.en.menu.main]]
        weight = 2
        identifier = "tags"
        pre = ""
        post = ""
        name = "Tags"
        url = "/tags/"
        title = ""
      [[languages.en.menu.main]]
        weight = 3
        identifier = "categories"
        pre = ""
        post = ""
        name = "Categories"
        url = "/categories/"
        title = ""
      [[languages.en.menu.main]]
        weight = 4
        identifier = "about"
        pre = ""
        post = ""
        name = "About"
        url = "/about/"
        title = ""
      [[languages.en.menu.main]]
        weight = 5
        identifier = "friend"
        pre = ""
        post = ""
        name = "Friend"
        url = "/friend/"
        title = ""
    [languages.en.params]
      [languages.en.params.search]
        enable = false
        type = "algolia"
        contentLength = 4000
        placeholder = ""
        maxResultLength = 10
        snippetLength = 30
        highlightTag = "em"
        absoluteURL = false
      [languages.en.params.search.algolia]
        index = "xxx"
        appID = ""
        searchKey = ""
      [languages.en.params.footer]
        enable = true
        # LoveIt 新增 | 0.2.0 自定义内容 (支持 HTML 格式)
        custom = ''
        # LoveIt 新增 | 0.2.0 是否显示 Hugo 和主题信息
        hugo = false
        # LoveIt 新增 | 0.2.0 是否显示版权信息
        copyright = true
        # LoveIt 新增 | 0.2.0 是否显示作者
        author = true
        # 网站创立年份
        since = 2023
        # 许可协议信息 (支持 HTML 格式)
        license = '<a rel="license external nofollow noopener noreffer" href="https://creativecommons.org/licenses/by-nc/4.0/" target="_blank">CC BY-NC 4.0</a>'
      [languages.en.params.home]
        rss = 10
      [languages.en.params.home.posts]
          enable = true
          # 主页每页显示文章数量
          paginate = 8
        [languages.en.params.home.profile]
          enable = true
          gravatarEmail = ""
          avatarURL = "/images/avatar.png"
          title = ""
          subtitle = "Ultimate goal: Go to the dock to grab some French fries"
          typeit = true
          social = true
          disclaimer = ""
      [languages.en.params.social]
        GitHub = "xxx"
        Twitter = ""
        Instagram = ""
        Facebook = ""
        Telegram = ""
        Youtubelegacy = ""
        Phone = ""
        Email = "stilig@xxx.com"
        RSS = true

  [languages.zh-cn]
    weight = 1
    languageCode = "zh-CN"
    languageName = "简体中文"
    hasCJKLanguage = true
    copyright = "This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License."
    [languages.zh-cn.menu]
      [[languages.zh-cn.menu.main]]
        weight = 1
        identifier = "posts"
        pre = ""
        post = ""
        name = "归档"
        url = "/posts/"
        title = "Posts"
      [[languages.zh-cn.menu.main]]
        weight = 2
        identifier = "tags"
        pre = ""
        post = ""
        name = "标签"
        url = "/tags/"
        title = "Tags"
      [[languages.zh-cn.menu.main]]
        weight = 3
        identifier = "categories"
        pre = ""
        post = ""
        name = "分类"
        url = "/categories/"
        title = "Categories"
      [[languages.zh-cn.menu.main]]
        weight = 4 
        identifier = "about"
        pre = ""
        post = ""
        name = "关于"
        url = "/about/"
        title = "About"
      [[languages.zh-cn.menu.main]]
        weight = 5
        identifier = "friend"
        pre = ""
        post = ""
        name = "友链"
        url = "/friend/"
        title = "Friend"
    [languages.zh-cn.params]
      [languages.zh-cn.params.search]
        enable = true
        type = "algolia"
        contentLength = 4000
        placeholder = ""
        maxResultLength = 10
        snippetLength = 50
        highlightTag = "em"
        absoluteURL = false
        [languages.zh-cn.params.search.algolia]
          index = "xxx"
          appID = ""
          searchKey = ""
        [languages.zh-cn.params.footer]
          enable = true
          # LoveIt 新增 | 0.2.0 自定义内容 (支持 HTML 格式)
          custom = ''
          # LoveIt 新增 | 0.2.0 是否显示 Hugo 和主题信息
          hugo = false
          # LoveIt 新增 | 0.2.0 是否显示版权信息
          copyright = true
          # LoveIt 新增 | 0.2.0 是否显示作者
          author = true
          # 网站创立年份
          since = 2023
          # ICP 备案信息,仅在中文显示
          icp = '' 
          # 许可协议信息 (支持 HTML 格式)
          license = '<a rel="license external nofollow noopener noreffer" href="https://creativecommons.org/licenses/by-nc/4.0/" target="_blank">CC BY-NC 4.0</a>' 
      [languages.zh-cn.params.home]
        rss = 10
        [languages.zh-cn.params.home.posts]
            enable = true
            # 主页每页显示文章数量
            paginate = 8
        [languages.zh-cn.params.home.profile]
          enable = true
          gravatarEmail = ""
          avatarURL = "/images/avatar.png"
          title = ""
          subtitle = "终极目标:去码头整点薯条"
          typeit = true
          social = true
          disclaimer = ""
      [languages.zh-cn.params.social]
        GitHub = "xxx"
        Weibo = ""
        Steam = ""
        Zhihu = ""
        Douban = ""
        Devto = ""
        Bilibili = ""
        Email = "stilig@xxx.com"
        Phone = ""
        RSS = true

中英文是两套配置(也就是说,添加一种语言 = 添加一组网页),具体的形式需要靠你自行的修改,也可以参考官方的配置文件

修改博文格式

同时我们可以参考官方文章文件来创建文件,即在终端的相应路径下,用下面这个代码来创建一个中文的文章文件:

1
hugo new posts/your_post_name/index.zh-cn.md

而用下面这个代码来创建一个英文的文章文件:

1
hugo new posts/your_post_name/index.en.md

细节修改

因为之前在写文章时,并未开启双语言,所以有些文章的内容并没有涉及语言的设置,但这些部分都已经修改,具体可见下文。

最近更新

具体可以见这里https://stilig.me/posts/enhancing-loveit-three/#%E6%98%BE%E7%A4%BA%E6%9C%80%E8%BF%91%E6%9B%B4%E6%96%B0%E7%9A%84%E5%8D%81%E7%AF%87%E6%96%87%E7%AB%A0

评论功能

具体可见这里https://stilig.me/posts/hugo-adds-giscus/#%E4%BB%8E%E5%AE%98%E7%BD%91%E8%8E%B7%E5%8F%96%E9%85%8D%E7%BD%AE%E4%BF%A1%E6%81%AF

文章过期提醒

具体可见这里https://stilig.me/posts/enhancing-loveit-two/#%E4%BF%AE%E6%94%B9%E5%9B%BD%E9%99%85%E5%8C%96%E6%96%87%E4%BB%B6

警告
本文最后更新于 August 15, 2023,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系删除。
若文章帮助到了您,就不妨点一下👇广告,支持博主吧!