class Fastlane::Actions::EnumeratedTranslationsAction

Public Class Methods

authors() click to toggle source
# File lib/fastlane/plugin/enumerated_translations/actions/enumerated_translations_action.rb, line 58
def self.authors
  ["Mads Bøgeskov"]
end
available_options() click to toggle source
# File lib/fastlane/plugin/enumerated_translations/actions/enumerated_translations_action.rb, line 71
def self.available_options
  [
    FastlaneCore::ConfigItem.new(key: :strings,
                            env_name: "ENUMERATED_TRANSLATIONS_INPUT",
                         description: "The .strings file used as the basis for the output",
                            optional: false,
                                type: String),

    FastlaneCore::ConfigItem.new(key: :result,
                            env_name: "ENUMERATED_TRANSLATIONS_RESULT",
                         description: "The .swift containing the Localisations enum",
                            optional: false,
                                type: String)
  ]
end
description() click to toggle source
# File lib/fastlane/plugin/enumerated_translations/actions/enumerated_translations_action.rb, line 54
def self.description
  "Converts a strings file to an enum, to make it more safe to access translations."
end
details() click to toggle source
# File lib/fastlane/plugin/enumerated_translations/actions/enumerated_translations_action.rb, line 66
def self.details
  # Optional:
  "Converts a strings file to an enum, to make it more safe to access translations."
end
is_supported?(platform) click to toggle source
# File lib/fastlane/plugin/enumerated_translations/actions/enumerated_translations_action.rb, line 87
def self.is_supported?(platform)
  true
end
return_value() click to toggle source
# File lib/fastlane/plugin/enumerated_translations/actions/enumerated_translations_action.rb, line 62
def self.return_value
  # If your method provides a return value, you can describe here what it does
end
run(params) click to toggle source
# File lib/fastlane/plugin/enumerated_translations/actions/enumerated_translations_action.rb, line 14
def self.run(params)
  file = File.new(params[:strings], 'r')
  keys = []
  values = []
  while (line = file.gets)
    withoutSemicolon = line.chop().split(' = ')
    keys.push(withoutSemicolon[0].chop().tr('"', ''))
    values.push(withoutSemicolon[1].tr('";', ''))
  end

  enumFile =  "// This file is autogenerated\n"
  enumFile =  "// Recreate it by running fastlane\n"
  enumFile += "// - Mads Bøgeskov\n"
  enumFile += "\n"

  enumFile += "import Foundation"
  enumFile += "\n\n"

  enumFile += "enum Localisations: String {\n"

  for i in 0 ... keys.length
    key = keys[i]
    value = values[i]

    enumFile += "    case #{key.sub(' ', '_').underscore}\n"
  end
  enumFile += "\n"
  enumFile += "    var toString: String {\n"
  enumFile += "        switch self {\n"
  for i in 0 ... keys.length
    enumFile += "        case .#{keys[i].sub(' ', '_').underscore}: return NSLocalizedString(\"#{keys[i]}\", comment: \"\")\n"
  end
  enumFile += "        }\n"
  enumFile += "    }\n"

  enumFile += "}\n"

  File.open(params[:result], 'w') { |file| file.write(enumFile) }
end