jekyll-locale 0.3.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d7711d55551114d2ff94841695d72b0db57e5101
4
- data.tar.gz: afa5ee87a7bb1a70fa8af07eb4d258b0f53a6cc3
3
+ metadata.gz: a6aa5bfa8934b5488a01509fcc5edc532e218b7a
4
+ data.tar.gz: 896e029d6388b5c7c71f7864f5dce874916dad10
5
5
  SHA512:
6
- metadata.gz: cdd9f7566119e15ed95fa2c92aef41bda3e474a45585b0872c1e9584d30fbc40f6dfe61ff5406c8caebdb6034386da2c6b6f806944cb713a8b7eeb72c4056d91
7
- data.tar.gz: 719fb8949ff087f5684de77b5c6aaf9f2ed25200a8d8b95c4e6ed2c073a85b428102ed4c9285c1332282cb51bc184f9fcf0b8cc0617c742084f23593e3e18ab6
6
+ metadata.gz: bd03beca23149b7a9d5518c8213185faa8c6b89361133a24bf4add596211fd2628797096fb99b30a7b235679eaa61d5469c2f8ceef8be7ff459d1a6ddea248fd
7
+ data.tar.gz: 80ef5c1b31991f96a3a6b8d26130039badeb93390285e80efff436920af1b9da086c7b4da123cc6525c569d4ab811e1492351da3717f3a20156be7872230cb97
data/README.md CHANGED
@@ -236,3 +236,34 @@ Instead filter out the canonical url from the for-loop, and render the canonical
236
236
  {% endfor %}
237
237
  <link rel="alternate" hreflang="x-default" href="{{ page.url | absolute_url }}" />
