HUGO

  • 新闻
  • 文档
  • 主题
  • 作品展示
  • 社区
  • GitHub
    • English
    • 中文

What's on this Page

  • 配置语言
    • 禁用语言
    • 配置多语种多主机
    • 分类和黑色星期五
  • 翻译您的内容
    • 按文件名翻译
    • 通过内容目录翻译
    • 绕过默认的链接。
    • 本地化固定链接
    • 捆绑页
  • 引用所翻译的内容
    • 列出所有可用的语言
  • 字符串翻译
  • 自定义日期
  • 菜单
  • 缺少翻译
  • 多语言支持主题
CONTENT MANAGEMENT

多语言模式

Hugo支持并排创建多语言网站.

你应该在你的站点配置了languages部分定义可用的语言。

另请参见Hugo多语言第1部分:内容翻译

配置语言

下面是一个多语种Hugo项目站点得配置例子:

config.
     
DefaultContentLanguage: en
copyright: Everything is mine
languages:
  en:
    params:
      linkedin: https://linkedin.com/whoever
    title: My blog
    weight: 1
  fr:
    params:
      linkedin: https://linkedin.com/fr/whoever
      navigation:
        help: Aide
    title: Mon blogue
    weight: 2
params:
  navigation:
    help: Help
DefaultContentLanguage = "en"
copyright = "Everything is mine"

[languages]
  [languages.en]
    title = "My blog"
    weight = 1
    [languages.en.params]
      linkedin = "https://linkedin.com/whoever"
  [languages.fr]
    title = "Mon blogue"
    weight = 2
    [languages.fr.params]
      linkedin = "https://linkedin.com/fr/whoever"
      [languages.fr.params.navigation]
        help = "Aide"

[params]
  [params.navigation]
    help = "Help"
{
   "DefaultContentLanguage": "en",
   "copyright": "Everything is mine",
   "languages": {
      "en": {
         "params": {
            "linkedin": "https://linkedin.com/whoever"
         },
         "title": "My blog",
         "weight": 1
      },
      "fr": {
         "params": {
            "linkedin": "https://linkedin.com/fr/whoever",
            "navigation": {
               "help": "Aide"
            }
         },
         "title": "Mon blogue",
         "weight": 2
      }
   },
   "params": {
      "navigation": {
         "help": "Help"
      }
   }
}

在 languages 块没有定义的所有内容将回退到该key的全局值 (e.g., 版权为英语en语言). 这也适用于params, 如上help证明: 您将获得在法语值Aide 和 Help在所有语言中没有这个参数设置.

通过上面的配置中,所有的内容,网站地图,RSS提要,paginations,和分类页/后面以英文(默认内容语言)呈现接着/fr后以法语呈现.

当与前面的问题Params在单一页面模板工作, 忽略params在翻译的值.

defaultContentLanguage 设置项目的默认语言. 如果没有设置,默认语言为en。

如果默认语言需要低于它自己的语言代码(/en)被渲染和其他人一样, 设置 defaultContentLanguageInSubdir: true.

只有明显的非全局选项可以为每个语言来覆盖。 全局选项的例子是baseURL,buildDrafts等。

禁用语言

您可以禁用一个或更多的语言。 在一个新的翻译工作时,这可能是有用的。

disableLanguages = ["fr", "ja"]

请注意,您不能禁用默认内容语言。

我们一直在这个作为一个独立的设置,使其通过OS环境更易于设置:

HUGO_DISABLELANGUAGES="fr ja" hugo

如果您已经禁用语言的config.toml列表, 你可以在这样的发展使他们:

HUGO_DISABLELANGUAGES=" " hugo server

配置多语种多主机

从 Hugo 0.31 我们支持多主机配置多种语言。 看到这个问题的详细信息。

这意味着,你现在可以配置每language一个baseURL:

如果baseURL被设置在language水平,那么所有的语言都必​​须有一个,他们都必须是不同的。

例:

config.
     
languages:
  en:
    baseURL: https://example.com
    languageName: English
    title: In English
    weight: 2
  fr:
    baseURL: https://example.fr
    languageName: Français
    title: En Français
    weight: 1
