Skip to content

Instantly share code, notes, and snippets.

@magnusdahlstrand
Created October 26, 2012 12:10
Show Gist options
  • Save magnusdahlstrand/3958441 to your computer and use it in GitHub Desktop.
Save magnusdahlstrand/3958441 to your computer and use it in GitHub Desktop.
Docpad - setup for multiple languages
/src
/documents
/articles
/gallery
/de
/articles
/gallery
/es
/articles
/gallery
/fr
/articles
/gallery

The two following parts are both "children" of the templateData object, defined in docpad.coffee

Locales

locale:
    # English
    en:
        _name: "English"
        _param: "en_GB"
        nav:
            home: "Home"
        meta:
            description: "Meta description in English"

    # French
    fr:
        _name: "Français"
        _param: "fr_FR"
        nav:
            home: "Accueil"
        meta:
            description: "Meta description in Français"

    # German
    de:
        _name: "Deutsch"
        _param: "de_DE"
        nav:
            home: "Home"
        meta:
            description: "Meta description in Deutsch"

    # Spanish
    es:
        _name: "Español"
        _param: "es_ES"
        nav:
            home: "Inicio"
        meta:
            description: "Meta description in Español"

Helpers

Both getLang and getLangAgnPath can take either the file's full path (relativePath), or its directory's path (relativeDirPath). Also, relativeBase can be useful for getLangAgnPath if image assets are sorted into the same structure as the documents, and you want the "article's" thumb image for example.

site:
    getLang: (inPath) ->
        # takes the path of the currently processed
        # file and extracts the name of the first
        # folder in the path and, if it's a language code,
        # returns it.
        # Otherwise, return default language code
        # 
        # Processing file in French, relativeDirPath
        # fr/articles/ -> fr
        # Processing file in English, relativePath
        # articles/code-talk.md -> en
    getLangAgnPath: (inPath) ->
        # takes the path of the currently processed
        # file and returns it without the language code
        # 
        # Processing file in French, relativePath
        # fr/articles/code-talk.md -> articles/code-talk.md
        # Processing file in English, relativePath
        # articles/code-talk.md -> articles/code-talk.md
        # Processing file in French, relativeBase
        # fr/articles/code-talk -> articles/code-talk

The two helper functions make it possible to do something like this: @locale[@site.getLang(@document.relativeDirPath)].nav.home to get the current document's locale's translation of the navigation's "Home" item.

Or /assets/images/<%- @site.getLangAgnPath(@document.relativeBase) %>/thumb.jpg to get the current article's thumb.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment