Skip to content

Commit

Permalink
Allow passing of properties to the phraseapp API that are allowed
Browse files Browse the repository at this point in the history
Not all properties are included, like enabling branching, since those might
interfere with the underlying behavior of what phraseapp_updater is trying
to do.

Secondly, properties that are already passed and set through other names are
not allowed. For example, `file-format` gets transformed into `main_format`.
  • Loading branch information
gordoncl committed Sep 20, 2024
1 parent edb725a commit e50979a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
35 changes: 34 additions & 1 deletion bin/phraseapp_updater
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,49 @@ class PhraseAppUpdaterCLI < Thor
method_option :parent_commit, type: :string, required: true, desc: 'git commit hash of initial locales'
method_option :remove_orphans, type: :boolean, default: true, desc: 'Remove keys not in the uploaded default locale'

# Options that mirror the PhraseApp API (https://developers.phrase.com/api/#post-/projects)
method_option :autotranslate_check_new_locales, type: :boolean, desc: 'Requires autotranslate_enabled to be true'
method_option :autotranslate_check_new_translation_keys, type: :boolean, desc: 'Requires autotranslate_enabled to be true'
method_option :autotranslate_check_new_uploads, type: :boolean, desc: 'Requires autotranslate_enabled to be true'
method_option :autotranslate_enabled, type: :boolean, desc: 'Autopilot, requires machine_translation_enabled.'
method_option :autotranslate_mark_as_unverified, type: :boolean, desc: 'Requires autotranslate_enabled to be true'
method_option :autotranslate_use_machine_translation, type: :boolean, desc: 'Requires autotranslate_enabled to be true'
method_option :autotranslate_use_translation_memory, type: :boolean, desc: 'Requires autotranslate_enabled to be true'
method_option :enable_all_data_type_translation_keys_for_translators, type: :boolean, desc: 'Otherwise, translators are not allowed to edit translations other than strings'
method_option :enable_icu_message_format, type: :boolean, desc: 'We can validate and highlight your ICU messages.'
method_option :machine_translation_enabled, type: :boolean, desc: 'Enable machine translation support in the project. Required for Pre-Translation'
method_option :point_of_contact, type: :string, desc: 'User ID of the point of contact for the project.'
method_option :protect_master_branch, type: :boolean, desc: 'Protect the master branch in project where branching is enabled'
method_option :workflow, type: :string, desc: 'Review Workflow. "simple" / "review".'
method_option :zero_plural_form_enabled, type: :boolean, desc: 'Displays the input fields for the \'ZERO\' plural form for every key as well although only some languages require the \'ZERO\' explicitly.'

def setup(locales_path)
validate_readable_path!('locales', locales_path)

handle_errors do
phraseapp_opts = options.slice(
:autotranslate_check_new_locales,
:autotranslate_check_new_translation_keys,
:autotranslate_check_new_uploads,
:autotranslate_enabled,
:autotranslate_mark_as_unverified,
:autotranslate_use_machine_translation,
:autotranslate_use_translation_memory,
:enable_all_data_type_translation_keys_for_translators,
:enable_icu_message_format,
:machine_translation_enabled,
:point_of_contact,
:protect_master_branch,
:workflow,
:zero_plural_form_enabled)

updater, project_id = PhraseAppUpdater.for_new_project(
options[:phraseapp_api_key],
options[:phraseapp_project_name],
options[:file_format],
options[:parent_commit],
verbose: options[:verbose])
verbose: options[:verbose],
**phraseapp_opts)

updater.upload_directory(locales_path, remove_orphans: options[:remove_orphans])

Expand Down
4 changes: 2 additions & 2 deletions lib/phraseapp_updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
class PhraseAppUpdater
using IndexBy

def self.for_new_project(phraseapp_api_key, phraseapp_project_name, file_format, parent_commit, verbose: false)
def self.for_new_project(phraseapp_api_key, phraseapp_project_name, file_format, parent_commit, verbose: false, **phraseapp_opts)
api = PhraseAppAPI.new(phraseapp_api_key, nil, LocaleFile.class_for_file_format(file_format))
project_id = api.create_project(phraseapp_project_name, parent_commit)
project_id = api.create_project(phraseapp_project_name, parent_commit, **phraseapp_opts)
return self.new(phraseapp_api_key, project_id, file_format, verbose: verbose), project_id
end

Expand Down
11 changes: 8 additions & 3 deletions lib/phraseapp_updater/phraseapp_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,15 @@ def initialize(api_key, project_id, locale_file_class)
@locale_file_class = locale_file_class
end

def create_project(name, parent_commit)
# @param [Hash] opts Options to be passed to the {https://developers.phrase.com/api/#post-/projects PhraseApp API}
def create_project(name, parent_commit, **opts)
params = Phrase::ProjectCreateParameters.new(
name: name,
main_format: @locale_file_class.phraseapp_type)
# Merges name and main_format into opts to prevent overriding these properties
opts.merge(
name: name,
main_format: @locale_file_class.phraseapp_type
)
)

project = phraseapp_request(Phrase::ProjectsApi, :project_create, params)

Expand Down

0 comments on commit e50979a

Please sign in to comment.