[languages]
  [languages.en]
    baseURL = "https://example.com"
    languageName = "English"
    title = "In English"
    weight = 2
  [languages.fr]
    baseURL = "https://example.fr"
    languageName = "Français"
    title = "En Français"
    weight = 1
{
   "languages": {
      "en": {
         "baseURL": "https://example.com",
         "languageName": "English",
         "title": "In English",
         "weight": 2
      },
      "fr": {
         "baseURL": "https://example.fr",
         "languageName": "Français",
         "title": "En Français",
         "weight": 1
      }
   }
}

通过上述,两个网站将生成到public用自己的根:

public
├── en
└── fr

所有URL(i.e .Permalink etc.)都会从根产生。 所以,上面的英文主页上都会有.Permalink设置成https://example.com/。

当你运行 hugo server 我们会启动多个HTTP服务器。 您通常会看到这样的事情在控制台:

Web Server is available at 127.0.0.1:1313 (bind address 127.0.0.1)
Web Server is available at 127.0.0.1:1314 (bind address 127.0.0.1)
Press Ctrl+C to stop

服务器之间的实时重载和 --navigateToChanged 按预期方式工作。

分类和黑色星期五

分类和黑色星期五配置也可以按语言设定:

config.
     
Taxonomies:
  tag: tags
blackfriday:
  angledQuotes: true
  hrefTargetBlank: true
languages:
  en:
    blackfriday:
      angledQuotes: false
    title: English
    weight: 1
  fr:
    Taxonomies:
      plaque: plaques
    title: Français
    weight: 2
[Taxonomies]
  tag = "tags"

[blackfriday]
  angledQuotes = true
  hrefTargetBlank = true

[languages]
  [languages.en]
    title = "English"
    weight = 1
    [languages.en.blackfriday]
      angledQuotes = false
  [languages.fr]
    title = "Français"
    weight = 2
    [languages.fr.Taxonomies]
      plaque = "plaques"
{
   "Taxonomies": {
      "tag": "tags"
   },
   "blackfriday": {
      "angledQuotes": true,
      "hrefTargetBlank": true
   },
   "languages": {
      "en": {
         "blackfriday": {
            "angledQuotes": false
         },
         "title": "English",
         "weight": 1
      },
      "fr": {
         "Taxonomies": {
            "plaque": "plaques"
         },
         "title": "Français",
         "weight": 2
      }
   }
}

翻译您的内容

有来管理你的内容翻译两种方式。 这两个确保每个页面分配一个语言,并链接到其对应的翻译。

按文件名翻译

考虑下面的例子:

  1. /content/about.en.md
  2. /content/about.fr.md

第一个文件指定的英语语言被链接到第二位。 第二个文件被指定法语,并链接到第一。

Their language is assigned according to the language code added as a suffix to the filename.

By having the same path and base filename, the content pieces are linked together as translated pages.

If a file has no language code, it will be assigned the default language.

通过内容目录翻译

该系统采用了每种语言的不同内容目录。每种语言的内容目录使用contentDir参数组。

config.
     
languages:
  en:
    contentDir: content/english
    languageName: English
    weight: 10
  fr:
    contentDir: content/french
    languageName: Français
    weight: 20
[languages]
  [languages.en]
    contentDir = "content/english"
    languageName = "English"
    weight = 10
  [languages.fr]
    contentDir = "content/french"
    languageName = "Français"
    weight = 20
{
   "languages": {
      "en": {
         "contentDir": "content/english",
         "languageName": "English",
         "weight": 10
      },
      "fr": {
         "contentDir": "content/french",
         "languageName": "Français",
         "weight": 20
      }
   }
}

contentDir的值可以是任何有效路径 - 甚至绝对路径引用。 唯一的限制是,内容目录不能重叠。

考虑到与上述配置相结合下面的例子:

  1. /content/english/about.md
  2. /content/french/about.md

第一个文件指定的英语语言被链接到第二位。 第二个文件被指定法语,并链接到第一。

Their language is assigned according to the content directory they are placed in.

By having the same path and basename (relative to their language content directory), the content pieces are linked together as translated pages.

绕过默认的链接。

任何网页在前面的问题共享相同的translationKey集将与作为翻译的页面,无论基本名称或位置.

考虑下面的例子:

  1. /content/about-us.en.md
  2. /content/om.nn.md
  3. /content/presentation/a-propos.fr.md
# set in all three pages
translationKey: "about"

