jekyll-language-plugin 1.1.7 → 1.1.8

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: 76a39471fe7e6ad748b7ddf255a6c254e4cb11c4
4
- data.tar.gz: 7a1af5612ba9cc46c054bc19344bbefdcf1b2b0e
3
+ metadata.gz: 9f68ac9191d9f6cb48cf3543c56a77b7e4feab60
4
+ data.tar.gz: 361f0c76aaba69215a66c1aed0137cee6224e25c
5
5
  SHA512:
6
- metadata.gz: 88d6e2795d11668a803166accdc6b167f290464ffd94202ccbc5bd96f7671d289b05607c051a574706205ae26e059256d60788764ec35f9cc20ba6200ed54fcb
7
- data.tar.gz: 1e9cdec599cf6801c27446668424b9893f0250b3d924298c4f04aafadbc7e72ccd26b17747beb0bf1cb2efad13e98b04cdf7f3b2f67e2b3ba3bb799c233e8041
6
+ metadata.gz: 4221a4fda1d0042a4af2ac0a7be3a78214e0ee80571470425dc9813fd4f33e3a66d5c0026d17a3c3ca38793421b00413bd17ab5f79e5c81da51b2ced57144728
7
+ data.tar.gz: 8337952640d00526dd6f98421a05943f1676586535145ba9667bccf384c90296fd634e37fca823ecf4a25f80d4aa65df36966eba4d5bd31565aabb6da051a080
data/README.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  > Jekyll 3.0-compatible multi-language plugin for posts, pages and includes
4
4
 
5
+ <div align="center"><img src="https://raw.githubusercontent.com/vwochnik/jekyll-language-plugin/master/images/jekyll-i18n-logo.png" width="480" height="143" alt="Jekll Language Plugin"></div>
6
+
5
7
  Jekyll Language Plugin is an internationalization plugin for [Jekyll][jekyll]. It diversifies pages, posts and includes that have been optimized for the use with this plugin into different languages which are organized into subdirectories named after the language name.
6
8
 
7
9
  This plugin has been developed with user-simplicity in mind. It does not require a complex setup process unlike some other internationalization plugins.
@@ -12,6 +14,7 @@ This plugin has been developed with user-simplicity in mind. It does not require
12
14
  * Supports all template languages that your Liquid pipeline supports.
13
15
  * Uses liquid tags in your HTML for including translated strings and language-specific includes.
14
16
  * Supports localized dates via liquid filter
17
+ * Supports localized URLs
15
18
  * Works with `jekyll serve --watch`
16
19
  * Supports includes translated into multiple languages
17
20
 
@@ -11,6 +11,7 @@ module Jekyll
11
11
 
12
12
  # plugin requires
13
13
  autoload :PluginError, 'jekyll/language-plugin/plugin_error.rb'
14
+ autoload :LanguageData, 'jekyll/language-plugin/language_data.rb'
14
15
  autoload :LiquidContext, 'jekyll/language-plugin/liquid_context.rb'
15
16
  autoload :DateLocalizer, 'jekyll/language-plugin/date_localizer.rb'
16
17
  autoload :VERSION, 'jekyll/language-plugin/version'
@@ -2,39 +2,27 @@ module Jekyll
2
2
  module LanguagePlugin
3
3
  module DateLocalizer
4
4
  def self.localize_date(date, format, context)
5
- translation = Jekyll::LanguagePlugin::LiquidContext.get_language_data(context, 'date')
5
+ language_data = Jekyll::LanguagePlugin::LiquidContext.get_language_data(context)
6
+ translation = language_data.get('date')
6
7
 
7
8
  # validate language translation
8
- raise Jekyll::LanguagePlugin::PluginError.new('Localized date is missing translation.') if translation.nil? ||
9
- !['abbr_daynames', 'daynames', 'abbr_monthnames', 'monthnames'].all? {|s| translation.key?(s) && translation[s].is_a?(Array) } ||
10
- translation['abbr_daynames'].size < 7 || translation['daynames'].size < 7 ||
11
- translation['abbr_monthnames'].size < 12 || translation['monthnames'].size < 12
12
-
13
- #convert to extended Time class
14
- date2 = JLPTime.at(date.to_i)
15
- date2.strftime_translate(format, translation)
16
- end
17
-
18
- class JLPTime < Time
19
- def strftime_translate(format = '%F', translation)
20
- result = self.strftime(
21
- #...you replaced the language dependent parts.
22
- format.gsub(/%([aAbB])/){ |m|
23
- case $1
24
- when 'a'; translation['abbr_daynames'][self.wday]
25
- when 'A'; translation['daynames'][self.wday]
26
- when 'b'; translation['abbr_monthnames'][self.mon-1]
27
- when 'B'; translation['monthnames'][self.mon-1]
28
- else
29
- raise Jekyll::LanguagePlugin::PluginError.new('Internal error.')
30
- end
31
- })
32
- if defined? @@encoding_converter
33
- @@encoding_converter.iconv(result)
34
- else
35
- result
36
- end
9
+ if !['abbr_daynames', 'daynames', 'abbr_monthnames', 'monthnames'].all? {|s| translation.key?(s) && translation[s].is_a?(Array) } ||
10
+ translation['abbr_daynames'].size < 7 || translation['daynames'].size < 7 ||
11
+ translation['abbr_monthnames'].size < 12 || translation['monthnames'].size < 12
12
+ raise Jekyll::LanguagePlugin::PluginError.new('Localized date is missing translation.')
37
13
  end
