Module: LostInTranslation

Included in:
Translate
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.0"

Instance Method Summary collapse

Instance Method Details

#ask_for_languagesObject



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

def ask_for_languages
  system "clear"
  puts "What I18n-yaml files do you want to compare?"
  lang_1 = [(print "Master (e.g. 'en'): "), STDIN.gets.chomp][1]
  lang_2 = [(print "Slave: (e.g. 'de'): "), STDIN.gets.chomp][1]
  [lang_1, lang_2]
end

#ask_for_max_countObject



21
22
23
24
25
26
# File 'lib/lost_in_translation/user_interface.rb', line 21

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_pathsObject



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

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"
  path_1 = [(print "Master: "), STDIN.gets.chomp][1]
  path_2 = [(print "Slave:: "), STDIN.gets.chomp][1]
  [path_1,path_2]
end

#ask_for_permission(lang_1, lang_2, app_name) ⇒ Object



2
3
4
5
6
7
8
9
10
11
# File 'lib/lost_in_translation/user_interface.rb', line 2

def ask_for_permission(lang_1, lang_2, app_name)
  system "clear"
  puts "Comparing Locales"
  puts "Application: #{app_name}" unless app_name.nil?
  puts "Master Locale: #{lang_1}.yml"
  puts "Slave Locale: #{lang_2}.yml"
  puts "Is this ok?[y/n]"
  a = STDIN.gets.chomp
  a == 'y' || a == 'Y'
end

#ask_for_sorting(lang, app_name) ⇒ Object



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

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, lang_1, lang_2) ⇒ Object



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

def ask_for_translation(value, new_structure, lang_1, lang_2)
  system "clear"
  new_s = new_structure.join('---')
  puts "#{new_s} is missing"
  puts "in #{lang_1}: #{value}"
  new_val = [(print lang_2 + ": "), STDIN.gets.chomp][1]
  new_val == '' ? nil : (new_s + '---' + new_val)
end

#clean(root, compared, structure = [], new_array = []) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/lost_in_translation/difference.rb', line 24

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.kind_of? Hash
  end
  new_array
end

#cleanupObject



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
30
31
32
33
34
35
# File 'lib/lost_in_translation/z_cleanup.rb', line 2

def cleanup
  if defined? Rails
    app_name = Rails.application.class.parent_name
    lang_2, lang_1 = Translate.ask_for_languages
    path_1 = "#{Rails.root}/config/locales/#{lang_1}.yml"
    path_2 = "#{Rails.root}/config/locales/#{lang_2}.yml"
  else
    app_name = nil
    path_2, path_1 = Translate.ask_for_paths

    lang_1 = Translate.get_locale(path_1)
    lang_2 = Translate.get_locale(path_2)
  end

  abort('NOPE') unless File.exist?(path_1) && File.exist?(path_2)

  prepare_yamls(path_1, path_2, false)

  first = YAML.load_file(path_1)
  second = YAML.load_file(path_2)

  new_strings_array = Translate.clean(first[lang_1], second[lang_2], [lang_2])

  first = {}
  new_strings_array.each do |string|
    string = string.split('---').map { |x| x == lang_2 ? x = lang_1 : x }.join('---')
    result = Translate.string_to_hash(string)
    Translate.merge_hash(result, first)
  end

  File.open(path_1, 'w') { |file| file.write(first.to_yaml) }

  prepare_yamls(path_1, path_2, false)
end

#define_snippetsObject



37
38
39
40
41
42
43
44
45
46
# File 'lib/lost_in_translation/file_functions.rb', line 37

def define_snippets
  snippets = []
  snippets << ["<<", "a_greater_than_sign"]
  snippets << ["*", "an_asterik_sign"]
  snippets << ["!", "a_bang_sign"]
  snippets << ["%", "a_percentage_sign"]
  snippets << ['a_bang_sign \'', '\'a_bang_sign']
  # snippets << ["&", "this_and_snippet_variable"]
  snippets
end

#diff(root, compared, lang_1, lang_2, structure = [], max_count = 0, new_array = []) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/lost_in_translation/difference.rb', line 2

def diff(root, compared, lang_1, lang_2, 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, lang_1, lang_2)
        new_array << new_val unless new_val.nil?
        count += 1
      end
    end
    diff(next_root, next_compared, lang_1, lang_2, new_structure, new_array) if next_root.kind_of? 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
30
31
# File 'lib/lost_in_translation/z_full_automatic.rb', line 2

def full_automatic
  if defined? Rails
    app_name = Rails.application.class.parent_name
    lang_2, lang_1 = Translate.ask_for_languages
    path_1 = "#{Rails.root}/config/locales/#{lang_1}.yml"
    path_2 = "#{Rails.root}/config/locales/#{lang_2}.yml"
  else
    app_name = nil
    path_2, path_1 = Translate.ask_for_paths

    lang_1 = Translate.get_locale(path_1)
    lang_2 = Translate.get_locale(path_2)
  end

  abort('NOPE') unless File.exist?(path_1) && File.exist?(path_2)

  first = YAML.load_file(path_1)
  second = YAML.load_file(path_2)

  new_strings_array = Translate.clean(first[lang_1], second[lang_2], [lang_2])

  first = {}
  new_strings_array.each do |string|
    string = string.split('---').map { |x| x == lang_2 ? x = lang_1 : x }.join('---')
    result = Translate.string_to_hash(string)
    Translate.merge_hash(result, first)
  end

  File.open(path_1, 'w') { |file| file.write(first.to_yaml) }
end

#get_locale(path) ⇒ Object



20
21
22
# File 'lib/lost_in_translation/difference.rb', line 20

def get_locale(path)
  path.split('/').last.split('.').first unless path.empty?
end

#half_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
30
31
# File 'lib/lost_in_translation/z_half_automatic.rb', line 2

def half_automatic
  if defined? Rails
    app_name = Rails.application.class.parent_name
    lang_2, lang_1 = Translate.ask_for_languages
    path_1 = "#{Rails.root}/config/locales/#{lang_1}.yml"
    path_2 = "#{Rails.root}/config/locales/#{lang_2}.yml"
  else
    app_name = nil
    path_2, path_1 = Translate.ask_for_paths

    lang_1 = Translate.get_locale(path_1)
    lang_2 = Translate.get_locale(path_2)
  end

  abort('NOPE') unless File.exist?(path_1) && File.exist?(path_2)

  first = YAML.load_file(path_1)
  second = YAML.load_file(path_2)

  new_strings_array = Translate.clean(first[lang_1], second[lang_2], [lang_2])

  first = {}
  new_strings_array.each do |string|
    string = string.split('---').map { |x| x == lang_2 ? x = lang_1 : x }.join('---')
    result = Translate.string_to_hash(string)
    Translate.merge_hash(result, first)
  end

  File.open(path_1, 'w') { |file| file.write(first.to_yaml) }
end

#interactiveObject



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
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/lost_in_translation/z_interactive.rb', line 2

def interactive
  if defined? Rails
    app_name = Rails.application.class.parent_name
    lang_1, lang_2 = Translate.ask_for_languages
    path_1 = "#{Rails.root}/config/locales/#{lang_1}.yml"
    path_2 = "#{Rails.root}/config/locales/#{lang_2}.yml"
  else
    app_name = nil
    path_1, path_2 = Translate.ask_for_paths

    lang_1 = Translate.get_locale(path_1)
    lang_2 = Translate.get_locale(path_2)
  end

  abort('NOPE') unless File.exist?(path_1) && File.exist?(path_2)
  abort('Well, in that case, forget it!') unless Translate.ask_for_permission(lang_1,lang_2,app_name)

  prepare_paths(path_1, path_2, true)
  prepare_paths(path_1, path_2, false)

  first = YAML.load_file(path_1)
  second = YAML.load_file(path_2)

  new_strings_array = Translate.diff(first[lang_1], second[lang_2], lang_1, lang_2, [lang_2])

  new_strings_array.each do |string|
    result = Translate.string_to_hash(string)
    Translate.merge_hash(result, second)
  end

  if Translate.ask_for_sorting(lang_1, app_name)
    first = Translate.sort_hash(first)
    File.open(path_1, 'w') { |file| file.write(first.to_yaml(line_width: -1)) }
  end

  second = Translate.sort_hash(second) if Translate.ask_for_sorting(lang_2, app_name)
  File.open(path_2, 'w') { |file| file.write(second.to_yaml(line_width: -1)) }

  prepare_paths(path_1, path_2, true)
end

#merge_hash(merge_from, merge_to) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/lost_in_translation/hash.rb', line 9

def merge_hash(merge_from, merge_to)
  unless merge_from.kind_of?(String) || merge_to.kind_of?(String)
    merged_hash = merge_to
    first_key = merge_from.keys.first
    if merge_to.has_key?(first_key)
      merged_hash[first_key] = merge_hash( merge_from[first_key], merge_to[first_key] )
    else
      merged_hash[first_key] = merge_from[first_key]
    end
    merged_hash
  end
end

#prepare_paths(path_1, path_2, post) ⇒ Object



2
3
4
5
6
# File 'lib/lost_in_translation/file_functions.rb', line 2

def prepare_paths(path_1, path_2, post)
  [path_1, path_2].each do |p|
    prepare_yaml(p, post)
  end
end

#prepare_yaml(path, post) ⇒ Object



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

def prepare_yaml(path, post)
  text = File.read(path)
  snippets = define_snippets

  snippets = snippets.reverse if post
  snippets.each do |snippet|
    snippet = snippet.reverse if post
    text = text.gsub(snippet.first, snippet.second)
  end

  if post
    variables = text.scan(/varyberryterry.*/)
    variables.each do |var|
      new_var = var.gsub(':', '')
      new_var = new_var.gsub('varyberryterry', ': &')
      text = text.gsub(var, new_var)
    end
  else
    variables = text.scan(/: &.*/)
    variables.each do |var|
      new_var = var.gsub(': &', 'varyberryterry')
      new_var += ':'
      text = text.gsub(var, new_var)
    end
  end

  File.open(path, "w") {|file| file.puts text }
end

#recentObject



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
30
31
32
33
34
35
36
37
# File 'lib/lost_in_translation/z_recent.rb', line 2

def recent
  app_name = Rails.application.class.parent_name
  lang_1, lang_2 = Translate.ask_for_languages
  path_1 = "#{Rails.root}/config/locales/#{lang_1}.yml"
  copy_path_1 = "#{Rails.root}/config/locales/#{lang_1}_copy.yml"
  path_2 = "#{Rails.root}/config/locales/#{lang_2}.yml"

  abort('NOPE') unless File.exist?(path_1) && File.exist?(path_2)

  FileUtils.cp(path_1, copy_path_1)
  `git checkout "#{path_1}"`

  prepare_yamls(path_1, copy_path_1, false)

  first = YAML.load_file(path_1)
  first_copy = YAML.load_file(copy_path_1)
  second = YAML.load_file(path_2)
  new_strings_array = Translate.diff(first_copy[lang_1], first[lang_1], lang_1, lang_2, [lang_1])

  new_strings_array.each do |string|
    string = string.split('---').map { |x| x == lang_1 ? x = lang_2 : x }.join('---')
    result = Translate.string_to_hash(string)
    Translate.merge_hash(result, second)
  end

  if Translate.ask_for_sorting(lang_1, app_name)
    first_copy = Translate.sort_hash(first_copy)
    File.open(path_1, 'w') { |file| file.write(first_copy.to_yaml) }
  end

  second = Translate.sort_hash(second) if Translate.ask_for_sorting(lang_2, app_name)
  File.open(path_2, 'w') { |file| file.write(second.to_yaml) }
  prepare_yamls(path_1, copy_path_1, true)

  FileUtils.rm(copy_path_1)
end

#sort_hash(object) ⇒ Object



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

def sort_hash(object)
  return object unless object.is_a?(Hash)
  hash = Hash.new
  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



2
3
4
5
6
7
# File 'lib/lost_in_translation/hash.rb', line 2

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