Class: Fastlane::Actions::TranslationAction

Inherits:
Action
  • Object
show all
Defined in:
lib/fastlane/plugin/translation/actions/translation_action.rb

Documentation collapse

Class Method Summary collapse

Class Method Details

.authorsObject



120
121
122
# File 'lib/fastlane/plugin/translation/actions/translation_action.rb', line 120

def self.authors
  ["Krzysztof Piatkowski", "Jakob Jensen"]
end

.available_optionsObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/fastlane/plugin/translation/actions/translation_action.rb', line 131

def self.available_options
  [
    FastlaneCore::ConfigItem.new(key: :config_path,
                                 env_name: "FL_TRANSLATION_CONFIG_PATH",
                                 description: "refrence for the config json file, see https://github.com/gimite/google-drive-ruby/blob/master/doc/authorization.md for more info",
                                 optional: false),
    FastlaneCore::ConfigItem.new(key: :doc_id,
                                 env_name: "FL_TRANSLATION_DOC_ID",
                                 description: "id of the google sheet, can be used instead of doc_name after creation",
                                 optional: false),
    FastlaneCore::ConfigItem.new(key: :cvs_output_path,
                                 default_value: FastlaneCore::FastlaneFolder.path + "translation.cvs",
                                 env_name: "FL_TRANSLATION_CVS_OUTPUT_PATH",
                                 description: "The path where the cvs is placed",
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :ios_output_paths,
                                 default_value: nil,
                                 env_name: "FL_TRANSLATION_IOS_OUTPUT_PATH",
                                 is_string: false,
                                 description: "An map from path to a column in the google sheet outputs a localized .strings file",
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :android_output_paths,
                                 default_value: nil,
                                 env_name: "FL_TRANSLATION_ANDROID_OUTPUT_PATH",
                                 is_string: false,
                                 description: "An map from path to a column in the google sheet outputs a strings.xml",
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :swift_struct_path,
                                 default_value: nil,
                                 env_name: "FL_TRANSLATION_SWIFT_STRUCT_PATH",
                                 is_string: false,
                                 description: "Creates a swift struct with all the translations as properties",
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :dotnet_class_path,
                                 default_value: nil,
                                 env_name: "FL_TRANSLATION_DOTNET_CLASS_PATH",
                                 is_string: false,
                                 description: "Creates a .net class with all the translations as properties",
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :key,
                                 default_value: 1,
                                 is_string: false,
                                 env_name: "FL_TRANSLATION_KEY",
                                 description: "index of key column",
                                 optional: true)
  ]
end

.convert_csv_to_android_paths(key, cvs_path, output_paths) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/fastlane/plugin/translation/actions/translation_action.rb', line 51

def self.convert_csv_to_android_paths(key, cvs_path, output_paths)
  output_paths.each do |file_path, index|
    FileUtils.mkdir_p(File.dirname(file_path))
    UI.message("Writing #{file_path}")
    file = open(file_path, 'w')
    file.write("<resources>\n")
    CSV.foreach(cvs_path) do |row|
      if row[key] && row[key].length > 0
        key_row = row[key]
        file.write("\t<string name=\"#{key_row}\">#{row[index]}</string>\n")
      end
    end
    file.write('</resources>')
    file.close
  end
end

.convert_csv_to_ios_paths(key, cvs_path, output_paths) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/fastlane/plugin/translation/actions/translation_action.rb', line 34

def self.convert_csv_to_ios_paths(key, cvs_path, output_paths)
  output_paths.each do |file_path, index|
    FileUtils.mkdir_p(File.dirname(file_path))
    UI.message("Writing #{file_path}")
    file = open(file_path, 'w')
    CSV.foreach(cvs_path) do |row|
      if row[key] && row[key].length > 0 && !row[index].nil?
        key_row = row[key]
        value_row = row[index].gsub("\"", "\\\"")
        value_row = value_row.gsub("\n", "\\n")
        file.write("\"#{key_row}\" = \"#{value_row}\";\n")
      end
    end
    file.close
  end
end

.create_dotnet_class(key, cvs_path, dotnet_path) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/fastlane/plugin/translation/actions/translation_action.rb', line 96

def self.create_dotnet_class(key, cvs_path, dotnet_path)
  UI.message("Writing dotnet struct #{dotnet_path}")
  FileUtils.mkdir_p(File.dirname(dotnet_path))
  file = open(dotnet_path, 'w')
  file.write("using Foundation;\nstatic class Translations {\n")

  CSV.foreach(cvs_path) do |row|
    if row[key] && row[key].length > 0
      key_row = row[key]
      file.write("\tpublic static string #{key_row} { get { return NSBundle.MainBundle.LocalizedString (\"#{key_row}\", null); } }\n")
    end
  end
  file.write("}")
  file.close
end

.create_swift_struct(key, master_index, cvs_path, swift_path) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/fastlane/plugin/translation/actions/translation_action.rb', line 68

def self.create_swift_struct(key, master_index, cvs_path, swift_path)
  UI.message("Writing swift struct #{swift_path}")
  FileUtils.mkdir_p(File.dirname(swift_path))
  file = open(swift_path, 'w')
  file.write("import Foundation\n")
  file.write("// swiftlint:disable identifier_name line_length file_length type_body_length superfluous_disable_command\n")
  file.write("struct Translations {\n")

  CSV.foreach(cvs_path) do |row|
    if row[key] && row[key].length > 0 && row.compact.length > 1
      key_row = row[key]
      master = row[master_index]
      parameters = master.scan(/\%\d+/)
      if parameters.count > 0
        args_str = parameters.map { |e| e.sub('%', 'p') + ': String' }.join(', _ ')
        file.write("\tstatic func #{key_row}(_ #{args_str}) -> String {")
        file.write(" return NSLocalizedString(\"#{key_row}\", comment: \"\")")
        parameters.each { |e| file.write(".replacingOccurrences(of: \"#{e}\", with: #{e.sub('%', 'p')})") }
        file.write(" }\n")
      else
        file.write("\tstatic let #{key_row} = NSLocalizedString(\"#{key_row}\", comment: \"\")\n")
      end
    end
  end
  file.write("}\n")
  file.close
end

.descriptionObject



116
117
118
# File 'lib/fastlane/plugin/translation/actions/translation_action.rb', line 116

def self.description
  "Output translations from Google sheet into templates."
end

.detailsObject



127
128
129
# File 'lib/fastlane/plugin/translation/actions/translation_action.rb', line 127

def self.details
  nil
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


179
180
181
# File 'lib/fastlane/plugin/translation/actions/translation_action.rb', line 179

def self.is_supported?(platform)
  true
end

.return_valueObject



124
125
# File 'lib/fastlane/plugin/translation/actions/translation_action.rb', line 124

def self.return_value
end

.run(params) ⇒ 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
# File 'lib/fastlane/plugin/translation/actions/translation_action.rb', line 7

def self.run(params)
  UI.message "Logging into google using #{params[:config_path]}"
  session = GoogleDrive::Session.from_config(params[:config_path])

  UI.message "Downloading CVS to #{params[:cvs_output_path]}"
  file = session.file_by_id(params[:doc_id])
  file.export_as_file(params[:cvs_output_path], "text/csv")

  if params[:ios_output_paths]
    self.convert_csv_to_ios_paths(params[:key], params[:cvs_output_path], params[:ios_output_paths])
  end

  if params[:android_output_paths]
    convert_csv_to_android_paths(params[:key], params[:cvs_output_path], params[:android_output_paths])
  end

  if params[:swift_struct_path]
    self.create_swift_struct(params[:key], params[:ios_output_paths].values.first, params[:cvs_output_path], params[:swift_struct_path])
  end

  if params[:dotnet_class_path]
    self.create_dotnet_class(params[:key], params[:cvs_output_path], params[:dotnet_class_path])
  end

  File.delete(params[:cvs_output_path])
end