Class: PolyglotIos::Serializer::Source::Swift

Inherits:
Object
  • Object
show all
Includes:
ERB::Util
Defined in:
lib/ios_polyglot_cli/serializers/sources/sources_serializer_swift.rb

Instance Method Summary collapse

Constructor Details

#initialize(translation_keys) ⇒ Swift

Returns a new instance of Swift.



10
11
12
# File 'lib/ios_polyglot_cli/serializers/sources/sources_serializer_swift.rb', line 10

def initialize(translation_keys)
  @translation_keys = translation_keys
end

Instance Method Details

#build_translations ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ios_polyglot_cli/serializers/sources/sources_serializer_swift.rb', line 42

def build_translations()
  root_enum = TranslationEnum.new("Strings")
  
  @translation_keys.each do |translation_key|
    key_name = translation_key.name
    next if key_name.nil?
    components = key_name.split(".")
    root_enum.insert(components, key_name)
  end

  return root_enum
end

#render ⇒ Object



14
15
16
17
# File 'lib/ios_polyglot_cli/serializers/sources/sources_serializer_swift.rb', line 14

def render()
  @root_enum = build_translations()
  ERB.new(template, trim_mode: '-').result(binding)
end

#save(sources_path) ⇒ Object



55
56
57
58
59
# File 'lib/ios_polyglot_cli/serializers/sources/sources_serializer_swift.rb', line 55

def save(sources_path)
  FileUtils.mkdir_p sources_path unless File.exist? sources_path
  output_path = File.join(sources_path, "Strings.swift")
  File.write(output_path, render)
end

#template ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ios_polyglot_cli/serializers/sources/sources_serializer_swift.rb', line 19

def template()
"import Foundation\n\n// swiftlint:disable file_length type_body_length line_length superfluous_disable_command redundant_string_enum_value\n<%= @root_enum.serialized() %>\npublic protocol StringsProtocol: RawRepresentable {\n    var localized: String { get }\n    func localized(with args: CVarArg...) -> String\n}\n\npublic extension StringsProtocol where RawValue == String {\n    var localized: String {\nreturn self.rawValue.localized\n    }\n\n    func localized(with args: CVarArg...) -> String {\nreturn String(format: self.rawValue.localized, arguments: args)\n    }\n}\n"
end