Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'i18n'
- #
- # Localized dates in Octopress
- # ============================
- #
- # Written by Tero Niemi (ruby -e 'puts "tero\x2eniemi\100nimbus.fi"')
- # Granted into the Public Domain in March 2014
- #
- # This plugin allows showing the date values in different languages,
- # fex. "1. tammikuuta 2022" instead of "1st of January 2022".
- #
- # Installation
- # ------------
- #
- # 1. Replace the "Octopress/plugin/date.rb" with this file.
- #
- # 2. Install "i18n" gem. Add:
- # gem 'i18n', '~> 0.6.9'
- # to your Gemfile and execute:
- # bundle install
- #
- # 3. Download the "locale" directory from:
- # https://github.com/svenfuchs/rails-i18n/tree/master/rails/
- # and place it to your Octopress root: "Octopress/locale"
- #
- # (You need only the "locale" directory; discard rest of the repository!)
- #
- # (And from that directory you actually need only the .yml files for
- # the locales you are going to use!)
- #
- # 4. Add the following to your "_config.yml" file:
- #
- # # Directory for locale .yml files.
- # # (Defaults to "locale" if not defined here.)
- # locale_dir: locale
- #
- # # Either a strftime format string ("%F") or :default, :long, :short
- # # (Defaults to :default if not defined here.)
- # # (See your locale files for details, or to create new symbols!)
- # date_format: :default
- #
- # # Default language for the site.
- # # (Defaults to "en" if not defined here.)
- # # (See your locale files for available language codes!)
- # lang: fi
- #
- # 5a. Optionally, in your posts and pages metadata:
- #
- # lang: zh-CN # This blog post is in Chinese!
- #
- # date_format: "%Y" # This blog post shows only the year
- #
- # 5b. Optionally, in your html template files:
- #
- # <html lang="{{ site.lang }}">
- #
- # <article class="page" lang="{{ page.lang }}">
- #
- # <article class="post" lang="{{ post.lang }}">
- #
- # ... but that is just decoration.
- #
- module Octopress
- module Date
- # Returns a DateTime object if the input is a string
- def datetime(date)
- date.class == String ? DateTime.parse(date) : date
- end
- # Returns current language
- def lang
- self.data['lang'] || self.site.config['lang'] || 'en'
- end
- # Returns current date format string or symbol
- def date_format
- self.data['date_format'] || self.site.config['date_format'] || :default
- end
- # Returns date formatted in current language
- def format_date(date, date_format = date_format, lang = lang)
- I18n.localize datetime(date), :format => date_format, :locale => lang
- end
- # Returns the date-specific liquid attributes
- def liquid_date_attributes
- date_attributes = {}
- date_attributes['date_formatted'] = format_date(self.data['date']) if self.data.has_key?('date')
- date_attributes['updated_formatted'] = format_date(self.data['updated']) if self.data.has_key?('updated')
- date_attributes
- end
- end
- end
- module Jekyll
- class Site
- # Load internationalization locales just before site generation.
- # (Actually this should be done in proper i18n plugin,
- # but this is just a quick hack to fix the dates!)
- alias :super_generate :generate
- def generate
- locale_dir = File.expand_path(self.config['locale_dir'] || 'locale')
- File.directory? locale_dir or abort <<-ERROR_MSG
- Directory "#{locale_dir}" does not exists.
- Please get it from:
- https://github.com/svenfuchs/rails-i18n/tree/master/rails/
- (You need ONLY the "locale" directory!)
- ERROR_MSG
- I18n.enforce_available_locales = false # Just to remove nagging
- I18n.load_path = Dir[File.join(locale_dir, '*.yml')];
- super_generate
- end
- end
- class Post
- include Octopress::Date
- # Convert this Convertible's data to a Hash suitable for use by Liquid.
- # Overrides the default return data and adds any date-specific liquid attributes
- alias :super_to_liquid :to_liquid
- def to_liquid
- super_to_liquid.deep_merge(liquid_date_attributes)
- end
- end
- class Page
- include Octopress::Date
- # Convert this Convertible's data to a Hash suitable for use by Liquid.
- # Overrides the default return data and adds any date-specific liquid attributes
- alias :super_to_liquid :to_liquid
- def to_liquid
- super_to_liquid.deep_merge(liquid_date_attributes)
- end
- end
- end
- # eof
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement