Creating various docpad collections for different languages

I want to set up my multilingual DocPad blog so that the pages end with * .ru.md, go to the / ru / directory, and the pages end with * .en.md in the / en / directory.

Say this is the initial structure

src/
  pages/
    page1.ru.md
    page1.en.md
    page2.ru.md

And this is what I want:

./
  en/
    page1.html
  ru/
    page1.html
    page2.html

./because I'm going to post gh-pages.

And the same for messages. I would like to keep them in

src/
  posts/
    post-about-docpad.ru.md
    post-about-docpad.en.md

And get

./
  en/
    posts/
      post-about-docpad.html
  ru/
    posts/
      post-about-docpad.html

How do i set up docpad?

+4
source share
2 answers

The first step is to rename your documents to use a dash for such a language: page1-en.htmland page1-ru.htmlinstead of page1.en.htmland page1.ru.tml- otherwise, @kizu correctly indicates that this will cause problems, since DocPad displays from extension to extension.

, docpad :

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-, , .

DocPad cleanurls, .

docpad install cleanurls
docpad generate --env static
+2

, :

  • DocPad , , , , , Rendering the extension โ€ฆ didn't do anything., _en/_ru .en/.ru, .

  • Docpad, .. pages blog documents.

  • .html.md, DocPad, .

, , , , :)

, docpadConfig docpad.coffee ( GitHub):

docpadConfig = {
    collections:
        # Declare `ru` and `en` collections
        ruDocuments: ->
            @getCollection("documents").findAllLive({
                basename: /_ru$/
            })
        enDocuments: ->
            @getCollection("documents").findAllLive({
                basename: /_en$/
            })

    events:
        renderBefore: () ->
            # Rewrite `pages/` to the root and `posts/` to the `blog/`.
            this.docpad.getCollection('documents').forEach (page) ->
                newOutPath = page.get('outPath')
                    .replace('/out/pages/', '/out/')
                    .replace('/out/posts/', '/out/blog/')
                newUrl = page.get('url')
                    .replace('pages/', '')
                    .replace('posts/', 'blog/')
                page.set('outPath', newOutPath)
                page.setUrl(newUrl)

            # Rewrite `_ru` to the `/ru/`
            this.docpad.getCollection('ruDocuments').forEach (page) ->
                newOutPath = page.get('outPath')
                    .replace('/out/', '/out/ru/')
                    .replace('_ru.', '.')
                newUrl = '/ru' + page.get('url')
                page.set('outPath', newOutPath)
                page.setUrl(newUrl)

            # Rewrite `_en` to the `/en/`
            this.docpad.getCollection('enDocuments').forEach (page) ->
                newOutPath = page.get('outPath')
                    .replace('/out/', '/out/en/')
                    .replace('_en.', '.')
                page.set('outPath', newOutPath)
                newUrl = '/en' + page.get('url').replace('_en.', '.')
                page.setUrl(newUrl)
}
module.exports = docpadConfig

: ru- en-.

pages/ , _en/_ru /en/ /ru/.

, - , , , .

,

src/
  pages/
    page1.ru.md
    page1.en.md
    page2.ru.md
  posts/
    post-about-docpad.ru.md
    post-about-docpad.en.md

:

src/
  documents/
    pages/
      page1_ru.html.md
      page1_en.html.md
      page2_ru.html.md
    posts/
      post-about-docpad_ru.html.md
      post-about-docpad_en.html.md

, . , gh-pages: ? master gh-pages .

, , , , , .replace('/out/', '/out/ru/') .replace('/out/', '/ru/') .. - , out.

+4

Source: https://habr.com/ru/post/1540883/


All Articles