Class: Fastlane::Actions::IosDownloadStringsFilesFromGlotpressAction

Inherits:
Action
  • Object
show all
Defined in:
lib/fastlane/plugin/wpmreleasetoolkit/actions/ios/ios_download_strings_files_from_glotpress.rb

Documentation collapse

Class Method Summary collapse

Class Method Details

.authorsObject



107
108
109
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/ios/ios_download_strings_files_from_glotpress.rb', line 107

def self.authors
  ['Automattic']
end

.available_optionsObject



63
64
65
66
67
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
95
96
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/ios/ios_download_strings_files_from_glotpress.rb', line 63

def self.available_options
  [
    FastlaneCore::ConfigItem.new(key: :project_url,
                                 env_name: 'FL_IOS_DOWNLOAD_STRINGS_FILES_FROM_GLOTPRESS_PROJECT_URL',
                                 description: 'URL to the GlotPress project',
                                 type: String),
    FastlaneCore::ConfigItem.new(key: :locales,
                                 env_name: 'FL_IOS_DOWNLOAD_STRINGS_FILES_FROM_GLOTPRESS_LOCALES',
                                 description: 'The map of locales to download, each entry of the Hash corresponding to a { glotpress-locale-code => lproj-folder-basename } pair',
                                 type: Hash), # TODO: also support an Array of `Locale` POD/struct type when we introduce it later (see #296)
    FastlaneCore::ConfigItem.new(key: :download_dir,
                                 env_name: 'FL_IOS_DOWNLOAD_STRINGS_FILES_FROM_GLOTPRESS_DOWNLOAD_DIR',
                                 description: 'The parent directory containing all the `*.lproj` subdirectories in which the downloaded files will be saved',
                                 type: String),
    FastlaneCore::ConfigItem.new(key: :table_basename,
                                 env_name: 'FL_IOS_DOWNLOAD_STRINGS_FILES_FROM_GLOTPRESS_TABLE_BASENAME',
                                 description: 'The basename to save the `.strings` files under',
                                 type: String,
                                 optional: true,
                                 default_value: 'Localizable'),
    FastlaneCore::ConfigItem.new(key: :filters,
                                 env_name: 'FL_IOS_DOWNLOAD_STRINGS_FILES_FROM_GLOTPRESS_FILTERS',
                                 description: 'The GlotPress filters to use when requesting the translations export',
                                 type: Hash,
                                 optional: true,
                                 default_value: { status: 'current' }),
    FastlaneCore::ConfigItem.new(key: :skip_file_validation,
                                 env_name: 'FL_IOS_DOWNLOAD_STRINGS_FILES_FROM_GLOTPRESS_SKIP_FILE_VALIDATION',
                                 description: 'If true, skips the validation of `.strings` files after download',
                                 type: Fastlane::Boolean,
                                 optional: true,
                                 default_value: false),
  ]
end

.descriptionObject



52
53
54
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/ios/ios_download_strings_files_from_glotpress.rb', line 52

def self.description
  'Downloads the `.strings` files from GlotPress for the various locales'
end

.detailsObject



56
57
58
59
60
61
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/ios/ios_download_strings_files_from_glotpress.rb', line 56

def self.details
  "    Downloads the `.strings` files from GlotPress for the various locales,\n    validates them, and saves them in the relevant `*.lproj` directories for each locale\n  DETAILS\nend\n"

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/ios/ios_download_strings_files_from_glotpress.rb', line 111

def self.is_supported?(platform)
  i[ios mac].include?(platform)
end

.return_typeObject



98
99
100
101
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/ios/ios_download_strings_files_from_glotpress.rb', line 98

def self.return_type
  # Describes what type of data is expected to be returned
  # see RETURN_TYPES in https://github.com/fastlane/fastlane/blob/master/fastlane/lib/fastlane/action.rb
end

.return_valueObject



103
104
105
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/ios/ios_download_strings_files_from_glotpress.rb', line 103

def self.return_value
  # Textual description of what the return value is
end

.run(params) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/ios/ios_download_strings_files_from_glotpress.rb', line 6

def self.run(params)
  # TODO: Once we introduce the `Locale` POD via #296, check if the param is an array of locales and if so convert it to Hash{glotpress=>lproj}
  locales = params[:locales]
  download_dir = params[:download_dir]

  UI.user_error!("The parent directory `#{download_dir}` (which contains all the `*.lproj` subdirectories) must already exist") unless Dir.exist?(download_dir)

  locales.each do |glotpress_locale, lproj_name|
    # Download the export in the proper `.lproj` directory
    UI.message "Downloading translations for '#{lproj_name}' from GlotPress (#{glotpress_locale}) [#{params[:filters]}]..."
    lproj_dir = File.join(download_dir, "#{lproj_name}.lproj")
    destination = File.join(lproj_dir, "#{params[:table_basename]}.strings")
    FileUtils.mkdir_p(lproj_dir)

    Fastlane::Helper::Ios::L10nHelper.download_glotpress_export_file(
      project_url: params[:project_url],
      locale: glotpress_locale,
      filters: params[:filters],
      destination: destination
    )
    # Do a quick check of the downloaded `.strings` file to ensure it looks valid
    validate_strings_file(destination) unless params[:skip_file_validation]
  end
end

.validate_strings_file(destination) ⇒ Object

Validate that a ‘.strings` file downloaded from GlotPress seems valid and does not contain empty translations



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/ios/ios_download_strings_files_from_glotpress.rb', line 32

def self.validate_strings_file(destination)
  return unless File.exist?(destination) # If the file failed to download, don't try to validate an non-existing file. We'd already have a separate error for the download failure anyway.

  translations = Fastlane::Helper::Ios::L10nHelper.read_strings_file_as_hash(path: destination)
  empty_keys = translations.select { |_, value| value.nil? || value.empty? }.keys.sort
  unless empty_keys.empty?
    UI.error(
      "Found empty translations in `#{destination}` for the following keys: #{empty_keys.inspect}.\n" \
        + "This is likely a GlotPress bug, and will lead to copies replaced by empty text in the UI.\n" \
        + 'Please report this to the GlotPress team, and fix the file locally before continuing.'
    )
  end
rescue StandardError => e
  UI.error("Error while validating the file exported from GlotPress (`#{destination}`) - #{e.message.chomp}")
end