From 8b6719980d316c6239046a0e89ef9c364bd980c8 Mon Sep 17 00:00:00 2001
From: Pavel Goltsev
Date: Wed, 18 Jul 2018 23:46:55 +0300
Subject: [PATCH 01/13] Add localization of site properties defined in
'localize_site'
---
lib/jekyll-multiple-languages-plugin.rb | 44 +++++++++++++++++++++++++
1 file changed, 44 insertions(+)
diff --git a/lib/jekyll-multiple-languages-plugin.rb b/lib/jekyll-multiple-languages-plugin.rb
index 1cbabf6..feeba89 100644
--- a/lib/jekyll-multiple-languages-plugin.rb
+++ b/lib/jekyll-multiple-languages-plugin.rb
@@ -82,6 +82,42 @@ def process
exit
end
+ site_props_to_localize = self.config['localize_site'] || []
+ site_values = {}
+ # Saving original values.
+ site_props_to_localize.each do |prop_name|
+ site_values[prop_name] = self.config[prop_name]
+ end
+
+ #======================================
+ # translate_site
+ #
+ # Translate properties mentioned in a list 'localize_site' of _config.yml.
+ # If the property is not defined or it is empty, no translation is performed.
+ #======================================
+ def translate_site(lang, site_values, site_props)
+ def translate_key(key, lang)
+ unless self.parsed_translations.has_key?(lang)
+ puts "Loading translation from file #{self.source}/_i18n/#{lang}.yml"
+ self.parsed_translations[lang] = YAML.load_file("#{self.source}/_i18n/#{lang}.yml")
+ end
+
+ translation = self.parsed_translations[lang].access(key) if key.is_a?(String)
+
+ if translation.nil? or translation.empty?
+ translation = self.parsed_translations[self.config['default_lang']].access(key)
+
+ puts "Missing i18n key: #{lang}:#{key}"
+ puts "Using translation '%s' from default language: %s" %[translation, self.config['default_lang']]
+ end
+
+ return translation
+ end
+
+ site_props.each do |prop_name|
+ self.config[prop_name] = translate_key(site_values[prop_name], lang)
+ end
+ end
# Variables
#-------------------------------------------------------------------------
@@ -99,6 +135,8 @@ def process
self.config['baseurl_root'] = baseurl_org # Baseurl of website root (without the appended language code)
self.config['translations'] = self.parsed_translations # Hash that stores parsed translations read from YAML files. Exposes this hash to Liquid.
+ # Translate site attributes to default language
+ translate_site(languages.first, site_values, site_props_to_localize)
# Build the website for default language
#-------------------------------------------------------------------------
@@ -120,6 +158,9 @@ def process
self.config['baseurl'] = baseurl_org + "/" + lang
self.config['lang'] = lang
+ # Translate site attributes to current language
+ translate_site(lang, site_values, site_props_to_localize)
+
puts "Building site for language: \"#{self.config['lang']}\" to: #{self.dest}"
process_org
@@ -129,6 +170,9 @@ def process
self.config[ 'baseurl' ] = baseurl_org # Baseurl set on _config.yml
@dest = dest_org # Destination folder where the website is generated
+ # Get translation back to default language
+ translate_site(languages.first, site_values, site_props_to_localize)
+
puts 'Build complete'
end
From 26d5b9a3ff9d56c77b8e9ab19566432fb58abf8a Mon Sep 17 00:00:00 2001
From: Pavel Goltsev
Date: Thu, 19 Jul 2018 00:20:23 +0300
Subject: [PATCH 02/13] Extract site localization logic into separate class
---
lib/jekyll-multiple-languages-plugin.rb | 109 +++++++++++++++---------
1 file changed, 69 insertions(+), 40 deletions(-)
diff --git a/lib/jekyll-multiple-languages-plugin.rb b/lib/jekyll-multiple-languages-plugin.rb
index feeba89..c6954bd 100644
--- a/lib/jekyll-multiple-languages-plugin.rb
+++ b/lib/jekyll-multiple-languages-plugin.rb
@@ -82,42 +82,6 @@ def process
exit
end
- site_props_to_localize = self.config['localize_site'] || []
- site_values = {}
- # Saving original values.
- site_props_to_localize.each do |prop_name|
- site_values[prop_name] = self.config[prop_name]
- end
-
- #======================================
- # translate_site
- #
- # Translate properties mentioned in a list 'localize_site' of _config.yml.
- # If the property is not defined or it is empty, no translation is performed.
- #======================================
- def translate_site(lang, site_values, site_props)
- def translate_key(key, lang)
- unless self.parsed_translations.has_key?(lang)
- puts "Loading translation from file #{self.source}/_i18n/#{lang}.yml"
- self.parsed_translations[lang] = YAML.load_file("#{self.source}/_i18n/#{lang}.yml")
- end
-
- translation = self.parsed_translations[lang].access(key) if key.is_a?(String)
-
- if translation.nil? or translation.empty?
- translation = self.parsed_translations[self.config['default_lang']].access(key)
-
- puts "Missing i18n key: #{lang}:#{key}"
- puts "Using translation '%s' from default language: %s" %[translation, self.config['default_lang']]
- end
-
- return translation
- end
-
- site_props.each do |prop_name|
- self.config[prop_name] = translate_key(site_values[prop_name], lang)
- end
- end
# Variables
#-------------------------------------------------------------------------
@@ -135,8 +99,9 @@ def translate_key(key, lang)
self.config['baseurl_root'] = baseurl_org # Baseurl of website root (without the appended language code)
self.config['translations'] = self.parsed_translations # Hash that stores parsed translations read from YAML files. Exposes this hash to Liquid.
+ site_localize = SiteLocalize.new(self)
# Translate site attributes to default language
- translate_site(languages.first, site_values, site_props_to_localize)
+ site_localize.translate(languages.first)
# Build the website for default language
#-------------------------------------------------------------------------
@@ -159,7 +124,7 @@ def translate_key(key, lang)
self.config['lang'] = lang
# Translate site attributes to current language
- translate_site(lang, site_values, site_props_to_localize)
+ site_localize.translate(lang)
puts "Building site for language: \"#{self.config['lang']}\" to: #{self.dest}"
@@ -170,8 +135,8 @@ def translate_key(key, lang)
self.config[ 'baseurl' ] = baseurl_org # Baseurl set on _config.yml
@dest = dest_org # Destination folder where the website is generated
- # Get translation back to default language
- translate_site(languages.first, site_values, site_props_to_localize)
+ # Set translation to default language
+ site_localize.translate(languages.first)
puts 'Build complete'
end
@@ -517,6 +482,70 @@ def access(path)
end
+################################################################################
+# class SiteLocalize
+################################################################################
+class SiteLocalize
+ #======================================
+ # initialize
+ #======================================
+ def initialize(site)
+ super()
+ @site = site
+ self.store_original_values
+ end
+
+ #======================================
+ # store_original_values
+ #
+ # Store original values of site properties defined
+ # in a property 'localize_site' of _config.yml.
+ #======================================
+ def store_original_values
+ @site_props = @site.config['localize_site'] || []
+ @site_values = {}
+ # Saving original values.
+ @site_props.each do |prop_name|
+ @site_values[prop_name] = @site.config[prop_name]
+ end
+ end
+
+ #======================================
+ # translate
+ #
+ # Translate properties defined in a list 'localize_site' of _config.yml.
+ #======================================
+ def translate(lang)
+ @site_props.each do |prop_name|
+ @site.config[prop_name] = self.translate_key(@site_values[prop_name], lang)
+ end
+ end
+
+ #======================================
+ # translate_key
+ #
+ # Translate given key to given language.
+ #======================================
+ def translate_key(key, lang)
+ unless @site.parsed_translations.has_key?(lang)
+ puts "Loading translation from file #{@site.source}/_i18n/#{lang}.yml"
+ @site.parsed_translations[lang] = YAML.load_file("#{@site.source}/_i18n/#{lang}.yml")
+ end
+
+ translation = @site.parsed_translations[lang].access(key) if key.is_a?(String)
+
+ if translation.nil? or translation.empty?
+ translation = @site.parsed_translations[@site.config['default_lang']].access(key)
+
+ puts "Missing i18n key: #{lang}:#{key}"
+ puts "Using translation '%s' from default language: %s" %[translation, @site.config['default_lang']]
+ end
+
+ translation
+ end
+end
+
+
################################################################################
# Liquid tags definitions
From cb46b766608710976d7c8bbbbc84987f3d3aa1af Mon Sep 17 00:00:00 2001
From: Pavel Goltsev
Date: Thu, 19 Jul 2018 00:25:29 +0300
Subject: [PATCH 03/13] Convert 'translate_key' method to a module based
function
It was needed to remove duplicate code that resided in
'LocalizeTag.render' method and newly created
'SiteLocalize.translate_key'.
---
lib/jekyll-multiple-languages-plugin.rb | 62 +++++++++++--------------
1 file changed, 28 insertions(+), 34 deletions(-)
diff --git a/lib/jekyll-multiple-languages-plugin.rb b/lib/jekyll-multiple-languages-plugin.rb
index c6954bd..25b7ff6 100644
--- a/lib/jekyll-multiple-languages-plugin.rb
+++ b/lib/jekyll-multiple-languages-plugin.rb
@@ -319,16 +319,7 @@ def render(context)
site.parsed_translations[lang] = YAML.load_file("#{site.source}/_i18n/#{lang}.yml")
end
- translation = site.parsed_translations[lang].access(key) if key.is_a?(String)
-
- if translation.nil? or translation.empty?
- translation = site.parsed_translations[site.config['default_lang']].access(key)
-
- puts "Missing i18n key: #{lang}:#{key}"
- puts "Using translation '%s' from default language: %s" %[translation, site.config['default_lang']]
- end
-
- translation
+ translate_key(key, lang, site)
end
end
@@ -482,6 +473,32 @@ def access(path)
end
+
+#======================================
+# translate_key
+#
+# Translate given key to given language.
+#======================================
+def translate_key(key, lang, site)
+ unless site.parsed_translations.has_key?(lang)
+ puts "Loading translation from file #{site.source}/_i18n/#{lang}.yml"
+ site.parsed_translations[lang] = YAML.load_file("#{site.source}/_i18n/#{lang}.yml")
+ end
+
+ translation = site.parsed_translations[lang].access(key) if key.is_a?(String)
+
+ if translation.nil? or translation.empty?
+ translation = site.parsed_translations[site.config['default_lang']].access(key)
+
+ puts "Missing i18n key: #{lang}:#{key}"
+ puts "Using translation '%s' from default language: %s" %[translation, site.config['default_lang']]
+ end
+
+ translation
+end
+
+
+
################################################################################
# class SiteLocalize
################################################################################
@@ -517,32 +534,9 @@ def store_original_values
#======================================
def translate(lang)
@site_props.each do |prop_name|
- @site.config[prop_name] = self.translate_key(@site_values[prop_name], lang)
+ @site.config[prop_name] = translate_key(@site_values[prop_name], lang, @site)
end
end
-
- #======================================
- # translate_key
- #
- # Translate given key to given language.
- #======================================
- def translate_key(key, lang)
- unless @site.parsed_translations.has_key?(lang)
- puts "Loading translation from file #{@site.source}/_i18n/#{lang}.yml"
- @site.parsed_translations[lang] = YAML.load_file("#{@site.source}/_i18n/#{lang}.yml")
- end
-
- translation = @site.parsed_translations[lang].access(key) if key.is_a?(String)
-
- if translation.nil? or translation.empty?
- translation = @site.parsed_translations[@site.config['default_lang']].access(key)
-
- puts "Missing i18n key: #{lang}:#{key}"
- puts "Using translation '%s' from default language: %s" %[translation, @site.config['default_lang']]
- end
-
- translation
- end
end
From 508e0372b2f7d3f429a7754ded61c5e91f6567fb Mon Sep 17 00:00:00 2001
From: Pavel Goltsev
Date: Thu, 19 Jul 2018 01:26:12 +0300
Subject: [PATCH 04/13] Add handling of invalid property names
Correct handling of empty property name, a name defined as not a string
and a name consists of only spaces has been added.
---
lib/jekyll-multiple-languages-plugin.rb | 20 +++++++++++++++++---
1 file changed, 17 insertions(+), 3 deletions(-)
diff --git a/lib/jekyll-multiple-languages-plugin.rb b/lib/jekyll-multiple-languages-plugin.rb
index 25b7ff6..483b0fb 100644
--- a/lib/jekyll-multiple-languages-plugin.rb
+++ b/lib/jekyll-multiple-languages-plugin.rb
@@ -509,7 +509,22 @@ class SiteLocalize
def initialize(site)
super()
@site = site
- self.store_original_values
+ self.save_props
+ self.save_original_values
+ end
+
+ def save_props
+ @site_props = Array.new
+ (@site.config['localize_site'] || []).each do |prop_name|
+ if prop_name.is_a? String
+ prop_name = prop_name.strip
+ unless prop_name.empty?
+ @site_props.push(prop_name)
+ end
+ else
+ puts "Incorrect property name #{prop_name}. Must be a string"
+ end
+ end
end
#======================================
@@ -518,8 +533,7 @@ def initialize(site)
# Store original values of site properties defined
# in a property 'localize_site' of _config.yml.
#======================================
- def store_original_values
- @site_props = @site.config['localize_site'] || []
+ def save_original_values
@site_values = {}
# Saving original values.
@site_props.each do |prop_name|
From 55256d08473e4cc656d5369f9c266ecf9cde4ded Mon Sep 17 00:00:00 2001
From: Pavel Goltsev
Date: Thu, 19 Jul 2018 15:35:28 +0300
Subject: [PATCH 05/13] Add handling of missing site properties proposed for
i18n
Also minor refactoring in favor of little optimization has been applied.
---
lib/jekyll-multiple-languages-plugin.rb | 35 +++++++++----------------
1 file changed, 13 insertions(+), 22 deletions(-)
diff --git a/lib/jekyll-multiple-languages-plugin.rb b/lib/jekyll-multiple-languages-plugin.rb
index 483b0fb..11eadfb 100644
--- a/lib/jekyll-multiple-languages-plugin.rb
+++ b/lib/jekyll-multiple-languages-plugin.rb
@@ -510,45 +510,36 @@ def initialize(site)
super()
@site = site
self.save_props
- self.save_original_values
end
def save_props
- @site_props = Array.new
+ @site_props = {}
(@site.config['localize_site'] || []).each do |prop_name|
- if prop_name.is_a? String
+ if prop_name.is_a?(String)
prop_name = prop_name.strip
- unless prop_name.empty?
- @site_props.push(prop_name)
+ if prop_name.empty?
+ puts "There is empty property defined in 'localize_site'"
+ else
+ prop_value = @site.config[prop_name]
+ if prop_value and !prop_value.empty?
+ # If site has this property, saving it for further translation
+ @site_props[prop_name] = prop_value
+ end
end
else
- puts "Incorrect property name #{prop_name}. Must be a string"
+ puts "Incorrect property name '#{prop_name}'. Must be a string"
end
end
end
- #======================================
- # store_original_values
- #
- # Store original values of site properties defined
- # in a property 'localize_site' of _config.yml.
- #======================================
- def save_original_values
- @site_values = {}
- # Saving original values.
- @site_props.each do |prop_name|
- @site_values[prop_name] = @site.config[prop_name]
- end
- end
-
#======================================
# translate
#
# Translate properties defined in a list 'localize_site' of _config.yml.
#======================================
def translate(lang)
- @site_props.each do |prop_name|
- @site.config[prop_name] = translate_key(@site_values[prop_name], lang, @site)
+ @site_props.each do |prop_name, prop_value|
+ @site.config[prop_name] = translate_key(prop_value, lang, @site)
end
end
end
From d8150880d24b9d8809cbb9185ad55c060e9e1999 Mon Sep 17 00:00:00 2001
From: Pavel Goltsev
Date: Thu, 19 Jul 2018 15:36:38 +0300
Subject: [PATCH 06/13] Add method comment
---
lib/jekyll-multiple-languages-plugin.rb | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/lib/jekyll-multiple-languages-plugin.rb b/lib/jekyll-multiple-languages-plugin.rb
index 11eadfb..0b6bb5e 100644
--- a/lib/jekyll-multiple-languages-plugin.rb
+++ b/lib/jekyll-multiple-languages-plugin.rb
@@ -512,6 +512,12 @@ def initialize(site)
self.save_props
end
+ #======================================
+ # save_props
+ #
+ # Save property names and their values from 'site'
+ # which are proposed for i18n.
+ #======================================
def save_props
@site_props = {}
(@site.config['localize_site'] || []).each do |prop_name|
From e7051f924a8b1f582695417f99d91786cf6c3da3 Mon Sep 17 00:00:00 2001
From: Pavel Goltsev
Date: Fri, 20 Jul 2018 15:10:42 +0300
Subject: [PATCH 07/13] Apply refactoring
Class 'SiteLocalize' was renamed to 'Localizer' and reduced to
a common implementation, not only for site configuration localization.
A property which stores properties that must be localized renamed
to 'localize_props'.
---
lib/jekyll-multiple-languages-plugin.rb | 38 +++++++++++++------------
1 file changed, 20 insertions(+), 18 deletions(-)
diff --git a/lib/jekyll-multiple-languages-plugin.rb b/lib/jekyll-multiple-languages-plugin.rb
index 0b6bb5e..4891ba8 100644
--- a/lib/jekyll-multiple-languages-plugin.rb
+++ b/lib/jekyll-multiple-languages-plugin.rb
@@ -99,9 +99,9 @@ def process
self.config['baseurl_root'] = baseurl_org # Baseurl of website root (without the appended language code)
self.config['translations'] = self.parsed_translations # Hash that stores parsed translations read from YAML files. Exposes this hash to Liquid.
- site_localize = SiteLocalize.new(self)
+ localizer = Localizer.new(self.config,self)
# Translate site attributes to default language
- site_localize.translate(languages.first)
+ localizer.localize_to(languages.first)
# Build the website for default language
#-------------------------------------------------------------------------
@@ -124,7 +124,7 @@ def process
self.config['lang'] = lang
# Translate site attributes to current language
- site_localize.translate(lang)
+ localizer.localize_to(lang)
puts "Building site for language: \"#{self.config['lang']}\" to: #{self.dest}"
@@ -136,7 +136,7 @@ def process
@dest = dest_org # Destination folder where the website is generated
# Set translation to default language
- site_localize.translate(languages.first)
+ localizer.localize_to(languages.first)
puts 'Build complete'
end
@@ -500,36 +500,38 @@ def translate_key(key, lang, site)
################################################################################
-# class SiteLocalize
+# class Localize
################################################################################
-class SiteLocalize
+class Localizer
#======================================
# initialize
#======================================
- def initialize(site)
+ def initialize(data, site, props_key_name = 'localize_props')
super()
+ @data = data
@site = site
+ @props_key_name = props_key_name
self.save_props
end
#======================================
# save_props
#
- # Save property names and their values from 'site'
+ # Save property names and their values from data object
# which are proposed for i18n.
#======================================
def save_props
- @site_props = {}
- (@site.config['localize_site'] || []).each do |prop_name|
+ @props = {}
+ (@data[@props_key_name] || []).each do |prop_name|
if prop_name.is_a?(String)
prop_name = prop_name.strip
if prop_name.empty?
- puts "There is empty property defined in 'localize_site'"
+ puts "There is empty property defined in '#{@props_key_name}'"
else
- prop_value = @site.config[prop_name]
+ prop_value = @data[prop_name]
if prop_value and !prop_value.empty?
# If site has this property, saving it for further translation
- @site_props[prop_name] = prop_value
+ @props[prop_name] = prop_value
end
end
else
@@ -539,13 +541,13 @@ def save_props
end
#======================================
- # translate
+ # localize_to
#
- # Translate properties defined in a list 'localize_site' of _config.yml.
+ # Perform localization of properties defined in localization property list.
#======================================
- def translate(lang)
- @site_props.each do |prop_name, prop_value|
- @site.config[prop_name] = translate_key(prop_value, lang, @site)
+ def localize_to(lang)
+ @props.each do |prop_name, prop_value|
+ @data[prop_name] = translate_key(prop_value, lang, @site)
end
end
end
From 39792ee66525f54740bfe16fe37d8bb1134e987c Mon Sep 17 00:00:00 2001
From: Pavel Goltsev
Date: Fri, 20 Jul 2018 16:54:50 +0300
Subject: [PATCH 08/13] Add localization of keys in Front Matter of every page
---
lib/jekyll-multiple-languages-plugin.rb | 15 ++++++++++++++-
1 file changed, 14 insertions(+), 1 deletion(-)
diff --git a/lib/jekyll-multiple-languages-plugin.rb b/lib/jekyll-multiple-languages-plugin.rb
index 4891ba8..60e7dc5 100644
--- a/lib/jekyll-multiple-languages-plugin.rb
+++ b/lib/jekyll-multiple-languages-plugin.rb
@@ -49,7 +49,20 @@ module Jekyll
end
-
+ #*****************************************************************************
+ # :site, :pre_render hook
+ #*****************************************************************************
+ Jekyll::Hooks.register :site, :pre_render do |site, payload|
+
+ # Localize front matter data of every page.
+ #===========================================================================
+ lang = site.config['lang']
+ puts "Localizing pages's data for language: #{lang} ..."
+ (site.pages + site.documents).each do |item|
+ localizer = Localizer.new(item.data, site)
+ localizer.localize_to(lang)
+ end
+ end
##############################################################################
# class Site
From 8d3ccfcb66aa1bc524c428c811618795781fd735 Mon Sep 17 00:00:00 2001
From: Pavel Goltsev
Date: Fri, 20 Jul 2018 17:29:19 +0300
Subject: [PATCH 09/13] Apply refactoring
Method 'localize_to' of Localizer renamed to 'translate_to'.
Name of a property for translation renamed from 'localize_props' to
'translate_to'.
---
lib/jekyll-multiple-languages-plugin.rb | 16 ++++++++--------
1 file changed, 8 insertions(+), 8 deletions(-)
diff --git a/lib/jekyll-multiple-languages-plugin.rb b/lib/jekyll-multiple-languages-plugin.rb
index 60e7dc5..fbc6fbc 100644
--- a/lib/jekyll-multiple-languages-plugin.rb
+++ b/lib/jekyll-multiple-languages-plugin.rb
@@ -60,7 +60,7 @@ module Jekyll
puts "Localizing pages's data for language: #{lang} ..."
(site.pages + site.documents).each do |item|
localizer = Localizer.new(item.data, site)
- localizer.localize_to(lang)
+ localizer.translate_to(lang)
end
end
@@ -114,7 +114,7 @@ def process
localizer = Localizer.new(self.config,self)
# Translate site attributes to default language
- localizer.localize_to(languages.first)
+ localizer.translate_to(languages.first)
# Build the website for default language
#-------------------------------------------------------------------------
@@ -137,7 +137,7 @@ def process
self.config['lang'] = lang
# Translate site attributes to current language
- localizer.localize_to(lang)
+ localizer.translate_to(lang)
puts "Building site for language: \"#{self.config['lang']}\" to: #{self.dest}"
@@ -149,7 +149,7 @@ def process
@dest = dest_org # Destination folder where the website is generated
# Set translation to default language
- localizer.localize_to(languages.first)
+ localizer.translate_to(languages.first)
puts 'Build complete'
end
@@ -519,7 +519,7 @@ class Localizer
#======================================
# initialize
#======================================
- def initialize(data, site, props_key_name = 'localize_props')
+ def initialize(data, site, props_key_name = 'translate_props')
super()
@data = data
@site = site
@@ -554,11 +554,11 @@ def save_props
end
#======================================
- # localize_to
+ # translate_to
#
- # Perform localization of properties defined in localization property list.
+ # Perform translation of properties defined in translation property list.
#======================================
- def localize_to(lang)
+ def translate_to(lang)
@props.each do |prop_name, prop_value|
@data[prop_name] = translate_key(prop_value, lang, @site)
end
From 471f29ccbb89e7d533c241f97b5795d2d1383933 Mon Sep 17 00:00:00 2001
From: Pavel Goltsev
Date: Fri, 20 Jul 2018 17:35:32 +0300
Subject: [PATCH 10/13] Fix log message
---
lib/jekyll-multiple-languages-plugin.rb | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/lib/jekyll-multiple-languages-plugin.rb b/lib/jekyll-multiple-languages-plugin.rb
index fbc6fbc..d8f12ba 100644
--- a/lib/jekyll-multiple-languages-plugin.rb
+++ b/lib/jekyll-multiple-languages-plugin.rb
@@ -57,7 +57,7 @@ module Jekyll
# Localize front matter data of every page.
#===========================================================================
lang = site.config['lang']
- puts "Localizing pages's data for language: #{lang} ..."
+ puts "Localizing pages' data for language: #{lang} ..."
(site.pages + site.documents).each do |item|
localizer = Localizer.new(item.data, site)
localizer.translate_to(lang)
From 203fa3c554b5019491565a5cb39c7835cc7e73d1 Mon Sep 17 00:00:00 2001
From: Pavel Goltsev
Date: Fri, 20 Jul 2018 17:35:53 +0300
Subject: [PATCH 11/13] Adjust log message
---
lib/jekyll-multiple-languages-plugin.rb | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/lib/jekyll-multiple-languages-plugin.rb b/lib/jekyll-multiple-languages-plugin.rb
index d8f12ba..d22e3fa 100644
--- a/lib/jekyll-multiple-languages-plugin.rb
+++ b/lib/jekyll-multiple-languages-plugin.rb
@@ -57,7 +57,7 @@ module Jekyll
# Localize front matter data of every page.
#===========================================================================
lang = site.config['lang']
- puts "Localizing pages' data for language: #{lang} ..."
+ puts "Localizing pages' data for language: #{lang}"
(site.pages + site.documents).each do |item|
localizer = Localizer.new(item.data, site)
localizer.translate_to(lang)
From f2d3a87befd0a418094afbef9fbd124a792d501b Mon Sep 17 00:00:00 2001
From: Pavel Goltsev
Date: Fri, 20 Jul 2018 21:17:39 +0300
Subject: [PATCH 12/13] Apply refactoring and simplify code
A bug in 'serve' mode of Jekyll was fixed. The bug was in empty strings
once site rebuilt because of an attempt to translate already translated
strings once again.
Class Localizer has been removed due to redundancy and has been replaced
with function 'translate_props'.
New class TranslatedString was instroduced to store a key within
translated value and distinguish key and translated value.
---
lib/jekyll-multiple-languages-plugin.rb | 92 ++++++++++++-------------
1 file changed, 43 insertions(+), 49 deletions(-)
diff --git a/lib/jekyll-multiple-languages-plugin.rb b/lib/jekyll-multiple-languages-plugin.rb
index d22e3fa..c923eab 100644
--- a/lib/jekyll-multiple-languages-plugin.rb
+++ b/lib/jekyll-multiple-languages-plugin.rb
@@ -56,11 +56,8 @@ module Jekyll
# Localize front matter data of every page.
#===========================================================================
- lang = site.config['lang']
- puts "Localizing pages' data for language: #{lang}"
(site.pages + site.documents).each do |item|
- localizer = Localizer.new(item.data, site)
- localizer.translate_to(lang)
+ translate_props(item.data, site)
end
end
@@ -112,9 +109,8 @@ def process
self.config['baseurl_root'] = baseurl_org # Baseurl of website root (without the appended language code)
self.config['translations'] = self.parsed_translations # Hash that stores parsed translations read from YAML files. Exposes this hash to Liquid.
- localizer = Localizer.new(self.config,self)
# Translate site attributes to default language
- localizer.translate_to(languages.first)
+ translate_props(self.config, self)
# Build the website for default language
#-------------------------------------------------------------------------
@@ -137,7 +133,7 @@ def process
self.config['lang'] = lang
# Translate site attributes to current language
- localizer.translate_to(lang)
+ translate_props(self.config, self)
puts "Building site for language: \"#{self.config['lang']}\" to: #{self.dest}"
@@ -148,9 +144,6 @@ def process
self.config[ 'baseurl' ] = baseurl_org # Baseurl set on _config.yml
@dest = dest_org # Destination folder where the website is generated
- # Set translation to default language
- localizer.translate_to(languages.first)
-
puts 'Build complete'
end
@@ -332,7 +325,7 @@ def render(context)
site.parsed_translations[lang] = YAML.load_file("#{site.source}/_i18n/#{lang}.yml")
end
- translate_key(key, lang, site)
+ TranslatedString.translate(key, lang, site)
end
end
@@ -511,56 +504,57 @@ def translate_key(key, lang, site)
end
-
################################################################################
-# class Localize
+# class TranslatedString
################################################################################
-class Localizer
+class TranslatedString < String
#======================================
# initialize
#======================================
- def initialize(data, site, props_key_name = 'translate_props')
- super()
- @data = data
- @site = site
- @props_key_name = props_key_name
- self.save_props
+ def initialize(*several_variants, key)
+ super(*several_variants)
+ @key = key
+ end
+
+ def key
+ @key
end
#======================================
- # save_props
- #
- # Save property names and their values from data object
- # which are proposed for i18n.
+ # translate
#======================================
- def save_props
- @props = {}
- (@data[@props_key_name] || []).each do |prop_name|
- if prop_name.is_a?(String)
- prop_name = prop_name.strip
- if prop_name.empty?
- puts "There is empty property defined in '#{@props_key_name}'"
- else
- prop_value = @data[prop_name]
- if prop_value and !prop_value.empty?
- # If site has this property, saving it for further translation
- @props[prop_name] = prop_value
- end
- end
- else
- puts "Incorrect property name '#{prop_name}'. Must be a string"
- end
+ def self.translate(str, lang, site)
+ if str.is_a?(TranslatedString)
+ key = str.key
+ else
+ key = str
end
+ return TranslatedString.new(translate_key(key, lang, site), key = key)
end
+end
- #======================================
- # translate_to
- #
- # Perform translation of properties defined in translation property list.
- #======================================
- def translate_to(lang)
- @props.each do |prop_name, prop_value|
- @data[prop_name] = translate_key(prop_value, lang, @site)
+
+#======================================
+# translate_props
+#
+# Perform translation of properties defined in translation property list.
+#======================================
+def translate_props(data, site, props_key_name = 'translate_props')
+ lang = site.config['lang']
+ props = {}
+ (data[props_key_name] || []).each do |prop_name|
+ if prop_name.is_a?(String)
+ prop_name = prop_name.strip
+ if prop_name.empty?
+ puts "There is empty property defined in '#{props_key_name}'"
+ else
+ prop_value = data[prop_name]
+ if prop_value.is_a?(String) and !prop_value.empty?
+ data[prop_name] = TranslatedString.translate(prop_value, lang, site)
+ end
+ end
+ else
+ puts "Incorrect property name '#{prop_name}'. Must be a string"
end
end
end
From 25ee392da9a3504a8a02a9b2dac2384d7c2e88c8 Mon Sep 17 00:00:00 2001
From: Pavel Goltsev
Date: Fri, 3 Dec 2021 15:21:34 +0300
Subject: [PATCH 13/13] Add minor improvements
---
lib/jekyll-multiple-languages-plugin.rb | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/lib/jekyll-multiple-languages-plugin.rb b/lib/jekyll-multiple-languages-plugin.rb
index be46865..190b705 100644
--- a/lib/jekyll-multiple-languages-plugin.rb
+++ b/lib/jekyll-multiple-languages-plugin.rb
@@ -658,12 +658,11 @@ def self.translate(str, lang, site)
#======================================
def translate_props(data, site, props_key_name = 'translate_props')
lang = site.config['lang']
- props = {}
(data[props_key_name] || []).each do |prop_name|
if prop_name.is_a?(String)
prop_name = prop_name.strip
if prop_name.empty?
- puts "There is empty property defined in '#{props_key_name}'"
+ puts "There is an empty property defined in '#{props_key_name}'"
else
prop_value = data[prop_name]
if prop_value.is_a?(String) and !prop_value.empty?