Sha256: 7e7bbb97980c675e4d63e49b78f91676bf7cbc2d8176f01684ef299c0aeffdf7

Contents?: true

Size: 1.91 KB

Versions: 49

Compression:

Stored size: 1.91 KB

Contents

# Taken from https://github.com/uwolf/jekyll-i18n-date

# ------------------------------------------------------------------------------
# ~/_plugins/date-i18n.rb
# Plugin for DateTime Localization in Jekyll
#
# Product/Info:
# http://jekyll.one
#
# Copyright (C) 2021 Juergen Adams
# Copyright (c) 2018 Ulrich Wolf
#
# J1 Template is licensed under the MIT License.
# See: https://github.com/jekyll-one-org/J1 Template/blob/master/LICENSE
# jekyll-i18n-date is licensed under the MIT License.
# See: https://github.com/uwolf/jekyll-i18n-date/blob/master/LICENSE
# ------------------------------------------------------------------------------
# Example|s:
#  {{ page.date | localize: "%d.%m.%Y" }}
#  {{ post.date | localize: "%d.%m.%Y", "de" }}
# ------------------------------------------------------------------------------
require 'i18n'

module Jekyll
  # i18n filter for jekyll
  module I18nFilter
    # Example:
    #   {{ post.date | localize: "%d.%m.%Y" }}
    #   {{ post.date | localize: ":short" }}
    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

    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

		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

  end
end

Liquid::Template.register_filter(Jekyll::I18nFilter)

Version data entries

49 entries across 49 versions & 1 rubygems

Version Path
j1-template-2021.3.1 lib/starter_web/_plugins/date-i18n.rb
j1-template-2021.3.0 lib/starter_web/_plugins/date-i18n.rb
j1-template-2021.2.12 lib/starter_web/_plugins/date-i18n.rb
j1-template-2021.2.11 lib/starter_web/_plugins/date-i18n.rb
j1-template-2021.2.10 lib/starter_web/_plugins/date-i18n.rb
j1-template-2021.2.9 lib/starter_web/_plugins/date-i18n.rb
j1-template-2021.2.8 lib/starter_web/_plugins/date-i18n.rb
j1-template-2021.2.7 lib/starter_web/_plugins/date-i18n.rb
j1-template-2021.2.6 lib/starter_web/_plugins/date-i18n.rb
j1-template-2021.2.5 lib/starter_web/_plugins/date-i18n.rb
j1-template-2021.2.4 lib/starter_web/_plugins/date-i18n.rb
j1-template-2021.2.3 lib/starter_web/_plugins/date-i18n.rb
j1-template-2021.2.2 lib/starter_web/_plugins/date-i18n.rb
j1-template-2021.2.1 lib/starter_web/_plugins/date-i18n.rb
j1-template-2021.2.0 lib/starter_web/_plugins/date-i18n.rb
j1-template-2021.1.29 lib/starter_web/_plugins/date-i18n.rb
j1-template-2021.1.28 lib/starter_web/_plugins/date-i18n.rb
j1-template-2021.1.27 lib/starter_web/_plugins/date-i18n.rb
j1-template-2021.1.26 lib/starter_web/_plugins/date-i18n.rb
j1-template-2021.1.25 lib/starter_web/_plugins/date-i18n.rb