By setting the translationKey front matter param to about in all three pages, they will be linked as translated pages.

本地化固定链接

由于路径和文件名是用来处理连接,所有翻译的页面将共享相同的URL (除了语言子目录).

To localize the URLs, the slug or url front matter param can be set in any of the non-default language file.

For example, a French translation (content/about.fr.md) can have its own localized slug.

     
Title: A Propos
slug: a-propos
Title = "A Propos"
slug = "a-propos"
{
   "Title": "A Propos",
   "slug": "a-propos"
}

At render, Hugo will build both /about/ and /fr/a-propos/ while maintaining their translation linking.

If using url, remember to include the language part as well: /fr/compagnie/a-propos/.

捆绑页

为了避免重复文件的负担,每个页面捆绑继承其链接翻译的页面束除了内容文件的资源(降价文件,HTML文件等)。

Therefore, from within a template, the page will have access to the files from all linked pages’ bundles.

If, across the linked bundles, two or more files share the same basename, only one will be included and chosen as follows:

  • File from current language bundle, if present.
  • First file found across bundles by order of language Weight.

Page Bundle resources follow the same language assignment logic as content files, both by filename (image.jpg, image.fr.jpg) and by directory (english/about/header.jpg, french/about/header.jpg).

引用所翻译的内容

要创建的链接翻译内容的列表,请使用类似下面的模板:

layouts/partials/i18nlist.html

{{ if .IsTranslated }}
<h4>{{ i18n "translations" }}</h4>
<ul>
    {{ range .Translations }}
    <li>
        <a href="{{ .Permalink }}">{{ .Lang }}: {{ .Title }}{{ if .IsPage }} ({{ i18n "wordCount" . }}){{ end }}</a>
    </li>
    {{ end }}
</ul>
{{ end }}

The above can be put in a partial (i.e., inside layouts/partials/) and included in any template, whether a single content page or the homepage. It will not print anything if there are no translations for a given page.

The above also uses the i18n function described in the next section.

列出所有可用的语言

在Page上.AllTranslations可以用来列出所有的翻译, 包括页面本身. 在主页上可以用来建立一个语言导航:

layouts/partials/allLanguages.html

<ul>
{{ range $.Site.Home.AllTranslations }}
<li><a href="{{ .Permalink }}">{{ .Language.LanguageName }}</a></li>
{{ end }}
</ul>

字符串翻译

Hugo 使用 go-i18n 支持字符串翻译. 查看该项目的源代码库找工具,这将帮助您管理您的翻译工作流程.

Translations are collected from the themes/<THEME>/i18n/ folder (built into the theme), as well as translations present in i18n/ at the root of your project. In the i18n, the translations will be merged and take precedence over what is in the theme folder. Language files should be named according to RFC 5646 with names such as en-US.toml, fr.toml, etc.

从 Hugo 0.31 您不再需要使用一个有效的语言代码。它可以是任何东西。

查看 https://github.com/gohugoio/hugo/issues/3564

从你的模板中,这样使用i18n功能:

{{ i18n "home" }}

这将使用一个像这样的在i18n/en-US.toml里定义 :

[home]
other = "Home"

通常你想在翻译的字符串使用页面变量。 要做到这一点, 当调用 i18n 传 “.” 上下文:

{{ i18n "wordCount" . }}

这将使用像这个在i18n/en-US.toml里定义 :

[wordCount]
other = "This article has {{ .WordCount }} words."

单数和复数形式的例子:

[readingTime]
one = "One minute to read"
other = "{{.Count}} minutes to read"

然后在模板:

{{ i18n "readingTime" .ReadingTime }}

自定义日期

在写这篇文章的时候,Go还没有为国际日期语言环境的支持,但如果你做了一些工作,你可以模拟它。 例如,如果你想使用法语月份名称, 你可以用这个内容添加一个数据文件如 data/mois.yaml:

1: "janvier"
2: "février"
3: "mars"
4: "avril"
5: "mai"
6: "juin"
7: "juillet"
8: "août"
9: "septembre"
10: "octobre"
11: "novembre"
12: "décembre"

…随后指数在模板的非英语日期名像现在这样:

<time class="post-date" datetime="{{ .Date.Format '2006-01-02T15:04:05Z07:00' | safeHTML }}">
  Article publié le {{ .Date.Day }} {{ index $.Site.Data.mois (printf "%d" .Date.Month) }} {{ .Date.Year }} (dernière modification le {{ .Lastmod.Day }} {{ index $.Site.Data.mois (printf "%d" .Lastmod.Month) }} {{ .Lastmod.Year }})
</time>

This technique extracts the day, month and year by specifying .Date.Day, .Date.Month, and .Date.Year, and uses the month number as a key, when indexing the month name data file.

菜单

您可以定义菜单为各自独立的语言。 Creating multilingual menus works just like creating regular menus, except they’re defined in language-specific blocks in the configuration file:

defaultContentLanguage = "en"

[languages.en]
weight = 0
languageName = "English"

[[languages.en.menu.main]]
url    = "/"
name   = "Home"
weight = 0


[languages.de]
weight = 10
languageName = "Deutsch"

[[languages.de.menu.main]]
url    = "/"
name   = "Startseite"
weight = 0

The rendering of the main navigation works as usual. .Site.Menus will just contain the menu in the current language. Note that absLangURL below will link to the correct locale of your website. Without it, menu entries in all languages would link to the English version, since it’s the default content language that resides in the root directory.

<ul>
    {{- $currentPage := . -}}
    {{ range .Site.Menus.main -}}
    <li class="{{ if $currentPage.IsMenuCurrent "main" . }}active{{ end }}">
        <a href="{{ .URL | absLangURL }}">{{ .Name }}</a>
    </li>
    {{- end }}
</ul>

缺少翻译

如果字符串没有翻译当前语言,Hugo将使用值从默认语言。 如果没有默认值设置为空字符串将被显示。

While translating a Hugo website, it can be handy to have a visual indicator of missing translations. The enableMissingTranslationPlaceholders configuration option will flag all untranslated strings with the placeholder [i18n] identifier, where identifier is the id of the missing translation.

Hugo will generate your website with these missing translation placeholders. It might not be suitable for production environments.

For merging of content from other languages (i.e. missing content translations), see lang.Merge.

To track down missing translation strings, run Hugo with the --i18n-warnings flag:

 hugo --i18n-warnings | grep i18n
i18n|MISSING_TRANSLATION|en|wordCount

多语言支持主题

为了支持多语言模式,你的主题,一些注意事项必须采取模板中的URL。 如果有一种以上的语言,URL必须符合下列条件:

  • 从内置.Permalink或.RelPermalink来吧
  • 可与relLangURL template function 或者 absLangURL template function构建 要么 与{{ .LanguagePrefix }}前缀

如果定义一种以上的语言,在LanguagePrefix变量将等于/en(或任何你CurrentLanguage是)。 如果未启用,这将是一个空字符串(因此对于单一语言Hugo网站是无害的)。

