Skip to content

Instantly share code, notes, and snippets.

@maxpoletaev
Created November 16, 2014 12:15
Show Gist options
  • Save maxpoletaev/d3d1f6160d4bbf3a8ce8 to your computer and use it in GitHub Desktop.
Save maxpoletaev/d3d1f6160d4bbf3a8ce8 to your computer and use it in GitHub Desktop.
Jekyll i18n filter
require 'i18n'
# Create folder "_locales" and put some locale file from https://github.com/svenfuchs/rails-i18n/tree/master/rails/locale
module Jekyll
module I18nFilter
# Example:
# {{ post.date | localize: "%d.%m.%Y" }}
# {{ post.date | localize: ":short" }}
def localize(input, format=nil)
load_translations
format = (format =~ /^:(\w+)/) ? $1.to_sym : format
I18n.l input, :format => format
end
def load_translations
unless I18n::backend.instance_variable_get(:@translations)
I18n.backend.load_translations Dir[File.join(File.dirname(__FILE__),'../_locales/*.yml')]
I18n.default_locale = @context.registers[:site].config['locale']
I18n.enforce_available_locales = false
end
end
end
end
Liquid::Template.register_filter(Jekyll::I18nFilter)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment