Class: Lokale::Importer

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

Defined Under Namespace

Classes: Diff

Instance Method Summary collapse

Instance Method Details

#file_for_diff(diff, all_lfiles) ⇒ Object



398
399
400
# File 'lib/lokale/agent.rb', line 398

def file_for_diff(diff, all_lfiles)
  all_lfiles.select { |lf| lf.full_name == diff.name && lf.lang == diff.lang }.sample
end

#import_strings(agent, writer) ⇒ Object



365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'lib/lokale/agent.rb', line 365

def import_strings(agent, writer)
  xliff_paths = agent.proj_files
    .select { |f| f =~ /\.xliff$/ }
    .delete_if { |f| f =~ /export/ }
    # .select { |f| puts "select #{f}, #{f =~ /\.xliff^/}"; f =~ /\.xliff^/ }
    # .delete_if { |f| puts "delete #{f}, #{f =~ /export/}"; f =~ /export/ }
    
  return if xliff_paths.empty?
  diffs = xliff_paths.flat_map { |p| Diff.from_file(p) }
  diffs.each do |d|
    lf = file_for_diff(d, agent.lfiles)
    next if lf.nil?

    content = lf.content
    strings_to_append = []

    d.lstrings.each do |ls|
      string_regexp = /^\s*?"#{ls.key}"\s*?=\s*?"(.*?)";/
      if content =~ string_regexp
        if $1 != ls.str
          puts "#{lf.lang}/#{lf.full_name} update \"#{ls.key.blue}\": \"#{$1}\" -> \"#{ls.str.blue}\""
          content.sub! string_regexp, "\"#{ls.key}\" = \"#{ls.str}\";"
        end
      else
        strings_to_append << ls
      end
    end

    lf.write content        
    writer.append_new_strings(strings_to_append, lf) unless strings_to_append.empty?
  end
end