jekyll-language-plugin 1.0.2 → 1.1.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: 18a4c1437ec41d016a655990c7dd40086db5eed3
4
- data.tar.gz: 080a094139c5372c70dd659bd6e5bbae6ae5e9ed
3
+ metadata.gz: 09d08bc0bd9ace9e9ed14474e8f57ce0298e6779
4
+ data.tar.gz: a6ab1ecd54e36a03a986af23d5ee9b0f16f35af5
5
5
  SHA512:
6
- metadata.gz: 01a89b8faf0b802df025daa1712b44f788eecad67029394e3cf76be8c91f3ae91df9f0c945d61a4141145eecb60d7739799fc5dce9ced3de50a27d65a47bd4bb
7
- data.tar.gz: 6619b1f2f0fe8029b4c742a02ebedd1ebe4004a237746b9b75a562af2e077006a3ba9c7db61a2c62ed64e2cc30f4292e508e51642b8441a7f1c6d2a0706d1864
6
+ metadata.gz: 91724843ebb69d1ad67e45b3ca8937f50b2cbb2bf5f8c2f1c1caf0803c8d86ddf6d959656b293bfa755f57354b58678ed3010a9baf340270fc8cf0b657e9d26e
7
+ data.tar.gz: 97201c70f08003ea0426e0009202d1153660a651f5a87535dc0a1c7fc18ed7c88ec157018d6a980bd6e308f2791b7b96aa68b35f53468abe257af57988bfc7ef
data/README.md CHANGED
@@ -11,6 +11,7 @@ This plugin has been developed with user-simplicity in mind. It does not require
11
11
  * Translates pages and posts into multiple languages
12
12
  * Supports all template languages that your Liquid pipeline supports.
13
13
  * Uses liquid tags in your HTML for including translated strings and language-specific includes.
14
+ * Supports localized dates via liquid filter
14
15
  * Works with `jekyll serve --watch`
15
16
  * Supports includes translated into multiple languages
16
17
 
@@ -134,12 +135,34 @@ Currently, there are two liquid tags provided by this plugin.
134
135
 
135
136
  The `t` liquid tag provides a convenient way of accessing language-specific translations from the language data referred to in the configuration file.
136
137
 
137
- If an alias is given by the page's or post's front-matter, `t` will look into the language-specific alias first. Only if the key cannot be found there, it will perform another lookup without the alias. This can be useful for common translations like a copyright notice.
138
+ If an alias is given by the page's or post's front-matter, `t` will look into the language-specific alias first. Only if the key cannot be found there, it will perform another lookup without the alias. This can be useful for common translations like a copyright notice. The key can also be a dot-notation of multiple keys which are traversed upon lookup.
139
+
140
+ *Example*: `{% t homepage_welcome %}`
138
141
 
139
142
  ### Language-Specific Include Tag
140
143
 
141
144
  The `tinclude` liquid tag works just like the Jekyll-standard `include` tag. But unlike `include`, `tinclude` will not look into the `_includes` directory. Instead it will look into the directory specified by the `language_includes_dir` configuration setting, here `_i18n`. Then it travels one subdirectory down for the language name. If you `{% tinclude lorem.txt %}`, `tinclude` will look for the file in `_i18n/en/lorem.txt` if the language is English.
142
145
 
146
+ *Example*: `{% tinclude imprint.html %}`
147
+
148
+ ### Language-Specific Date Filter
149
+
150
+ The `tdate` liquid filter provides localized date-formatting using the day and month names specified in the language data for each language. Note that if you are using this filter, a `date` key must be present for every supported language.
151
+
152
+ The `tdate` filter takes one argument, the date format language key. A lookup is performed just like the `t` tag does.
153
+
154
+ The following excerpt shows the english date translation:
155
+
156
+ ```
157
+ date:
158
+ abbr_daynames: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
159
+ daynames: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
160
+ abbr_monthnames: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
161
+ monthnames: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
162
+ ```
163
+
164
+ *Example*: `{{ post.date | tdate: 'post_date_format' }}`
165
+
143
166
  # Example Site
