Module: Jekyll::I18nFilter
- Defined in:
- lib/starter_web/_plugins/i18n/date.rb
Overview
i18n filter for jekyll
Instance Method Summary collapse
- #current_locale(locale) ⇒ Object
- #load_translations ⇒ Object
-
#localize(input, format = nil, locale = nil) ⇒ Object
Example: post.date | localize: “%d.%m.%Y” } post.date | localize: “:short” }.
Instance Method Details
#current_locale(locale) ⇒ Object
54 55 56 57 58 59 60 61 62 |
# File 'lib/starter_web/_plugins/i18n/date.rb', line 54 def current_locale(locale) l = locale || @context.registers[:page]['language'] || @context.registers[:site].config['language'] if l && I18n.config.available_locales.include?(l.to_sym) l else false end end |
#load_translations ⇒ Object
48 49 50 51 52 |
# File 'lib/starter_web/_plugins/i18n/date.rb', line 48 def load_translations return false unless I18n.backend.send(:translations).empty? filename = File.join(File.dirname(__FILE__), '../../_data/locales/*.yml') I18n.backend.load_translations Dir[filename] end |
#localize(input, format = nil, locale = nil) ⇒ Object
Example:
{{ post.date | localize: "%d.%m.%Y" }}
{{ post.date | localize: ":short" }}
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/starter_web/_plugins/i18n/date.rb', line 30 def localize(input, format = nil, locale = nil) # Side effects: changes I18n.config, must run before current_locale is set load_translations input = Time.at(input) if input.class == Integer format = format =~ /^:(\w+)/ ? Regexp.last_match(1).to_sym : format if input && locale = current_locale(locale) I18n.locale = locale I18n.l(input, format: format) else input end end |