238
238
  ```
239
+
240
+ ## Liquid Filters
241
+
242
+ ### `localize_date`
243
+
244
+ This plugin provides a `localize_date` filter to aid in localizing valid date strings. It takes an optional parameter to specify the format
245
+ of the output string.
246
+
247
+ The filter technically delegates to the `I18n` module and therefore requires the translation data to follow a certain convention to pass through without errors.
248
+
249
+ ```yaml
250
+ date:
251
+ day_names : # Array of Day names in full. e.g. "Sunday", "Monday", ...
252
+ month_names : # Array of Month names in full. e.g. "January", "February", ...
253
+ abbr_day_names : # Array of abbreviations of above Day names. e.g. "Sun", "Mon", ...
254
+ abbr_month_names : # Array of abbreviations of above Month names. e.g. "Jan", "Feb", ...
255
+ time:
256
+ am: "am" # Placeholder for Ante-Meridian
257
+ pm: "pm" # Placeholder for Post-Meridian
258
+ formats: # A set of predefined strftime formats
259
+ default: "%B %d, %Y %l:%M:%S %p %z" # Used by default if no other `format` has been specified.
260
+ # my_format: # A valid strftime format of your choice.
261
+ ```
262
+
263
+ #### Requirements
264
+
265
+ The plugin also places a few conventions to streamline usage:
266
+ * All datetime data should be encompassed under a `locale_date` key for each locale except the default locale, for which, the datetime data has been set by default. But you're free to *redefine* it when necessary.
267
+ * The array of names are filled in by default using values defined in Ruby's `Date` class.
268
+ * The array of full day names and full month names have `nil` as the first entry. So locales for non-English languages should have `nil` as the first entry. (In YAML, null list item can be written as simply `~`)
269
+ * The optional parameter for the filter, `format` should either be a string that corresponds to the symbol of the `formats` subkey (e.g. `":default"`) or a valid `strftime` format.
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jekyll
4
+ module Locale::DateTimeHandler
5
+ DATETIME_DEFAULTS = {
6
+ :date => {
7
+ :day_names => Date::DAYNAMES,
8
+ :month_names => Date::MONTHNAMES,
9
+ :abbr_day_names => Date::ABBR_DAYNAMES,
10
+ :abbr_month_names => Date::ABBR_MONTHNAMES,
11
+ },
12
+ :time => {
13
+ :am => "am",
14
+ :pm => "pm",
15
+ :formats => {
16
+ :default => "%B %d, %Y %l:%M:%S %p %z",
17
+ },
18
+ },
19
+ }.freeze
20
+
21
+ class << self
22
+ extend Forwardable
23
+ [:config, :backend].each do |method|
24
+ private def_delegator I18n, method
25
+ end
26
+
27
+ def bootstrap(handler)
28
+ @handler = handler
29
+ config.available_locales = @handler.available_locales
30
+ end
31
+
32
+ def localize(input, format)
33
+ object = date_cache(input)
34
+ locale = @handler.current_locale.to_sym
35
+ data = @handler.locale_dates[locale.to_s] || {}
36
+ store_translations(locale, data) unless translations.key?(locale)
37
+ backend.localize(locale, object, format)
38
+ end
39
+
40
+ def store_translations(locale, data)
41
+ backend.store_translations(
42
+ locale,
43
+ Utils.deep_merge_hashes(
44
+ DATETIME_DEFAULTS, Utils.recursive_symbolize_hash_keys(data)
45
+ )
46
+ )
47
+ end
48
+
49
+ private
50
+
51
+ def translations
52
+ backend.send(:translations)
53
+ end
54
+
55
+ def date_cache(input)
56
+ @date_cache ||= {}
57
+ @date_cache[input] ||= Liquid::Utils.to_date(input)
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jekyll
4
+ module Locale
5
+ module Filters
6
+ def localize_date(input, format = :default)
7
+ format = symbol_or_strftime(format)
8
+ DateTimeHandler.localize(time(input), format)
9
+ end
10
+
11
+ private
12
+
13
+ def symbol_or_strftime(format)
14
+ return format if format.is_a?(Symbol)
15
+
16
+ format = format.to_s
17
+ if format.start_with?(":")
18
+ format.sub(":", "").to_sym
19
+ elsif format.start_with?("%")
20
+ format
21
+ else
22
+ :default
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+
29
+ Liquid::Template.register_filter(Jekyll::Locale::Filters)
@@ -8,9 +8,9 @@ module Jekyll
8
8
  def generate(site)
9
9
  @site = site
10
10
  handler = site.locale_handler
11
- return if handler.available_locales.empty?
11
+ return if handler.user_locales.empty?
12
12
 
13
- handler.available_locales.each do |locale|
13
+ handler.user_locales.each do |locale|
14
14
  handler.filtered_portfolio.each do |canon_doc|
15
15
  handler.append_document(Locale::AutoPage, canon_doc, locale, site.pages)
16
16
  end
@@ -2,6 +2,7 @@
2
2
 
3
3
  module Jekyll
4
4
  class Locale::Handler
5
+ attr_reader :locale_dates
5
6
  attr_writer :current_locale
6
7
 
7
8
  DEFAULT_CONFIG = {
@@ -25,8 +26,9 @@ module Jekyll
25
26
  end
26
27
 
27
28
  def reset
28
- @locale_data = nil
29
- @portfolio = nil
29
+ @portfolio = nil
30
+ @locale_data = {}
31
+ @locale_dates = {}
30
32
  end
31
33
 
32
34
  def data
@@ -50,7 +52,7 @@ module Jekyll
50
52
  end
51
53
 
52
54
  def read
53
- available_locales.each do |locale|
55
+ user_locales.each do |locale|
54
56
  portfolio.each do |canon_doc|
55
57
  # consider only instances of class that include `Jekyll::Locale::Support` mixin
56
58
  next unless canon_doc.is_a?(Jekyll::Locale::Support)
@@ -75,14 +77,18 @@ module Jekyll
75
77
  base_array << locale_page
76
78
  end
77
79
 
78
- def available_locales
79
- @available_locales ||= begin
80
+ def user_locales
81
+ @user_locales ||= begin
80
82
  locales = Array(config["locales_set"]) - [default_locale]
81
83
  locales.compact!
82
84
  locales
83
85
  end
84
86
  end
85
87
 
88
+ def available_locales
89
+ @available_locales ||= user_locales + [default_locale]
90
+ end
91
+
86
92
  def current_locale
87
93
  @current_locale ||= default_locale
88
94
  end
@@ -106,13 +112,20 @@ module Jekyll
106
112
  end
107
113
  end
108
114
 
115
+ def setup
116
+ @date_handler = Locale::DateTimeHandler
117
+ @date_handler.bootstrap(self)
118
+ @locale_data = setup_data if @locale_data.empty?
119
+ nil
120
+ end
121
+
109
122
  def inspect
110
123
  "#<#{self.class} @site=#{site}>"
111
124
  end
112
125
 
113
126
  private
114
127
 
115
- attr_reader :site, :config
128
+ attr_reader :site, :config, :locale_data
116
129
 
117
130
  def html_pages
118
131
  @html_pages ||= begin
@@ -125,26 +138,28 @@ module Jekyll
125
138
  @locales_dir ||= fetch("data_dir")
126
139
  end
127
140
 
128
- def locale_data
129
- @locale_data ||= begin
130
- ldata = site.site_data[locales_dir]
131
- result = {}
132
- return result unless ldata.is_a?(Hash)
133
-
134
- ldata.each do |loc, loc_data|
135
- locale = Utils.snakeify(loc)
136
- result[locale] = {}
137
- next unless loc_data.is_a?(Hash)
141
+ def setup_data
142
+ ldata = site.site_data[locales_dir]
143
+ result = {}
144
+ return result unless ldata.is_a?(Hash)
138
145
 
139
- loc_data.each do |key, value|
140
- next if key == "locale_date"
146
+ ldata.each do |loc, loc_data|
147
+ locale = Utils.snakeify(loc)
148
+ result[locale] = {}
149
+ next unless loc_data.is_a?(Hash)
141
150
 
142
- result[locale][Utils.snakeify(key)] = value.to_s
151
+ date_data = @date_handler::DATETIME_DEFAULTS
152
+ loc_data.each do |key, value|
153
+ if key == "locale_date"
154
+ date_data = Utils.recursive_symbolize_hash_keys(value) if value.is_a?(Hash)
155
+ elsif value.is_a?(String)
156
+ result[locale][Utils.snakeify(key)] = value
143
157
  end
144
158
  end
145
-
146
- result
159
+ @locale_dates[loc] = date_data
147
160
  end
161
+
162
+ result
148
163
  end
149
164
 
150
165
  def fetch(key)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Jekyll
4
4
  module Locale
5
- VERSION = "0.3.1"
5
+ VERSION = "0.4.0"
6
6
  end
7
7
  end
@@ -26,6 +26,15 @@ module Jekyll
26
26
  slugify(input.to_s, :mode => "latin", :replacement => "_")
27
27
  end
28
28
 
29
+ def recursive_symbolize_hash_keys(hash)
30
+ result = {}
31
+ hash.each do |key, value|
32
+ new_key = key.to_s.to_sym
33
+ result[new_key] = value.is_a?(Hash) ? recursive_symbolize_hash_keys(value) : value
34
+ end
35
+ result
36
+ end
37
+
29
38
  def slugify(string, mode: nil, cased: false, replacement: "-")
30
39
  mode ||= "default"
31
40
  return nil if string.nil?
data/lib/jekyll-locale.rb CHANGED
@@ -13,6 +13,9 @@ end
13
13
  require_relative "jekyll/patches/site"
14
14
  require_relative "jekyll/patches/utils"
15
15
 
16
+ require_relative "jekyll/locale/date_time_handler"
17
+ require_relative "jekyll/locale/filters"
18
+
16
19
  require_relative "jekyll/locale/mixins/support"
17
20
  require_relative "jekyll/locale/mixins/helper"
18
21
 
@@ -25,11 +28,13 @@ end
25
28
  Jekyll::Hooks.register :site, :after_reset do |site|
26
29
  handler = site.locale_handler
27
30
  handler.reset
31
+ I18n.config.enforce_available_locales = false
28
32
  require_relative "jekyll/locale/generator" if handler.mode == "auto"
29
33
  end
30
34
 
31
35
  Jekyll::Hooks.register :site, :post_read do |site|
32
36
  handler = site.locale_handler
37
+ handler.setup
33
38
  handler.read unless handler.mode == "auto"
34
39
  end
35
40
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-locale
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ashwin Maroli
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-10-05 00:00:00.000000000 Z
11
+ date: 2018-10-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jekyll
@@ -35,8 +35,10 @@ files:
35
35
  - README.md
36
36
  - lib/jekyll-locale.rb
37
37
  - lib/jekyll/locale/auto_page.rb
38
+ - lib/jekyll/locale/date_time_handler.rb
38
39
  - lib/jekyll/locale/document.rb
39
40
  - lib/jekyll/locale/drop.rb
41
+ - lib/jekyll/locale/filters.rb
40
42
  - lib/jekyll/locale/generator.rb
41
43
  - lib/jekyll/locale/handler.rb
42
44
  - lib/jekyll/locale/mixins/helper.rb