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, translations) ⇒ Swift

Returns a new instance of Swift.



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

def initialize(translation_keys, translations)
  @translation_keys = translation_keys
  @translations = translations
end

Instance Method Details

#build_translationsObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ios_polyglot_cli/serializers/sources/sources_serializer_swift.rb', line 56

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(".") 
    translation = @translations[key_name]
    if components && key_name && translation
      root_enum.insert(components, key_name, translation)
    end
  end

  return root_enum
end

#renderObject



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

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

#save(sources_path) ⇒ Object



72
73
74
75
76
# File 'lib/ios_polyglot_cli/serializers/sources/sources_serializer_swift.rb', line 72

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

#templateObject



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
# File 'lib/ios_polyglot_cli/serializers/sources/sources_serializer_swift.rb', line 20

def template()
<<-TEMPLATE
import Foundation

// swiftlint:disable all
<%= @root_enum.serialized() %>
public struct Strings: Hashable, ExpressibleByStringLiteral, StringsProtocol, Sendable {
    public let rawValue: String

    public init(rawValue: String) {
self.rawValue = rawValue
    }

    public init(stringLiteral rawValue: String) {
self.rawValue = rawValue
    }
}

public protocol StringsProtocol: RawRepresentable {
    var localized: String { get }
    func localized(with args: CVarArg...) -> String
}

public extension StringsProtocol where RawValue == String {
    var localized: String {
return self.rawValue.localized
    }

    func localized(with args: CVarArg...) -> String {
return String(format: self.rawValue.localized, arguments: args)
    }
}
// swiftlint:enable all
TEMPLATE
end