Sha256: d7030cfb0cf2dae008701cb4b37f3efe15925cc771f0280852267475cbcc1110

Contents?: true

Size: 1.68 KB

Versions: 1

Compression:

Stored size: 1.68 KB

Contents

require_relative 'swift_helpers/translation_case'
require_relative 'swift_helpers/translation_enum'

module PolyglotIos
  module Serializer
    module Source
      class Swift
        include ERB::Util
        
        def initialize(translation_keys)
          @translation_keys = translation_keys
        end
        
        def render()
          @root_enum = build_translations()
          ERB.new(template, nil, '-').result(binding)
        end

        def template()
<<-TEMPLATE
import Foundation

// swiftlint:disable file_length type_body_length line_length superfluous_disable_command redundant_string_enum_value
<%= @root_enum.serialized() %>
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)
    }
}
TEMPLATE
        end

        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

        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

      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ios_polyglot_cli-2.2.2 lib/ios_polyglot_cli/serializers/sources/sources_serializer_swift.rb