144
167
 
145
168
  This repository contains a ready-to-use example site using this plugin in the `example` subdirectory. Check it out and run `bundle install` followed by `bundle exec jekyll build` to build the site.
@@ -1,9 +1,14 @@
1
+ # require all files in jekyll-language-plugin subdirectory
2
+ require 'jekyll-language-plugin/liquid_context.rb'
3
+ require 'jekyll-language-plugin/date_localizer.rb'
4
+
1
5
  # require all files in jekyll subdirectory
2
6
  require 'jekyll/language_reader.rb'
3
7
  require 'jekyll/language_page.rb'
4
8
  require 'jekyll/language_document.rb'
5
9
  require 'jekyll/readers/language_page_reader.rb'
6
10
  require 'jekyll/readers/language_post_reader.rb'
11
+ require 'jekyll/filters/language_date.rb'
7
12
  require 'jekyll/tags/language.rb'
8
13
  require 'jekyll/tags/language_include.rb'
9
14
 
@@ -0,0 +1,39 @@
1
+ module JekyllLanguagePlugin
2
+ module DateLocalizer
3
+ def self.localize_date(date, format, context)
4
+ translation = JekyllLanguagePlugin::LiquidContext.get_language_data(context, 'date')
5
+
6
+ # validate language translation
7
+ return nil if translation.nil? ||
8
+ !['abbr_daynames', 'daynames', 'abbr_monthnames', 'monthnames'].all? {|s| translation.key?(s) && translation[s].is_a?(Array) } ||
9
+ translation['abbr_daynames'].size < 7 || translation['daynames'].size < 7 ||
10
+ translation['abbr_monthnames'].size < 12 || translation['monthnames'].size < 12
11
+
12
+ #convert to extended Time class
13
+ date2 = JLPTime.at(date.to_i)
14
+ date2.strftime_translate(format, translation)
15
+ end
16
+
17
+ class JLPTime < Time
18
+ def strftime_translate(format = '%F', translation)
19
+ result = self.strftime(
20
+ #...you replaced the language dependent parts.
21
+ format.gsub(/%([aAbB])/){ |m|
22
+ case $1
23
+ when 'a'; translation['abbr_daynames'][self.wday]
24
+ when 'A'; translation['daynames'][self.wday]
25
+ when 'b'; translation['abbr_monthnames'][self.mon-1]
26
+ when 'B'; translation['monthnames'][self.mon-1]
27
+ else
28
+ raise "Date#strftime: InputError"
29
+ end
30
+ })
31
+ if defined? @@encoding_converter
32
+ @@encoding_converter.iconv(result)
33
+ else
34
+ result
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,46 @@
1
+ module JekyllLanguagePlugin
2
+ module LiquidContext
3
+ def self.get_language_data(context, keys = nil)
4
+ site = context.registers[:site]
5
+ language_data = site.config['language_data']
6
+ page_language = context.registers[:page]['language']
7
+ return nil if language_data.to_s.empty? || page_language.to_s.empty?
8
+
9
+ keys2 = language_data.gsub("%%", page_language).split('.')
10
+ language_data = site.send(keys2.shift)
11
+ language_data = self.traverse_hash(language_data, keys2)
12
+
13
+ return language_data if keys.to_s.empty? # can also return nil
14
+ self.traverse_hash(language_data, keys)
15
+ end
16
+
17
+ def self.get_language_string(context, key)
18
+ data = self.get_language_data(context)
19
+ return "" if data.nil?
20
+
21
+ page_alias = context.registers[:page]['alias']
22
+ if (!page_alias.to_s.empty? &&
23
+ !(str = traverse_hash(traverse_hash(data, page_alias), key)).to_s.empty?) ||
24
+ !(str = traverse_hash(data, key)).to_s.empty?
25
+ return str
26
+ end
27
+
28
+ ""
29
+ end
30
+
31
+ def self.traverse_hash(hash, keys)
32
+ return nil if hash.nil? || keys.to_s.empty?
33
+ keys = keys.split('.') if keys.is_a?(String)
34
+
35
+ for key in keys
36
+ if !hash.is_a?(Hash)
37
+ return hash
38
+ elsif !hash.key?(key)
39
+ return nil
40
+ end
41
+ hash = hash[key]
42
+ end
43
+ hash
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,30 @@
1
+ module Jekyll
2
+ module Filters
3
+ module LanguageDate
4
+ def tdate(input, fkey)
5
+ if ((input.is_a?(String) && !/^\d+$/.match(input).nil?) || input.is_a?(Integer)) && input.to_i > 0
6
+ date = Time.at(input.to_i)
7
+ elsif input.is_a?(String)
8
+ case input.downcase
9
+ when 'now', 'today'
10
+ date = Time.now
11
+ else
12
+ date = Time.parse(input)
13
+ end
14
+ elsif input.is_a?(Time)
15
+ date = input
16
+ else
17
+ date = nil
18
+ end
19
+
20
+ return "" if !date.is_a?(Time)
21
+ format = JekyllLanguagePlugin::LiquidContext.get_language_string(@context, fkey)
22
+ return "" if format.nil?
23
+
24
+ JekyllLanguagePlugin::DateLocalizer.localize_date(date, format, @context).to_s
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ Liquid::Template.register_filter(Jekyll::Filters::LanguageDate)
@@ -8,7 +8,7 @@ module Jekyll
8
8
  ldocument.read
9
9
 
10
10
  languages = ldocument.languages.is_a?(Enumerable) ? ldocument.languages : []
11
- if ldocument.language and not languages.include?(ldocument.language)
11
+ if ldocument.language && !languages.include?(ldocument.language)
12
12
  languages.push(ldocument.language)
13
13
  end
14
14
 
@@ -7,36 +7,10 @@ module Jekyll
7
7
  @lkey = @params.shift
8
8
  end
9
9
 
10
- def get_language_data(context, language)
11
- language_data = context['site']['language_data'].strip
12
- traverse = language_data.gsub("%%", language).split('.')
13
-
14
- data = context['site']
15
- traverse.each { |t| data = data[t] }
16
- data
17
- end
18
-
19
- def get_language_string(context, key)
20
- page_language = context['page']['language']
21
-
22
- if not page_language
23
- return ""
24
- end
25
-
26
- page_alias = context['page']['alias']
27
-
28
- data = get_language_data(context, page_language)
29
- if page_alias and data.include?(page_alias) and data[page_alias].include?(key)
30
- "#{data[page_alias][key]}"
31
- elsif data.include?(key)
32
- "#{data[key]}"
33
- else
34
- ""
35
- end
36
- end
37
-
38
10
  def render(context)
39
- str = get_language_string(context, @lkey)
11
+ str = JekyllLanguagePlugin::LiquidContext.get_language_string(context, @lkey)
12
+ return "" if str.nil?
13
+
40
14
  @params.each { |p| str.sub!("%%", p) }
41
15
  str
42
16
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-language-plugin
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vincent Wochnik
@@ -74,6 +74,7 @@ extensions: []
74
74
  extra_rdoc_files: []
75
75
  files:
76
76
  - lib/jekyll-language-plugin.rb
77
+ - lib/jekyll/filters/language_date.rb
77
78
  - lib/jekyll/language_document.rb
78
79
  - lib/jekyll/language_page.rb
79
80
  - lib/jekyll/language_reader.rb
@@ -81,6 +82,8 @@ files:
81
82
  - lib/jekyll/readers/language_post_reader.rb
82
83
  - lib/jekyll/tags/language.rb
83
84
  - lib/jekyll/tags/language_include.rb
85
+ - lib/jekyll-language-plugin/date_localizer.rb
86
+ - lib/jekyll-language-plugin/liquid_context.rb
84
87
  - README.md
85
88
  - LICENSE.md
86
89
  homepage: https://github.com/vwochnik/jekyll-language-plugin