See Also

  • i18n
  • absLangURL
  • relLangURL
  • uniq
  • lang.Merge
  • 关于 Hugo
    • 概述
    • Hugo的安全模型
    • Hugo and GDPR
    • 什么是Hugo
    • Hugo 特征
    • 静态的好处
    • 证书
  • 入门
    • 入门概述
    • 快速开始
    • 安装 Hugo
    • 基本用法
    • 目录结构
    • 配置
    • 外部学习资源
  • Hugo 模块
    • Hugo 模块概述
    • 配置模块
    • 使用Hugo模块
    • 主题组件
  • 内容管理
    • 内容管理概述
    • 组织
    • 捆绑页
    • 内容格式
    • 前面的问题
    • 构建选项
    • 网页资源
    • 图像处理
    • 简码
    • 相关内容
    • 章节
    • 内容类型
    • 原型
    • 分类
    • 摘要
    • 链接和交叉引用
    • URL管理
    • 菜单
    • 目录
    • 静态文件
    • 注释
    • 多种语言 和 国际化
    • 语法高亮
  • 模板
    • 模板概述
    • 介绍
    • 模板查找顺序
    • 自定义输出格式
    • 基本模板和模块
    • 列表页面模板
    • 首页模板
    • 章节模板
    • 分类模板
    • 单页模板
    • 内容视图模板
    • 数据模板
    • 部件模板
    • 简码模板
    • 本地文件模板
    • 404页
    • 菜单模板
    • 分页
    • RSS 模板
    • 网站地图模板
    • Robots.txt
    • 内置模板
    • 另类模板
    • 模板调试
  • 函数
    • 函数快速参考
    • .AddDate
    • .Format
    • .Get
    • .GetPage
    • .HasMenuCurrent
    • .IsMenuCurrent
    • .Param
    • .Render
    • .RenderString
    • .Scratch
    • .Unix
    • absLangURL
    • absURL
    • after
    • anchorize
    • append
    • apply
    • base64
    • chomp
    • complement
    • cond
    • countrunes
    • countwords
    • dateFormat
    • default
    • delimit
    • dict
    • echoParam
    • emojify
    • eq
    • errorf and warnf
    • fileExists
    • findRE
    • first
    • float
    • ge
    • getenv
    • group
    • gt
    • hasPrefix
    • highlight
    • htmlEscape
    • htmlUnescape
    • hugo
    • humanize
    • i18n
    • in
    • index
    • int
    • intersect
    • isset
    • jsonify
    • lang.Merge
    • lang.NumFmt
    • last
    • le
    • lower
    • lt
    • markdownify
    • Math
    • md5
    • merge
    • ne
    • now
    • os.Stat
    • partialCached
    • path.Base
    • path.Dir
    • path.Ext
    • path.Join
    • path.Split
    • plainify
    • pluralize
    • print
    • printf
    • println
    • querify
    • range
    • readDir
    • readFile
    • ref
    • reflect.IsMap
    • reflect.IsSlice
    • relLangURL
    • relref
    • relURL
    • replace
    • replaceRE
    • safeCSS
    • safeHTML
    • safeHTMLAttr
    • safeJS
    • safeURL
    • seq
    • sha
    • shuffle
    • singularize
    • slice
    • slicestr
    • sort
    • split
    • string
    • strings.HasSuffix
    • strings.Repeat
    • strings.RuneCount
    • strings.TrimLeft
    • strings.TrimPrefix
    • strings.TrimRight
    • strings.TrimSuffix
    • substr
    • symdiff
    • templates.Exists
    • time
    • title
    • transform.Unmarshal
    • trim
    • truncate
    • union
    • uniq
    • upper
    • urlize
    • urls.Parse
    • where
    • with
    • 图片函数
  • 变量
    • 变量概述
    • 网站变量
    • 简码变量
    • 页面变量
    • 页面方法
    • 分类变量
    • 文件变量
    • 菜单项属性
    • Hugo 变量
    • Git的变量
    • 网站地图变量
  • Hugo 管道
    • Hugo 管道概述
    • Hugo 管道简介
    • SASS / SCSS
    • PostCSS
    • 资产压缩
    • Asset 捆绑
    • 指纹和SRI
    • 来自模板资源
    • 从字符串资源
  • CLI
  • 故障排除
    • 疑难解答
    • FAQ
    • 构建性能
  • 工具
    • 开发工具概述
    • 迁移
    • 入门套件
    • 前端
    • 编辑器插件
    • 搜索
    • 其他的项目
  • 托管和部署
    • 托管和部署概述
    • Hugo 部署
    • 使用Nanobox的主机无关部署
    • AWS Amplify托管
    • Netlify托管
    • Render托管
    • Firebase托管
    • GitHub托管
    • GitLab托管
    • KeyCDN托管
    • Bitbucket托管
    • 使用Wercker部署
    • 使用rsync部署
  • 贡献
    • 贡献Hugo
    • 开发
    • 文档
    • 主题
  • 保养
“多语言模式” was last updated: March 24, 2020: 添加翻译 (d6d8ad2)
Improve this page
By the Hugo Authors
Hugo Logo
  • File an Issue
  • Get Help
  • Discuss Source Code
  • @GoHugoIO
  • @spf13
  • @bepsays

Netlify badge

 
 

Hugo Sponsors

Logo for Forestry.io
Logo for Linode
Logo for eSolia
 

The Hugo logos are copyright © Steve Francia 2013–2020.

The Hugo Gopher is based on an original work by Renée French.

  • 新闻
  • 文档
  • 主题
  • 作品展示
  • 社区
  • GitHub
  • 关于 Hugo
  • 入门
  • Hugo 模块
  • 内容管理
  • 模板
  • 函数
  • 变量
  • Hugo 管道
  • CLI
  • 故障排除
  • 工具
  • 托管和部署
  • 贡献
  • 保养