jekyll-language-plugin 1.2.2 → 1.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b5fb5ec3d91366e33ef3249fe444732578a19a58
4
- data.tar.gz: 021ac231fcf5c00641237f732664ac5d9dae7665
3
+ metadata.gz: 474a7a7e024e8fdae812794d6970da82887655c2
4
+ data.tar.gz: 4a8f30df701b2530cda2ce3339a95bb234983a25
5
5
  SHA512:
6
- metadata.gz: 0f94e8808c97c02c5971cdd91ee8246ae9531b37f57b03c5a898292bd231c8b65e9460e4a7b4df8ffeeaf699ad2a1fe500a76c80ba2814420f6aeeb7b69dfb38
7
- data.tar.gz: af646f2a74818cc0deab7e3d49214126872d0eb88b19288be7b6bc1fc44dadc83d6f83995324460c20e943750974eee2dea4bc3500f0b3e9721be27e65a96ef1
6
+ metadata.gz: b029db16f1aa3a05c7e4d60a0d60d849bdac2ca87541cdea1d71308c71d5c336b841642f750b52261e62e11438f75026d4cd242e54fbdb27ead31789fee6d635
7
+ data.tar.gz: 5146e49851e0bd2e50bc124e446c4ee41faae2cf992f1854784cac6d848c0637746fe440b43b2e7680b3978bc8cf99aeab5577ce3b557abcaef8b2daf71319cd
data/README.md CHANGED
@@ -17,6 +17,7 @@ This plugin has been developed with user-simplicity in mind. It does not require
17
17
  * Supports localized URLs
18
18
  * Works with `jekyll serve --watch`
19
19
  * Supports includes translated into multiple languages
20
+ * Includes language and date translations for 27 languages
20
21
 
21
22
  ## Installation
22
23
 
@@ -22,12 +22,8 @@ module Jekyll
22
22
  end
23
23
 
24
24
  class << self
25
- def loaders
26
- @loaders ||= []
27
- end
28
-
29
25
  def register_loader(loader)
30
- loaders.push(loader)
26
+ LanguageData.register_loader(loader)
31
27
  end
32
28
  end
33
29
  end
@@ -41,9 +37,17 @@ require 'jekyll/language-plugin/loaders/builtin_data_loader.rb'
41
37
  Dir[File.join(File.dirname(__FILE__), 'jekyll/language-plugin/tags/*.rb')].each{ |f| require f }
42
38
  require 'jekyll/language-plugin/filters/language_date.rb'
43
39
 
44
- # replace Jekyll::Reader upon page reset with Jekyll::LanguageReader extension
45
40
  Jekyll::Hooks.register :site, :after_reset do |site|
41
+ # replace Jekyll::Reader with Jekyll::LanguageReader extension
46
42
  site.reader = Jekyll::LanguageReader.new(site)
43
+
44
+ # add dynamic languageData property
45
+ unless site.respond_to?(:languageData) && site.respond_to?(:languageData=)
46
+ site.class.module_eval { attr_accessor :languageData }
47
+ end
48
+
49
+ # create new language data instance
50
+ site.languageData = Jekyll::LanguagePlugin::LanguageData.new(site)
47
51
  end
48
52
 
49
53
  # monkey patch URL.sanitize_url for handling of triple slashes
@@ -1,17 +1,18 @@
1
1
  module Jekyll
2
2
  module LanguagePlugin
3
- module DateLocalizer
4
- def self.localize_date(date, format, context)
5
- language_data = Jekyll::LanguagePlugin::LiquidContext.get_language_data(context)
6
- translation = language_data.get('date')
3
+ class DateLocalizer
4
+ attr_reader :translation
7
5
 
8
- raise Jekyll::LanguagePlugin::PluginError.new('No localized date available.') if translation.nil?
6
+ def initialize(translation)
7
+ @translation = translation
8
+ end
9
9
 
10
+ def localize_date(date, format)
10
11
  # validate language translation
11
12
  if !['abbr_daynames', 'daynames', 'abbr_monthnames', 'monthnames'].all? {|s| translation.key?(s) && translation[s].is_a?(Array) } ||
12
13
  translation['abbr_daynames'].size < 7 || translation['daynames'].size < 7 ||
13
14
  translation['abbr_monthnames'].size < 12 || translation['monthnames'].size < 12
14
- raise Jekyll::LanguagePlugin::PluginError.new('Invalid localized date translation.')
15
+ raise Jekyll::LanguagePlugin::PluginError.new('Invalid date translation.')
15
16
  end
16
17
 
17
18
  date.strftime(
@@ -19,10 +19,7 @@ module Jekyll
19
19
  end
20
20
 
21
21
  return "" if !date.is_a?(Time)
22
- format = LiquidContext.get_language_string(@context, fkey)
23
- return "" if format.nil?
24
-
25
- Jekyll::LanguagePlugin::DateLocalizer.localize_date(date, format, @context).to_s
22
+ LiquidContext.get_localized_date(@context, date, fkey)
26
23
  end
27
24
  end
28
25
  end
@@ -1,23 +1,46 @@
1
1
  module Jekyll
2
2
  module LanguagePlugin
3
3
  class LanguageData
4
- attr_reader :language
4
+ attr_reader :site, :language
5
5
 
6
- def initialize(site, language)
7
- @language = language
8
- @loaders = Jekyll::LanguagePlugin.loaders.map{ |l| l.new(site) }
6
+ def initialize(site)
7
+ @site = site
8
+ @l_inst_ary = Array.new
9
9
  end
10
10
 
11
- def get(key)
12
- @loaders.inject(nil) do |result, loader|
13
- loader.load(@language) unless loader.loaded?(@language)
14
- result = loader.get(key, @language)
11
+ def get(key, language)
12
+ inject_loaders(language) do |result, loader|
13
+ loader.get(key, language)
14
+ end
15
+ end
16
+
17
+ def get_with_placeholders(key, tokens, language)
18
+ inject_loaders(language) do |result, loader|
19
+ loader.get_with_placeholders(key, tokens, language)
20
+ end
21
+ end
22
+
23
+ def inject_loaders(language)
24
+ self.class.loaders.inject(nil) do |result, loader|
25
+ unless l_inst = @l_inst_ary.detect { |l| l.is_a?(loader) }
26
+ l_inst = loader.new(@site)
27
+ @l_inst_ary.push(l_inst)
28
+ end
29
+
30
+ l_inst.load(language) unless l_inst.loaded?(language)
31
+ result = yield result, l_inst
15
32
  break result unless result.nil?
16
33
  end
17
34
  end
18
35
 
19
- def has?(key)
20
- !get(key).nil?
36
+ class << self
37
+ def loaders
38
+ @loaders ||= []
39
+ end
40
+
41
+ def register_loader(loader)
42
+ loaders.push(loader)
43
+ end
21
44
  end
22
45
  end
23
46
  end
@@ -2,38 +2,77 @@ module Jekyll
2
2
  module LanguagePlugin
3
3
  module LiquidContext
4
4
  def self.get_language_data(context)
5
- if !context.registers[:language_data].nil?
6
- return context.registers[:language_data]
7
- end
5
+ context.registers[:site].languageData
6
+ end
8
7
 
8
+ def self.get_language(context)
9
9
  language = context.registers[:page]['language']
10
+
10
11
  if language.to_s.empty?
11
12
  raise Jekyll::LanguagePlugin::PluginError.new('No language specified for current page or post.')
12
13
  end
13
14
 
14
- site = context.registers[:site]
15
- context.registers[:language_data] = LanguageData.new(site, language)
15
+ language
16
16
  end
17
17
 
18
- def self.get_language_string(context, key)
18
+ def self.get_language_string(context, key, tokens=nil)
19
19
  language_data = self.get_language_data(context)
20
+ language = self.get_language(context)
20
21
 
21
22
  subset = context.registers[:page]['subset']
22
- str = language_data.get([subset, key]) unless subset.to_s.empty?
23
- str ||= language_data.get(key)
24
23
 
25
- raise Jekyll::LanguagePlugin::PluginError.new("Key #{key} not found in translation.") if str.nil?
24
+ if tokens.is_a?(Array) && tokens.length > 0
25
+ unless subset.to_s.empty?
26
+ str = language_data.get_with_placeholders([subset, key], tokens, language)
27
+ end
28
+ str ||= language_data.get_with_placeholders(key, tokens, language)
29
+ else
30
+ unless subset.to_s.empty?
31
+ str = language_data.get([subset, key], language)
32
+ end
33
+ str ||= language_data.get(key, language)
34
+ end
35
+
36
+ if str.nil?
37
+ raise Jekyll::LanguagePlugin::PluginError.new("Key #{key} not found in translation.")
38
+ end
39
+
26
40
  str
27
41
  end
28
42
 
43
+ def self.get_localized_date(context, date, format_key)
44
+ language_data = self.get_language_data(context)
45
+ language = self.get_language(context)
46
+
47
+ subset = context.registers[:page]['subset']
48
+ format_str = language_data.get([subset, format_key], language) unless subset.to_s.empty?
49
+ format_str ||= language_data.get(format_key, language)
50
+
51
+ if format_str.nil?
52
+ raise Jekyll::LanguagePlugin::PluginError.new("Date format key #{key} not found in translation.")
53
+ end
54
+
55
+ date_translation = language_data.get('date', language)
56
+
57
+ if date_translation.nil?
58
+ raise Jekyll::LanguagePlugin::PluginError.new('No localized date available for translation.')
59
+ end
60
+
61
+ date_localizer = DateLocalizer.new(date_translation)
62
+ date_localizer.localize_date(date, format_str)
63
+ end
64
+
29
65
  def self.get_language_name(context, name)
30
66
  language_data = self.get_language_data(context)
31
- translation = language_data.get('lang')
67
+ language = self.get_language(context)
32
68
 
33
- raise Jekyll::LanguagePlugin::PluginError.new('Language name not found in translation.') if translation.nil? ||
34
- !translation.key?(name)
69
+ str = language_data.get(['lang', name])
35
70
 
36
- translation[name]
71
+ if str.nil?
72
+ raise Jekyll::LanguagePlugin::PluginError.new("Language name #{name} not found in translation.")
73
+ end
74
+
75
+ str
37
76
  end
38
77
  end
39
78
  end
@@ -10,7 +10,7 @@ module Jekyll
10
10
  end
11
11
 
12
12
  def loaded?(language)
13
- false
13
+ true
14
14
  end
15
15
 
16
16
  def load(language)
@@ -21,13 +21,16 @@ module Jekyll
21
21
  nil
22
22
  end
23
23
 
24
+ def get_with_placeholders(key, tokens, language)
25
+ res = get(key, language)
26
+ return nil if res.nil?
27
+ res.gsub(/%%/).with_index { |m, i| tokens[i] || m }
28
+ end
29
+
24
30
  def traverse_hash(hash, keys)
25
31
  for key in keys
26
- if !hash.is_a?(Hash)
27
- return hash
28
- elsif !hash.key?(key)
29
- return nil
30
- end
32
+ return hash unless hash.is_a?(Hash)
33
+ return nil unless hash.key?(key)
31
34
  hash = hash[key]
32
35
  end
33
36
  hash
@@ -35,4 +35,5 @@ module Jekyll
35
35
  end
36
36
  end
37
37
 
38
+ $stdout.puts("Registering!")
38
39
  Jekyll::LanguagePlugin.register_loader(Jekyll::LanguagePlugin::Loaders::BuiltinDataLoader)
@@ -13,20 +13,18 @@ module Jekyll
13
13
  key = context.evaluate(name)
14
14
  raise Liquid::SyntaxError.new("Invalid language key expression: #{exp}") if key.nil?
15
15
 
16
- # get language string from evaluated key
17
- str = Jekyll::LanguagePlugin::LiquidContext.get_language_string(context, key)
18
-
16
+ tokens = Array.new
19
17
  if p.consume?(:colon)
20
18
  loop do
21
19
  arg = Liquid::Expression.parse(exp = p.expression)
22
- argstr = context.evaluate(arg)
23
- raise Liquid::SyntaxError.new("Invalid parameter expression: #{exp}") if argstr.nil?
24
- raise Jekyll::LanguagePlugin::PluginError.new("Language string is lacking parameter placeholder.") unless str.include?("%%")
25
- str.sub!("%%", argstr)
20
+ token = context.evaluate(arg)
21
+ raise Liquid::SyntaxError.new("Invalid parameter expression: #{exp}") if token.nil?
22
+ tokens.push(token)
26
23
  break if !p.consume?(:comma)
27
24
  end
28
25
  end
29
- str
26
+
27
+ Jekyll::LanguagePlugin::LiquidContext.get_language_string(context, key, tokens)
30
28
  end
31
29
  end
32
30
  end
@@ -1,5 +1,5 @@
1
1
  module Jekyll
2
2
  module LanguagePlugin
3
- VERSION = '1.2.2'
3
+ VERSION = '1.2.3'
4
4
  end
5
5
  end
@@ -17,9 +17,9 @@ module Jekyll
17
17
  })
18
18
 
19
19
  if !language.nil? && !subset.nil?
20
- @language_data ||= LanguagePlugin::LanguageData.new(@site, language)
20
+ language_data = @site.languageData || LanguagePlugin::LanguageData.new(@site)
21
21
 
22
- data = @language_data.get(subset)
22
+ data = language_data.get(subset, language)
23
23
 
24
24
  if !data.nil?
25
25
  filtered = data.reject{ |k, v| v.is_a?(Enumerable) }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-language-plugin
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.2
4
+ version: 1.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vincent Wochnik
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-13 00:00:00.000000000 Z
11
+ date: 2015-12-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jekyll