Advertisement
tniemi

Localized dates in Octopress

Mar 16th, 2014
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 4.57 KB | None | 0 0
  1. require 'i18n'
  2. #
  3. #   Localized dates in Octopress
  4. #   ============================
  5. #
  6. #   Written by Tero Niemi (ruby -e 'puts "tero\x2eniemi\100nimbus.fi"')
  7. #   Granted into the Public Domain in March 2014
  8. #
  9. #   This plugin allows showing the date values in different languages,
  10. #   fex. "1. tammikuuta 2022" instead of "1st of January 2022".
  11. #
  12. #   Installation
  13. #   ------------
  14. #
  15. #   1.  Replace the "Octopress/plugin/date.rb" with this file.
  16. #
  17. #   2.  Install "i18n" gem. Add:
  18. #           gem 'i18n', '~> 0.6.9'
  19. #       to your Gemfile and execute:
  20. #           bundle install
  21. #
  22. #   3.  Download the "locale" directory from:
  23. #           https://github.com/svenfuchs/rails-i18n/tree/master/rails/
  24. #       and place it to your Octopress root: "Octopress/locale"
  25. #
  26. #       (You need only the "locale" directory; discard rest of the repository!)
  27. #
  28. #       (And from that directory you actually need only the .yml files for
  29. #       the locales you are going to use!)
  30. #
  31. #   4.  Add the following to your "_config.yml" file:
  32. #
  33. #           # Directory for locale .yml files.
  34. #           # (Defaults to "locale" if not defined here.)
  35. #           locale_dir: locale
  36. #
  37. #           # Either a strftime format string ("%F") or :default, :long, :short
  38. #           # (Defaults to :default if not defined here.)
  39. #           # (See your locale files for details, or to create new symbols!)
  40. #           date_format: :default
  41. #
  42. #           # Default language for the site.
  43. #           # (Defaults to "en" if not defined here.)
  44. #           # (See your locale files for available language codes!)
  45. #           lang: fi
  46. #
  47. #   5a. Optionally, in your posts and pages metadata:
  48. #
  49. #           lang: zh-CN             # This blog post is in Chinese!
  50. #
  51. #           date_format: "%Y"       # This blog post shows only the year
  52. #
  53. #   5b. Optionally, in your html template files:
  54. #
  55. #           <html lang="{{ site.lang }}">
  56. #
  57. #           <article class="page" lang="{{ page.lang }}">
  58. #
  59. #           <article class="post" lang="{{ post.lang }}">
  60. #
  61. #       ... but that is just decoration.
  62. #
  63. module Octopress
  64.   module Date
  65.  
  66.     # Returns a DateTime object if the input is a string
  67.     def datetime(date)
  68.       date.class == String ? DateTime.parse(date) : date
  69.     end
  70.  
  71.     # Returns current language
  72.     def lang
  73.       self.data['lang'] || self.site.config['lang'] || 'en'
  74.     end
  75.  
  76.     # Returns current date format string or symbol
  77.     def date_format
  78.       self.data['date_format'] || self.site.config['date_format'] || :default
  79.     end
  80.  
  81.     # Returns date formatted in current language
  82.     def format_date(date, date_format = date_format, lang = lang)
  83.       I18n.localize datetime(date), :format => date_format, :locale => lang
  84.     end
  85.  
  86.     # Returns the date-specific liquid attributes
  87.     def liquid_date_attributes
  88.       date_attributes = {}
  89.       date_attributes['date_formatted']    = format_date(self.data['date'])    if self.data.has_key?('date')
  90.       date_attributes['updated_formatted'] = format_date(self.data['updated']) if self.data.has_key?('updated')
  91.       date_attributes
  92.     end
  93.  
  94.   end
  95. end
  96.  
  97. module Jekyll
  98.  
  99.   class Site
  100.  
  101.     # Load internationalization locales just before site generation.
  102.     # (Actually this should be done in proper i18n plugin,
  103.     # but this is just a quick hack to fix the dates!)
  104.     alias :super_generate :generate
  105.     def generate
  106.       locale_dir = File.expand_path(self.config['locale_dir'] || 'locale')
  107.  
  108.       File.directory? locale_dir or abort <<-ERROR_MSG
  109.  
  110.         Directory "#{locale_dir}" does not exists.
  111.  
  112.         Please get it from:
  113.             https://github.com/svenfuchs/rails-i18n/tree/master/rails/
  114.         (You need ONLY the "locale" directory!)
  115.  
  116.       ERROR_MSG
  117.  
  118.       I18n.enforce_available_locales = false  # Just to remove nagging
  119.       I18n.load_path = Dir[File.join(locale_dir, '*.yml')];
  120.  
  121.       super_generate
  122.     end
  123.   end
  124.  
  125.   class Post
  126.     include Octopress::Date
  127.  
  128.     # Convert this Convertible's data to a Hash suitable for use by Liquid.
  129.     # Overrides the default return data and adds any date-specific liquid attributes
  130.     alias :super_to_liquid :to_liquid
  131.     def to_liquid
  132.       super_to_liquid.deep_merge(liquid_date_attributes)
  133.     end
  134.   end
  135.  
  136.   class Page
  137.     include Octopress::Date
  138.  
  139.     # Convert this Convertible's data to a Hash suitable for use by Liquid.
  140.     # Overrides the default return data and adds any date-specific liquid attributes
  141.     alias :super_to_liquid :to_liquid
  142.     def to_liquid
  143.       super_to_liquid.deep_merge(liquid_date_attributes)
  144.     end
  145.   end
  146.  
  147. end
  148.  
  149. # eof
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement