Module: ImmosquareTranslate::YmlTranslator

Extended by:
SharedMethods
Defined in:
lib/immosquare-translate/yml_translator.rb

Constant Summary

Constants included from SharedMethods

SharedMethods::DOUBLE_QUOTE, SharedMethods::NOTHING, SharedMethods::OPEN_AI_MODELS, SharedMethods::SIMPLE_QUOTE, SharedMethods::SPACE

Class Method Summary collapse

Class Method Details

.translate(file_path, locale_to, options = {}) ⇒ Object



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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/immosquare-translate/yml_translator.rb', line 11

def translate(file_path, locale_to,  options = {})
  begin
    ##=============================================================##
    ## options
    ##=============================================================##
    options = {
      :reset_translations => false
    }.merge(options)
    options[:reset_translations] = false  if ![true, false].include?(options[:reset_translations])


    ##=============================================================##
    ## Load config keys from config_dev.yml
    ##=============================================================##
    raise("Error: openai_api_key not found in config_dev.yml") if ImmosquareTranslate.configuration.openai_api_key.nil?
    raise("Error: File #{file_path} not found")                if !File.exist?(file_path)
    raise("Error: locale is not a locale")                     if !locale_to.is_a?(String) || locale_to.size != 2

    ##============================================================##
    ## We clean the file before translation
    ##============================================================##
    ImmosquareYaml.clean(file_path)

    ##============================================================##
    ## We parse the clean input file
    ##============================================================##
    hash_from = ImmosquareYaml.parse(file_path)
    raise("#{file_path} is not a correct yml translation file") if !hash_from.is_a?(Hash) && hash_from.keys.size > 1

    ##============================================================##
    ## Check if the locale is present in the file
    ##============================================================##
    locale_from = hash_from.keys.first.to_s
    raise("Error: The destination file (#{locale_to}) is the same as the source file (#{locale_from}).")    if locale_from == locale_to
    raise("Error: Expected the source file (#{file_path}) to end with '#{locale_from}.yml' but it didn't.") if !file_path.end_with?("#{locale_from}.yml")


    ##============================================================##
    ## Prepare the output file
    ##============================================================##
    file_basename        = File.basename(file_path)
    file_dirname         = File.dirname(file_path)
    translated_file_path = "#{file_dirname}/#{file_basename.gsub("#{locale_from}.yml", "#{locale_to}.yml")}"

    ##============================================================##
    ## We create a hash with all keys from the source file
    ##============================================================##
    hash_to = {locale_to => hash_from.delete(locale_from)}

    ##============================================================##
    ## We create a array with all keys from the source file
    ##============================================================##
    array_to = translatable_array(hash_to)
    array_to = array_to.map {|k, v| [k, v, nil] }

    ##============================================================##
    ## If we already have a translation file for the language
    ## we get the values in it and put it in our
    ## file... You have to do well with !nil?
    ## to retrieve the values "" and " "...
    ##============================================================##
    if File.exist?(translated_file_path) && options[:reset_translations] == false
      temp_hash = ImmosquareYaml.parse(translated_file_path)
      raise("#{translated_file_path} is not a correct yml translation file") if !temp_hash.is_a?(Hash) && temp_hash.keys.size > 1

      ##============================================================##
      ## t can be nil if the key is not present in the source file
      ##============================================================##
      translatable_array(temp_hash).each do |key, value|
        t    = array_to.find {|k, _v| k == key }
        t[2] = value if !t.nil? && !value.nil?
      end
    end

    ##============================================================##
    ## Here we have to do all the translation logic...
    ## For the moment we use the OPENAI API, but we can imagine
    ## using other translation APIs in the future.
    ##============================================================##
    translated_array = translate_with_open_ai(array_to, locale_from, locale_to)

    ##============================================================##
    ## Then we have to reformat the output yml file
    ##============================================================##
    final_array = translated_array.map do |k, _from, to|
      parsed_to = !to.nil? && to.start_with?("[") && to.end_with?("]") ? JSON.parse(to) : to
      [k, parsed_to]
    end
    final_hash = translatable_hash(final_array)


    ##============================================================##
    ## We write the output file and clean it
    ##============================================================##
    File.write(translated_file_path, ImmosquareYaml.dump(final_hash))
    ImmosquareYaml.clean(translated_file_path)
  rescue StandardError => e
    puts(e.message)
    puts(e.backtrace)
    false
  end
end