在hugo配置文件中使用特定于语言的字符串 [英] Use variable language specific strings in hugo config file

查看:317
本文介绍了在hugo配置文件中使用特定于语言的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是使用 hugo 建立一个多语言站点.为此,我想:

My goal is to build a multilingual site using hugo. For this I would like to:

  • 不要触摸主题文件
  • 具有一个配置文件,该文件定义了所有语言的整体结构(config.toml)
  • 具有适用于所有语言的字符串"文件

因此,例如,我将有一个像这样的config.toml文件:

So for example, I would have a config.toml file like this:

[params.navigation]
    brand = "out-website"

    [params.navigation.links]
        about     = $ABOUT_NAME
        services  = $SERVICES_NAME
        team      = $TEAM_NAME
        impressum = $IMPRESSUM_NAME

一个英语文件:

ABOUT_NAME=About
SERVICES_NAME=Services
TEAM_NAME=Team
IMPRESSUM_NAME=Impressum

和像这样的德语文件:

ABOUT_NAME=Über uns
SERVICES_NAME=Dienste
TEAM_NAME=Mitarbeiter
IMPRESSUM_NAME=Impressum

然后我想用英语编译该项目,我按照以下方式进行操作:

And then I want to compile the project for english, I do something along the line of:

hugo --theme=... --config=config.toml --config=english.toml

和德语:

hugo --theme=... --config=config.toml --config=german.toml

或以类似的方式.

为此,我需要在config.toml中使用在english.toml或german.toml中定义的变量

For this I need to use variables in the config.toml that are defined in english.toml or german.toml

到目前为止,我的Google搜索表明我无法在toml中使用变量. 那么,有没有其他方法可以实现这一目标呢?

My google search so far say, that I cannot use variables in toml. So is there a different approach with which I could achieve this?

推荐答案

您使用变量的方法不是最佳方法,请使用下面的教程.

Your approach with variables is not optimal, use the tutorial below.

多语言站点已成为Hugo 0.16的功能,但我设法使用教程和一些技巧.

Multilingual sites are coming as a feature on Hugo 0.16, but I managed to build a multilingual site on current Hugo using this tutorial and some hacks.

上面的教程要求每种语言都有一个单独的域名".我设法绕过了这一点,不得不进入站点,一个在根目录(EN),另一个在文件夹(/LT/).

The tutorial above requires to "have a separate domain name for each language". I managed to bypass that and to have to sites, one at root (EN), and one in the folder (/LT/).

这是我使用的步骤.

  1. 设置可靠的构建运行器,可以使用Grunt/Gulp,但是我使用了npm脚本.我入侵了 npm-build-boilerplate ,并从Hugo外包了渲染. Hugo仅用于生成文件. 这很重要,因为1)我们将生成两个网站; 2)黑客将需要对文件夹进行操作,这在npm上很容易(我不擅长构建自定义的Grunt/Gulp脚本).

  1. Set up reliable build runner, you can use Grunt/Gulp, but I used npm scripts. I hacked npm-build-boilerplate and outsourced rendering from Hugo. Hugo is used only to generate files. This is important, because 1) we will be generating two sites; 2) hacks will require operations on folders, what is easy on npm (I'm not proficient on building custom Grunt/Gulp scripts).

根据教程在根文件夹中设置config_en.toml和config_de.toml.

Set up config_en.toml and config_de.toml in root folder as per tutorial.

这是我config_en.toml的摘录:

Here's excerpt from my config_en.toml:

contentdir = "content/en"
publishdir = "public"

这是我的config_lt.toml的摘录(根据您的情况将其更改为DE):

Here's excerpt from my config_lt.toml (change it to DE in your case):

contentdir = "content/lt"
publishdir = "public/lt"

基本上,我希望我的website.com显示EN版本,而website.com/lt/显示LT版本.这与教程有所不同,将需要进行黑客攻击.

Basically, I want my website.com to show EN version, and website.com/lt/ to show LT version. This deviates from tutorial and will require hacks.

  1. 根据教程在/data文件夹中设置翻译.

奖金:在Dash上设置代码段:

Bonus: set up snippet on Dash:

{{(index $ .Site.Data.translations $ .Site.Params.locale).@ cursor}}

{{ ( index $.Site.Data.translations $.Site.Params.locale ).@cursor }}

每当我输入"trn",我都会得到上面的内容,剩下的就是从翻译文件中粘贴密钥,然后以正确的语言获得值.

Whenever I type "trn", I get the above, what's left is to paste the key from translations file and I get the value in correct language.

  1. 缺乏部分.博客 posts 可以在/lt/上正常运行,但不能在静态页面(着陆页)上正常运行.我使用指令.
  1. Hack part. The blog posts will work fine on /lt/, but not static pages (landing pages). I use this instruction.

基本上,每个静态页面(例如content/lt/duk.md)都将设置为slug:

Basically, every static page, for example content/lt/duk.md will have slug set:

slug: "lt/duk"

但是这种错误会导致 double URL路径,例如lt/lt/duk.

But this slug will result in double URL path, for example lt/lt/duk.

我使用rsync的npm脚本任务和手动命令来删除冗余文件夹,以恢复此操作:

I restore this using my npm scripts task using rsync and manual command to delete redundant folder:

"restorefolders": "rsync -a public/lt/lt/ public/lt/ && rm -rf public/lt/lt/",

这篇关于在hugo配置文件中使用特定于语言的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