为不同的语言生成不同的docpad集合 [英] Generate different docpad collections for different languages
问题描述
我想调整我的多肺DocPad博客,以使以* .ru.md结尾的页面进入/ru/目录,以* .en.md结尾的页面进入/en/目录.
I want to tune my multilungual DocPad blog so that pages ended with *.ru.md go into /ru/ directory, and pages ended with *.en.md go into /en/ directory.
假设这是初始结构
src/
pages/
page1.ru.md
page1.en.md
page2.ru.md
这就是我想要的:
./
en/
page1.html
ru/
page1.html
page2.html
./
,因为我要托管gh-pages
.
与帖子相同.我想将它们存储在
And the same for posts. I would like to store them in
src/
posts/
post-about-docpad.ru.md
post-about-docpad.en.md
并获得
./
en/
posts/
post-about-docpad.html
ru/
posts/
post-about-docpad.html
我应该如何配置docpad?
How should I config docpad?
推荐答案
第一步是重命名文档,以使用破折号表示语言,例如:page1-en.html
和page1-ru.html
,而不是page1.en.html
和page1.ru.tml
-就像@kizu正确指出的那样,它将在DocPad从扩展扩展到另一扩展时引起问题.
The 1st step is renaming your documents to use a dash for the language like so: page1-en.html
, and page1-ru.html
, instead of page1.en.html
and page1.ru.tml
— as otherwise as @kizu correctly points out, it will cause problems as DocPad renders from extension to extension.
完成后,您可以将以下内容添加到docpad配置文件中:
Once that is done, you can add the following to your docpad configuration file:
collections:
# Fetch documents in different languages
translate: (database) ->
languageRegex = /^(.+?)-(en|ru)$/
#docpadOutPath = @getConfig().outPath
@getCollection('documents').findAllLive({basename: languageRegex}).on 'add', (document) ->
# Prepare
a = document.attributes
parts = a.basename.match(languageRegex)
basename = parts[1]
language = parts[2]
relativeOutPath = "#{language}/#{a.relativeOutDirPath}/#{basename}.#{a.outExtension}"
#outPath = "#{docpadOutPath}/#{relativeOutPath}"
urls = ["/#{relativeOutPath}"]
# Apply
document
.setMetaDefaults({
#outPath
url: urls[0]
})
.addUrl(urls)
这将使URL以您想要的方式工作.
This will have URLs working in the way you want.
然后在安装了cleanurls插件的静态环境中运行DocPad,以将文档写入所需的位置.
Then run DocPad in the static environment with the cleanurls plugin installed, to write the documents to the desired locations.
docpad install cleanurls
docpad generate --env static
这篇关于为不同的语言生成不同的docpad集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!