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.5'

Class Method Summary collapse

Class Method Details

.ask_for_languagesObject



15
16
17
18
19
20
21
# File 'lib/lost_in_translation/user_interface.rb', line 15

def self.ask_for_languages
  log
  log '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_countObject



23
24
25
26
27
28
# File 'lib/lost_in_translation/user_interface.rb', line 23

def self.ask_for_max_count
  log
  log 'How many do you want to edit max?'
  max_count = [(print '# Max. Count (default = 50000): '), STDIN.gets.chomp][1]
  max_count
end

.ask_for_pathsObject



30
31
32
33
34
35
36
37
# File 'lib/lost_in_translation/user_interface.rb', line 30

def self.ask_for_paths
  log
  log 'Please type in the absolute paths to the I18n-yaml file you want to compare?'
  log '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



5
6
7
8
9
10
11
12
13
# File 'lib/lost_in_translation/user_interface.rb', line 5

def self.ask_for_permission(lang1, lang2, app_name)
  log
  log 'Comparing Locales'
  log "Application: #{app_name}" unless app_name.nil?
  log "Master Locale: #{lang1}.yml"
  log "Slave Locale: #{lang2}.yml"
  a = [(print "# Is this ok?[Y/n]: "), STDIN.gets.chomp][1]
  a == 'y' || a == 'Y' || a == ''
end

.ask_for_sorting(lang, _app_name) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/lost_in_translation/user_interface.rb', line 39

def self.ask_for_sorting(lang, _app_name)
  log
  log "Do you want the #{lang}.yml to be sorted?"
  log 'Alphabetically & Recursive (ASC)'
  a = [(print "# [Y/n]: "), STDIN.gets.chomp][1]
  a == 'y' || a == 'Y' || a == ''
end

.ask_for_translation(value, new_structure, lang1, lang2) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/lost_in_translation/user_interface.rb', line 47

def self.ask_for_translation(value, new_structure, lang1, lang2)
  log
  new_s = new_structure.join('---')
  log "#{new_s} is missing"
  log "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 self.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

.cleanupObject



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
# File 'lib/lost_in_translation/z_cleanup.rb', line 5

def self.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, true)
  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 self.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_automaticObject



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 self.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 self.get_locale(path)
  path.split('/').last.split('.').first unless path.empty?
end

.half_automaticObject



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 self.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

.interactiveObject



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
45
# File 'lib/lost_in_translation/z_interactive.rb', line 5

def self.interactive
  system 'clear'
  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.ask_for_permission(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

.log(text = '') ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/lost_in_translation/user_interface.rb', line 56

def self.log(text = '')
  max_length = 70
  outside = 4
  puts '#' * max_length if text.blank?
  arr = text.scan(/.{1,#{max_length - outside}}/)
  arr.each do |str|
    fill_length = max_length - str.length - outside
    fill = ' ' * fill_length
    puts '# ' + str + fill + ' #'
  end
end

.merge_hash(merge_from, merge_to) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/lost_in_translation/hash.rb', line 12

def self.merge_hash(merge_from, merge_to)
  return if merge_from.is_a?(String) || merge_to.is_a?(String)
  pp merge_to
  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 self.prepare_paths(path1, path2, post)
  [path1, path2].each do |p|
    prepare_yaml(p, post)
  end
end

.recentObject



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
40
# File 'lib/lost_in_translation/z_recent.rb', line 4

def self.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, true)
  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



25
26
27
28
29
30
31
# File 'lib/lost_in_translation/hash.rb', line 25

def self.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 self.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