14
+
15
+ date.strftime(
16
+ format.gsub(/%([aAbB])/){ |m|
17
+ case $1
18
+ when 'a'; translation['abbr_daynames'][date.wday]
19
+ when 'A'; translation['daynames'][date.wday]
20
+ when 'b'; translation['abbr_monthnames'][date.mon-1]
21
+ when 'B'; translation['monthnames'][date.mon-1]
22
+ else
23
+ raise Jekyll::LanguagePlugin::PluginError.new('Internal error.')
24
+ end
25
+ })
38
26
  end
39
27
  end
40
28
  end
@@ -0,0 +1,58 @@
1
+ module Jekyll
2
+ module LanguagePlugin
3
+ class LanguageData
4
+ attr_reader :site, :Language
5
+
6
+ def initialize(site, language)
7
+ @site = site
8
+ @language = language
9
+ end
10
+
11
+ def get(keys)
12
+ obj = retrieve_obj(keys)
13
+ raise Jekyll::LanguagePlugin::PluginError.new("Invalid language data configuration. #{keys.join('.')} not found.") if language_data.nil?
14
+ obj
15
+ end
16
+
17
+ def has?(keys)
18
+ !retrieve_obj(keys).nil?
19
+ end
20
+
21
+ def retrieve_obj(keys)
22
+ return nil if @language.to_s.empty?
23
+ keys = resolve_dot_notation([language_data, keys])
24
+
25
+ obj = site.send(keys.shift)
26
+ traverse_hash(obj, keys)
27
+ end
28
+
29
+ def language_data()
30
+ @language_data ||= site.config['language_data'].to_s || 'data.lang.%%'
31
+ @language_data.gsub("%%", @language)
32
+ end
33
+
34
+ def traverse_hash(hash, keys)
35
+ return nil if hash.nil?
36
+
37
+ for key in keys
38
+ if !hash.is_a?(Hash)
39
+ return hash
40
+ elsif !hash.key?(key)
41
+ return nil
42
+ end
43
+ hash = hash[key]
44
+ end
45
+ hash
46
+ end
47
+
48
+ def resolve_dot_notation(keys)
49
+ return keys.split('.') if keys.is_a?(String)
50
+ return [] if !keys.is_a?(Enumerable)
51
+
52
+ keys.flat_map do |key|
53
+ resolve_dot_notation(key)
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -1,62 +1,43 @@
1
1
  module Jekyll
2
2
  module LanguagePlugin
3
3
  module LiquidContext
4
- def self.get_language_data(context, keys = nil)
5
- site = context.registers[:site]
6
- language_data = site.config['language_data'].to_s || 'data.lang.%%'
7
-
8
- page_language = context.registers[:page]['language']
9
- return nil if page_language.to_s.empty?
10
-
11
- keys2 = language_data.gsub("%%", page_language).split('.')
12
- language_data = site.send(keys2.shift)
13
- language_data = self.traverse_hash(language_data, keys2)
14
- raise Jekyll::LanguagePlugin::PluginError.new('Invalid language data configuration.') if language_data.nil?
4
+ def self.get_language_data(context)
5
+ if !context.registers[:language_data].nil?
6
+ return context.registers[:language_data]
7
+ end
15
8
 
16
- return language_data if keys.to_s.empty? # can also return nil
9
+ language = context.registers[:page]['language']
10
+ if language.to_s.empty?
11
+ raise Jekyll::LanguagePlugin::PluginError.new('No language specified for current page or post.')
12
+ end
17
13
 
18
- language_data = self.traverse_hash(language_data, keys)
19
- raise Jekyll::LanguagePlugin::PluginError.new("Language subset #{keys.to_s} not found.") if language_data.nil?
20
- language_data
14
+ site = context.registers[:site]
15
+ context.registers[:language_data] = LanguageData.new(site, language)
21
16
  end
22
17
 
23
18
  def self.get_language_string(context, key)
24
- data = self.get_language_data(context)
25
- return "" if data.nil?
26
-
27
- language_subset = context.registers[:page]['subset']
28
- if (!language_subset.to_s.empty? &&
29
- !(str = traverse_hash(traverse_hash(data, language_subset), key)).to_s.empty?) ||
30
- !(str = traverse_hash(data, key)).to_s.empty?
31
- return str
19
+ language_data = self.get_language_data(context)
20
+
21
+ if language_data.nil?
22
+ $stdout.puts("language data is nil")
23
+ end
24
+ subset = context.registers[:page]['subset']
25
+ if !subset.to_s.empty? && language_data.has?([subset, key])
26
+ return language_data.get([subset, key])
32
27
  end
33
28
 
34
- raise Jekyll::LanguagePlugin::PluginError.new("Language string #{key} not found.")
29
+ language_data.get(key)
35
30
  end
