Class: Transdifflation::Comparer
- Inherits:
-
Object
- Object
- Transdifflation::Comparer
- Defined in:
- lib/transdifflation.rb
Overview
Implements the core
Constant Summary collapse
- NOT_TRANSLATED =
const string added to keys not translated
"**NOT_TRANSLATED** "
Instance Attribute Summary collapse
-
#has_changes ⇒ Object
readonly
Instance variable to get if changes have been detected.
Instance Method Summary collapse
-
#coverage_rate(hash_from_locale, hash_to_locale, token = NOT_TRANSLATED) ⇒ Object
Get Coverage rate from two hashes, depending on the number of keys that have a given token.
-
#get_rest_of_translation(source, target, from_locale, to_locale) ⇒ Object
Get Diff from YAML translation locale file from filesystem and generate differences in a file on our host.
-
#get_transdifflation_from_file(tag_name, path_to_yaml_relative_from_rails_root, from_locale = :en, to_locale = :es) ⇒ Object
Get Diff from YAML translation locale file from filesystem and generate differences in a file on our host.
-
#get_transdifflation_from_gem(gem_name, path_to_yaml_in_gem, from_locale = :en, to_locale = :es) ⇒ Object
Get Diff from YAML translation locale file from a gem and generate differences in a file on our host.
-
#initialize ⇒ Comparer
constructor
A new instance of Comparer.
-
#rate_from_branch(hash_from, hash_to, token, words, found) ⇒ Object
Get the number of translated keys.
Constructor Details
#initialize ⇒ Comparer
Returns a new instance of Comparer.
20 21 22 |
# File 'lib/transdifflation.rb', line 20 def initialize @has_changes = false end |
Instance Attribute Details
#has_changes ⇒ Object (readonly)
Instance variable to get if changes have been detected
18 19 20 |
# File 'lib/transdifflation.rb', line 18 def has_changes @has_changes end |
Instance Method Details
#coverage_rate(hash_from_locale, hash_to_locale, token = NOT_TRANSLATED) ⇒ Object
Get Coverage rate from two hashes, depending on the number of keys that have a given token
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/transdifflation.rb', line 118 def coverage_rate(hash_from_locale, hash_to_locale, token = NOT_TRANSLATED) if hash_from_locale.nil? return "Translation coverage error: from_locale language not detected." end if hash_to_locale.nil? return "Translation coverage error: to_locale language not detected." end if hash_from_locale.empty? return "from_locale is empty, so you have everything translated" end words = 0 found = 0 words, found = rate_from_branch(hash_from_locale, hash_to_locale, token, words, found) percent = (found.to_f/words.to_f) * 100 truncate = "%.2f" % percent return "#{truncate}% #{found}/#{words} entries translated" end |
#get_rest_of_translation(source, target, from_locale, to_locale) ⇒ Object
Get Diff from YAML translation locale file from filesystem and generate differences in a file on our host
105 106 107 108 109 110 |
# File 'lib/transdifflation.rb', line 105 def get_rest_of_translation(source, target, from_locale, to_locale) added_diff_hash = {} generate_added_diff(source, target, added_diff_hash, Array.new, from_locale, to_locale, false) added_diff_hash.unsymbolize! end |
#get_transdifflation_from_file(tag_name, path_to_yaml_relative_from_rails_root, from_locale = :en, to_locale = :es) ⇒ Object
Get Diff from YAML translation locale file from filesystem and generate differences in a file on our host
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/transdifflation.rb', line 67 def get_transdifflation_from_file(tag_name, path_to_yaml_relative_from_rails_root, from_locale=:en, to_locale=:es ) #default values in optional params from_locale ||= I18n.default_locale to_locale ||= I18n.locale yml_source_content = YAMLReader.read_YAML_from_pathfile(path_to_yaml_relative_from_rails_root) puts "Loaded YAML content from file '#{path_to_yaml_relative_from_rails_root}'" #build the file name in our host filename_in_SRC = File.basename(path_to_yaml_relative_from_rails_root ) host_target_filename = filename_in_SRC.gsub(/-?#{from_locale}\./) do |match_s| match_s.sub("#{from_locale}", "#{to_locale}") end #The folder is created if doesn't exist if !File.directory?("config/locales/#{to_locale}") FileUtils.mkdir_p(File.join(Rails.root, "config/locales/#{to_locale}")) end host_target_file = File.join( Rails.root, "config/locales/#{to_locale}", "#{tag_name}.#{host_target_filename}") if(!File.file? host_target_file) get_first_time_file(yml_source_content, host_target_file, from_locale, to_locale) else generate_diff_file(yml_source_content, host_target_file, from_locale, to_locale) end @has_changes end |
#get_transdifflation_from_gem(gem_name, path_to_yaml_in_gem, from_locale = :en, to_locale = :es) ⇒ Object
Get Diff from YAML translation locale file from a gem and generate differences in a file on our host
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/transdifflation.rb', line 30 def get_transdifflation_from_gem(gem_name, path_to_yaml_in_gem, from_locale=:en, to_locale=:es ) #default values in optional params from_locale ||= I18n.default_locale to_locale ||= I18n.locale yml_gem_content = YAMLReader.read_YAML_from_gem(gem_name, path_to_yaml_in_gem) puts "Loaded YAML content from gem '#{gem_name}', file '#{path_to_yaml_in_gem}'" #build the file name in our host filename_in_gem_SRC = File.basename( path_to_yaml_in_gem ) host_target_filename = filename_in_gem_SRC.gsub(/-?#{from_locale}\./) do |match_s| match_s.sub("#{from_locale}", "#{to_locale}") end #The folder is created if doesn't exist if !File.directory?("config/locales/#{to_locale}") FileUtils.mkdir_p(File.join(Rails.root, "config/locales/#{to_locale}")) end host_target_file = File.join( Rails.root, "config/locales/#{to_locale}", "#{gem_name}.#{host_target_filename}") if(!File.file? host_target_file) get_first_time_file(yml_gem_content, host_target_file, from_locale, to_locale) else generate_diff_file(yml_gem_content, host_target_file, from_locale, to_locale) end @has_changes end |
#rate_from_branch(hash_from, hash_to, token, words, found) ⇒ Object
Get the number of translated keys
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/transdifflation.rb', line 149 def rate_from_branch(hash_from, hash_to, token, words, found) hash_from.each_pair{ |key, value| if hash_from[key.to_sym].instance_of? Hash if hash_to[key.to_sym] words, found = rate_from_branch(hash_from[key.to_sym], hash_to[key.to_sym], token, words, found) else # Sum other words # could have nested branches, so we call it with hash_from[key.to_sym] to count the number of words, returning the found to a temporal var words, temp = rate_from_branch(hash_from[key.to_sym], hash_from[key.to_sym], token, words, found) end else words += 1 if hash_to[key.to_sym] found += 1 if !hash_to[key.to_sym].to_s.include?(token) end end } return words, found end |