prometheus-ev / jekyll-localization

Jekyll plugin that adds localization features to the rendering engine.
http://rdoc.info/gems/jekyll-localization
Other
68 stars 7 forks source link

Consider adding auto_translate filter #2

Closed ptrin closed 12 years ago

ptrin commented 12 years ago

Thanks for the excellent localization plugin.

When I was first familiarizing myself with how to use it, I felt that the liquid filter was kind of clumsy because it was necessary to provide each of the language variables individually. I thought that it would be better to be able to pass one variable and let the plugin decide which translation to use based on the filename. I'm no Ruby coder, but I messed around and came up with this and it seems to work:

def auto_translate(*translations)
    if !translations[0][lang].nil?
        translations[0][lang]
    else
        translations[0]["en"]
    end
end

alias_method :at, :auto_translate

I structure my language variables in my front matter like so:

copy:
    header:
        en: "blah blah blah"
        fr: "yadda yadda yadda"

Then the template is much cleaner:

<h1>{{ copy.header | at }}</h1>
blackwinter commented 12 years ago

hey, great idea! what about amending translate_lang (and by extension translate) to accept either an array or a hash? this would give you the same syntax, but you'd still use the same method t.