Module: LostInTranslation
- Included in:
- Lit
- Defined in:
- lib/lost_in_translation.rb,
lib/lost_in_translation/hash.rb,
lib/lost_in_translation/railtie.rb,
lib/lost_in_translation/version.rb,
lib/lost_in_translation/z_recent.rb,
lib/lost_in_translation/z_cleanup.rb,
lib/lost_in_translation/difference.rb,
lib/lost_in_translation/z_interactive.rb,
lib/lost_in_translation/file_functions.rb,
lib/lost_in_translation/user_interface.rb,
lib/lost_in_translation/z_full_automatic.rb,
lib/lost_in_translation/z_half_automatic.rb
Defined Under Namespace
Classes: Railtie
Constant Summary collapse
- VERSION =
'0.2.4'
Instance Method Summary collapse
- #ask_for_languages ⇒ Object
- #ask_for_max_count ⇒ Object
- #ask_for_paths ⇒ Object
- #ask_for_permission(lang1, lang2, app_name) ⇒ Object
- #ask_for_sorting(lang, _app_name) ⇒ Object
- #ask_for_translation(value, new_structure, lang1, lang2) ⇒ Object
- #clean(root, compared, structure = [], new_array = []) ⇒ Object
- #cleanup ⇒ Object
- #diff(root, compared, lang1, lang2, structure = [], _max_count = 0, new_array = []) ⇒ Object
- #full_automatic ⇒ Object
- #get_locale(path) ⇒ Object
- #half_automatic ⇒ Object
- #interactive ⇒ Object
- #merge_hash(merge_from, merge_to) ⇒ Object
- #prepare_paths(path1, path2, post) ⇒ Object
- #recent ⇒ Object
- #sort_hash(object) ⇒ Object
- #string_to_hash(string) ⇒ Object
Instance Method Details
#ask_for_languages ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/lost_in_translation/user_interface.rb', line 15 def ask_for_languages system 'clear' puts 'What I18n-yaml files do you want to compare?' lang1 = [(print "Master (e.g. 'en'): "), STDIN.gets.chomp][1] lang2 = [(print "Slave: (e.g. 'de'): "), STDIN.gets.chomp][1] [lang1, lang2] end |
#ask_for_max_count ⇒ Object
23 24 25 26 27 28 |
# File 'lib/lost_in_translation/user_interface.rb', line 23 def ask_for_max_count system 'clear' puts 'How many do you want to edit max?' max_count = [(print 'Max. Count (default = 50000): '), STDIN.gets.chomp][1] max_count end |
#ask_for_paths ⇒ Object
30 31 32 33 34 35 36 37 |
# File 'lib/lost_in_translation/user_interface.rb', line 30 def ask_for_paths system 'clear' puts 'Please type in the absolute paths to the I18n-yaml file you want to compare?' puts 'e.g. /home/youruser/Documents/your-awesome-app/config/locales/en.yml' path1 = [(print 'Master: '), STDIN.gets.chomp][1] path2 = [(print 'Slave:: '), STDIN.gets.chomp][1] [path1, path2] end |
#ask_for_permission(lang1, lang2, app_name) ⇒ Object
4 5 6 7 8 9 10 11 12 13 |
# File 'lib/lost_in_translation/user_interface.rb', line 4 def (lang1, lang2, app_name) system 'clear' puts 'Comparing Locales' puts "Application: #{app_name}" unless app_name.nil? puts "Master Locale: #{lang1}.yml" puts "Slave Locale: #{lang2}.yml" puts 'Is this ok?[y/n]' a = STDIN.gets.chomp a == 'y' || a == 'Y' end |
#ask_for_sorting(lang, _app_name) ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/lost_in_translation/user_interface.rb', line 39 def ask_for_sorting(lang, _app_name) system 'clear' puts "Do you want the #{lang}.yml to be sorted?" puts 'Alphabetically & Recursive (ASC)' puts '[y/n]' a = STDIN.gets.chomp a == 'y' || a == 'Y' end |
#ask_for_translation(value, new_structure, lang1, lang2) ⇒ Object
48 49 50 51 52 53 54 55 |
# File 'lib/lost_in_translation/user_interface.rb', line 48 def ask_for_translation(value, new_structure, lang1, lang2) system 'clear' new_s = new_structure.join('---') puts "#{new_s} is missing" puts "in #{lang1}: #{value}" new_val = [(print lang2 + ': '), STDIN.gets.chomp][1] new_val == '' ? nil : (new_s + '---' + new_val) end |
#clean(root, compared, structure = [], new_array = []) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/lost_in_translation/difference.rb', line 27 def clean(root, compared, structure = [], new_array = []) root.each_pair do |key, value| next_root = root[key] next_compared = compared.nil? ? nil : compared[key] new_structure = structure.dup << key if value.is_a? String unless compared.nil? || compared[key].nil? new_array << "#{new_structure.join('---')}-#{value}" end end clean(next_root, next_compared, new_structure, new_array) if next_root.is_a? Hash end new_array end |
#cleanup ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/lost_in_translation/z_cleanup.rb', line 5 def cleanup if defined? Rails lang2, lang1 = LostInTranslation.ask_for_languages path1 = "#{Rails.root}/config/locales/#{lang1}.yml" path2 = "#{Rails.root}/config/locales/#{lang2}.yml" else path2, path1 = LostInTranslation.ask_for_paths lang1 = LostInTranslation.get_locale(path1) lang2 = LostInTranslation.get_locale(path2) end abort('NOPE') unless File.exist?(path1) && File.exist?(path2) prepare_paths(path1, path2, false) first = YAML.load_file(path1) second = YAML.load_file(path2) new_strings_array = LostInTranslation.clean(first[lang1], second[lang2], [lang2]) first = {} new_strings_array.each do |string| string = string.split('---').map { |x| x == lang2 ? lang1 : x }.join('---') result = LostInTranslation.string_to_hash(string) LostInTranslation.merge_hash(result, first) end File.open(path1, 'w') { |file| file.write(first.to_yaml) } prepare_paths(path1, path2, true) end |
#diff(root, compared, lang1, lang2, structure = [], _max_count = 0, new_array = []) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/lost_in_translation/difference.rb', line 5 def diff(root, compared, lang1, lang2, structure = [], _max_count = 0, new_array = []) count = 0 root.each_pair do |key, value| next_root = root[key] next_compared = compared.nil? ? nil : compared[key] new_structure = structure.dup << key if value.is_a? String if compared.nil? || compared[key].nil? new_val = ask_for_translation(value, new_structure, lang1, lang2) new_array << new_val unless new_val.nil? count += 1 end end diff(next_root, next_compared, lang1, lang2, new_structure, new_array) if next_root.is_a? Hash end new_array end |
#full_automatic ⇒ Object
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/lost_in_translation/z_full_automatic.rb', line 2 def full_automatic if defined? Rails lang2, lang1 = LostInTranslation.ask_for_languages path1 = "#{Rails.root}/config/locales/#{lang1}.yml" path2 = "#{Rails.root}/config/locales/#{lang2}.yml" else path2, path1 = LostInTranslation.ask_for_paths lang1 = LostInTranslation.get_locale(path1) lang2 = LostInTranslation.get_locale(path2) end abort('NOPE') unless File.exist?(path1) && File.exist?(path2) first = YAML.load_file(path1) second = YAML.load_file(path2) new_strings_array = LostInTranslation.clean(first[lang1], second[lang2], [lang2]) first = {} new_strings_array.each do |string| string = string.split('---').map { |x| x == lang2 ? lang1 : x }.join('---') result = LostInTranslation.string_to_hash(string) LostInTranslation.merge_hash(result, first) end File.open(path1, 'w') { |file| file.write(first.to_yaml) } end |
#get_locale(path) ⇒ Object
23 24 25 |
# File 'lib/lost_in_translation/difference.rb', line 23 def get_locale(path) path.split('/').last.split('.').first unless path.empty? end |
#half_automatic ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/lost_in_translation/z_half_automatic.rb', line 5 def half_automatic if defined? Rails lang2, lang1 = LostInTranslation.ask_for_languages path1 = "#{Rails.root}/config/locales/#{lang1}.yml" path2 = "#{Rails.root}/config/locales/#{lang2}.yml" else path2, path1 = LostInTranslation.ask_for_paths lang1 = LostInTranslation.get_locale(path1) lang2 = LostInTranslation.get_locale(path2) end abort('NOPE') unless File.exist?(path1) && File.exist?(path2) first = YAML.load_file(path1) second = YAML.load_file(path2) new_strings_array = LostInTranslation.clean(first[lang1], second[lang2], [lang2]) first = {} new_strings_array.each do |string| string = string.split('---').map { |x| x == lang2 ? lang1 : x }.join('---') result = LostInTranslation.string_to_hash(string) LostInTranslation.merge_hash(result, first) end File.open(path1, 'w') { |file| file.write(first.to_yaml) } end |
#interactive ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/lost_in_translation/z_interactive.rb', line 5 def interactive if defined? Rails app_name = Rails.application.class.parent_name lang1, lang2 = LostInTranslation.ask_for_languages path1 = "#{Rails.root}/config/locales/#{lang1}.yml" path2 = "#{Rails.root}/config/locales/#{lang2}.yml" else app_name = nil path1, path2 = LostInTranslation.ask_for_paths lang1 = LostInTranslation.get_locale(path1) lang2 = LostInTranslation.get_locale(path2) end abort('NOPE') unless File.exist?(path1) && File.exist?(path2) abort('Well, in that case, forget it!') unless LostInTranslation.(lang1, lang2, app_name) prepare_paths(path1, path2, true) prepare_paths(path1, path2, false) first = YAML.load_file(path1) second = YAML.load_file(path2) new_strings_array = LostInTranslation.diff(first[lang1], second[lang2], lang1, lang2, [lang2]) new_strings_array.each do |string| result = LostInTranslation.string_to_hash(string) LostInTranslation.merge_hash(result, second) end if LostInTranslation.ask_for_sorting(lang1, app_name) first = LostInTranslation.sort_hash(first) File.open(path1, 'w') { |file| file.write(first.to_yaml(line_width: -1)) } end second = LostInTranslation.sort_hash(second) if LostInTranslation.ask_for_sorting(lang2, app_name) File.open(path2, 'w') { |file| file.write(second.to_yaml(line_width: -1)) } prepare_paths(path1, path2, true) end |
#merge_hash(merge_from, merge_to) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/lost_in_translation/hash.rb', line 12 def merge_hash(merge_from, merge_to) return if merge_from.is_a?(String) || merge_to.is_a?(String) merged_hash = merge_to first_key = merge_from.keys.first merged_hash[first_key] = if merge_to.key?(first_key) merge_hash(merge_from[first_key], merge_to[first_key]) else merge_from[first_key] end merged_hash end |
#prepare_paths(path1, path2, post) ⇒ Object
5 6 7 8 9 |
# File 'lib/lost_in_translation/file_functions.rb', line 5 def prepare_paths(path1, path2, post) [path1, path2].each do |p| prepare_yaml(p, post) end end |
#recent ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/lost_in_translation/z_recent.rb', line 4 def recent app_name = Rails.application.class.parent_name lang1, lang2 = LostInTranslation.ask_for_languages path1 = "#{Rails.root}/config/locales/#{lang1}.yml" copy_path1 = "#{Rails.root}/config/locales/#{lang1}_copy.yml" path2 = "#{Rails.root}/config/locales/#{lang2}.yml" abort('NOPE') unless File.exist?(path1) && File.exist?(path2) FileUtils.cp(path1, copy_path1) `git checkout "#{path1}"` prepare_paths(path1, copy_path1, false) first = YAML.load_file(path1) first_copy = YAML.load_file(copy_path1) second = YAML.load_file(path2) new_strings_array = LostInTranslation.diff(first_copy[lang1], first[lang1], lang1, lang2, [lang1]) new_strings_array.each do |string| string = string.split('---').map { |x| x == lang1 ? lang2 : x }.join('---') result = LostInTranslation.string_to_hash(string) LostInTranslation.merge_hash(result, second) end if LostInTranslation.ask_for_sorting(lang1, app_name) first_copy = LostInTranslation.sort_hash(first_copy) File.open(path1, 'w') { |file| file.write(first_copy.to_yaml) } end second = LostInTranslation.sort_hash(second) if LostInTranslation.ask_for_sorting(lang2, app_name) File.open(path2, 'w') { |file| file.write(second.to_yaml) } prepare_paths(path1, path2, true) FileUtils.rm(copy_path1) end |
#sort_hash(object) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/lost_in_translation/hash.rb', line 24 def sort_hash(object) return object unless object.is_a?(Hash) hash = {} object.each { |k, v| hash[k] = sort_hash(v) } sorted = hash.sort { |a, b| a[0].to_s <=> b[0].to_s } hash.class[sorted] end |
#string_to_hash(string) ⇒ Object
5 6 7 8 9 10 |
# File 'lib/lost_in_translation/hash.rb', line 5 def string_to_hash(string) array = string.split('---') value = array.pop arr = array.reverse arr[1..-1].inject(arr[0] => value) { |memo, i| { i => memo } } end |