Class: SwiftWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/localio/writers/swift_writer.rb

Class Method Summary collapse

Class Method Details

.write(languages, terms, path, formatter, options) ⇒ Object



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
# File 'lib/localio/writers/swift_writer.rb', line 7

def self.write(languages, terms, path, formatter, options)
  puts 'Writing iOS translations...'
  create_constants = options[:create_constants].nil? ? true : options[:create_constants]

  constant_segments = nil
  languages.keys.each do |lang|
    output_path = File.join(path, "#{lang}.lproj/")

    # We have now to iterate all the terms for the current language, extract them, and store them into a new array

    segments = SegmentsListHolder.new lang
    constant_segments = SegmentsListHolder.new lang
    terms.each do |term|
      key = Formatter.format(term.keyword, formatter, method(:swift_key_formatter))
      translation = term.values[lang]
      segment = Segment.new(key, translation, lang)
      segment.key = nil if term.is_comment?
      segments.segments << segment

      unless term.is_comment?
        constant_key = swift_constant_formatter term.keyword
        constant_value = key
        constant_segment = Segment.new(constant_key, constant_value, lang)
        constant_segments.segments << constant_segment
      end
    end

    TemplateHandler.process_template 'ios_localizable.erb', output_path, 'Localizable.strings', segments
    puts " > #{lang.yellow}"
  end

  if create_constants && !constant_segments.nil?
    TemplateHandler.process_template 'swift_constant_localizable.erb', path, 'LocalizableConstants.swift', constant_segments
    puts ' > ' + 'LocalizableConstants.swift'.yellow
  end
end