36
31
 
37
32
  def self.get_language_name(context, name)
38
- translation = self.get_language_data(context, 'lang')
33
+ language_data = self.get_language_data(context)
34
+ translation = language_data.get('lang')
39
35
 
40
36
  raise Jekyll::LanguagePlugin::PluginError.new('Language name not found in translation.') if translation.nil? ||
41
37
  !translation.key?(name)
42
38
 
43
39
  translation[name]
44
40
  end
45
-
46
- def self.traverse_hash(hash, keys)
47
- return nil if hash.nil? || keys.to_s.empty?
48
- keys = keys.split('.') if keys.is_a?(String)
49
-
50
- for key in keys
51
- if !hash.is_a?(Hash)
52
- return hash
53
- elsif !hash.key?(key)
54
- return nil
55
- end
56
- hash = hash[key]
57
- end
58
- hash
59
- end
60
41
  end
61
42
  end
62
43
  end
@@ -7,7 +7,10 @@ module Jekyll
7
7
  end
8
8
 
9
9
  def page_language(context)
10
- context.registers[:page].nil? ? "." : context.registers[:page]["language"]
10
+ if context.registers[:page].nil? || context.registers[:page]['language'].to_s.empty?
11
+ raise Jekyll::LanguagePlugin::PluginError.new('No language specified for current page or post.')
12
+ end
13
+ context.registers[:page].nil? ? "." : context.registers[:page]["language"]
11
14
  end
12
15
 
13
16
  def resolved_includes_dir(context)
@@ -1,5 +1,5 @@
1
1
  module Jekyll
2
2
  module LanguagePlugin
3
- VERSION = '1.1.7'
3
+ VERSION = '1.1.8'
4
4
  end
5
5
  end
@@ -13,6 +13,11 @@ module Jekyll
13
13
  data['languages']
14
14
  end
15
15
 
16
+ def subset
17
+ return nil if data.nil? || data['subset'].nil?
18
+ data['subset']
19
+ end
20
+
16
21
  def url_template
17
22
  return "/:language" + url_template_orig if !language.nil?
18
23
  url_template_orig
@@ -20,7 +25,8 @@ module Jekyll
20
25
 
21
26
  def url_placeholders
22
27
  url_placeholders_orig.merge!({
23
- language: language
28
+ language: language,
29
+ subset: subset
24
30
  })
25
31
  end
26
32
  end
@@ -13,15 +13,30 @@ module Jekyll
13
13
  data['languages']
14
14
  end
15
15
 
16
+ def subset
17
+ return nil if data.nil? || data['subset'].nil?
18
+ data['subset']
19
+ end
20
+
16
21
  def template
17
22
  return "/:language" + template_orig if !language.nil?
18
23
  template_orig
19
24
  end
20
25
 
21
26
  def url_placeholders
22
- url_placeholders_orig.merge!({
23
- language: language
27
+ result = url_placeholders_orig.merge!({
28
+ language: language,
29
+ subset: subset
24
30
  })
31
+
32
+ if !language.nil? && !subset.nil?
33
+ @language_data ||= LanguagePlugin::LanguageData.new(@site, language)
34
+
35
+ data = @language_data.get(subset).reject{ |k, v| v.is_a?(Enumerable) }
36
+ result.merge!(Hash[data.map{ |k, v| ["t.#{k}", v] }])
37
+ end
38
+
39
+ result
25
40
  end
26
41
  end
27
42
  end
@@ -5,7 +5,9 @@ module Jekyll
5
5
  def read(files)
6
6
  read_orig(files).flat_map do |page|
7
7
  lpages = []
8
- lpage = LanguagePage.new(@site, @site.source, page.dir, page.name)
8
+ #TODO: this is a bit hacky. It would be better to directly have access to @dir instance variable
9
+ path = page.url_placeholders[:path]
10
+ lpage = LanguagePage.new(@site, @site.source, path, page.name)
9
11
  if lpage.languages
10
12
  for language in lpage.languages do
11
13
  if lpage.language == language
@@ -14,7 +16,7 @@ module Jekyll
14
16
  lpage.data['language'] = language
15
17
  lpages << lpage
16
18
  else
17
- lpage2 = LanguagePage.new(@site, @site.source, page.dir, page.name)
19
+ lpage2 = LanguagePage.new(@site, @site.source, path, page.name)
18
20
  lpage2.data['language'] = language
19
21
  lpages << lpage2
20
22
  end
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.1.7
4
+ version: 1.1.8
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-11-23 00:00:00.000000000 Z
11
+ date: 2015-12-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jekyll
@@ -76,6 +76,7 @@ files:
76
76
  - lib/jekyll-language-plugin.rb
77
77
  - lib/jekyll/language-plugin/date_localizer.rb
78
78
  - lib/jekyll/language-plugin/filters/language_date.rb
79
+ - lib/jekyll/language-plugin/language_data.rb
79
80
  - lib/jekyll/language-plugin/liquid_context.rb
80
81
  - lib/jekyll/language-plugin/plugin_error.rb
81
82
  - lib/jekyll/language-plugin/tags/language.rb