Class: Lokale::Agent

Inherits:
Object
  • Object
show all
Defined in:
lib/lokale/agent.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(proj_path, macros) ⇒ Agent

Returns a new instance of Agent.



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/lokale/agent.rb', line 64

def initialize(proj_path, macros)
  @proj_path = proj_path
  @macros = macros

  @writer = Writer.new
  @exporter = Exporter.new
  @importer = Importer.new

  get_localization_files
  find_all_localization_calls
end

Instance Attribute Details

#lfilesObject (readonly)

Returns the value of attribute lfiles.



62
63
64
# File 'lib/lokale/agent.rb', line 62

def lfiles
  @lfiles
end

#macrosObject (readonly)

Returns the value of attribute macros.



62
63
64
# File 'lib/lokale/agent.rb', line 62

def macros
  @macros
end

#proj_pathObject (readonly)

Returns the value of attribute proj_path.



62
63
64
# File 'lib/lokale/agent.rb', line 62

def proj_path
  @proj_path
end

#sfiles_proceededObject (readonly)

Returns the value of attribute sfiles_proceeded.



62
63
64
# File 'lib/lokale/agent.rb', line 62

def sfiles_proceeded
  @sfiles_proceeded
end

Instance Method Details

#append_new_macro_callsObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/lokale/agent.rb', line 117

def append_new_macro_calls
  @macros.each do |macro|
    next if macro.localization_file.nil?
    file = @lfiles.select { |lf| lf.full_name == macro.localization_file && lf.lang == Config.get.main_lang }.sample
    next if file.nil?

    found_strings = macro.found_strings.keys
    proj_keys = found_strings.map { |ls| ls.key }.to_set
    current_keys = file.keys.to_set
    new_keys = proj_keys - current_keys

    next if new_keys.empty?

    new_lstrings = found_strings.select { |ls| new_keys.include? ls.key }
    @writer.append_new_strings(new_lstrings, file)
  end
end

#copy_baseObject



106
107
108
109
110
111
112
113
114
115
# File 'lib/lokale/agent.rb', line 106

def copy_base
  main_lfiles = @lfiles.group_by { |f| f.lang }[Config.get.main_lang].select { |f| f.strings_file? }
  base_lfiles = @lfiles.group_by { |f| f.lang }[Config.get.base_lang].select { |f| f.strings_file? }

  main_lfiles.each do |en|
    base = base_lfiles.select { |f| f.full_name == en.full_name }.sample
    next if base.nil?
    IO.copy_stream(en.path, base.path)
  end
end

#export_xliffsObject



135
136
137
138
139
140
# File 'lib/lokale/agent.rb', line 135

def export_xliffs
  files = Config.get.base_lang.nil? ? @lfiles : @lfiles.select { |lf| lf.lang != Config.get.base_lang }
  main_lang = Config.get.main_lang
  diffs = Exporter::Diff.find(files, main_lang)
  @exporter.export(diffs) unless diffs.empty?
end

#find_all_localization_callsObject



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/lokale/agent.rb', line 91

def find_all_localization_calls
  @macros.each { |m| m.clear_calls }

  @sfiles_proceeded = 0
  proj_files do |file| 
    next unless file.source_file?        

    file_content = File.read(file)
    @macros.each { |macro| macro.read_from file_content }
    @sfiles_proceeded += 1
  end
end

#get_localization_filesObject



86
87
88
89
# File 'lib/lokale/agent.rb', line 86

def get_localization_files
  return @lfiles unless @lfiles.nil?
  @lfiles = proj_files.map { |file| LFile.try_to_read(file) }.compact
end

#proj_filesObject



78
79
80
81
82
83
84
# File 'lib/lokale/agent.rb', line 78

def proj_files 
  if block_given?
    Dir.glob("#{@proj_path}/**/**") { |f| yield f }
  else
    Dir.glob("#{@proj_path}/**/**")
  end
end

#try_to_importObject



142
143
144
# File 'lib/lokale/agent.rb', line 142

def try_to_import
  @importer.import_strings(self, @writer)
end