Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for AWS Translate #560

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,17 @@ $ i18n-tasks translate-missing --backend=openai
$ i18n-tasks translate-missing --from=en es fr
```

### AWS Translate missing keys

Translate missing values with AWS ([more below on configuration](#aws-translation-config)).

```console
$ i18n-tasks translate-missing --backend=aws

# accepts from and locales options:
$ i18n-tasks translate-missing --from=en es fr
```

### Find usages

See where the keys are used with `i18n-tasks find`:
Expand Down Expand Up @@ -505,6 +516,26 @@ or via environment variable:
OPENAI_API_KEY=<OpenAI API key>
OPENAI_MODEL=<optional>
```
<a name="aws-translation-config"></a>
### AWS Translate

`i18n-tasks translate-missing` requires AWS Credentials with access to AWS::Translate::TranslateText.

```yaml
# config/i18n-tasks.yml
translation:
aws_region: <AWS Region>
aws_access_key_id: <AWS Access Key ID>
aws_secret_access_key: <AWS Secret Access Key>
```

or via environment variable:

```bash
AWS_REGION=<AWS Region>
AWS_ACCESS_KEY_ID=<AWS Access Key ID>
AWS_SECRET_ACCESS_KEY=<AWS Secret Access Key>
```

## Interactive console

Expand Down
6 changes: 6 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ en:
Set OpenAI API key via OPENAI_API_KEY environment variable or translation.openai_api_key
in config/i18n-tasks.yml. Get the key at https://openai.com/.
no_results: OpenAI returned no results.
aws_translate:
errors:
no_api_key: >-
Set up valid credentails with access to AWS Translate via AWS_REGION, AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY environment variables
or translation.aws_region, translation.aws_access_key_id, aws_secret_access_key in config/i18n-tasks.yml.
no_results: AWS returned no results.
remove_unused:
confirm:
one: "%{count} translation will be removed from %{locales}."
Expand Down
1 change: 1 addition & 0 deletions i18n-tasks.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,5 @@
s.add_development_dependency 'deepl-rb', '>= 2.1.0'
s.add_development_dependency 'easy_translate', '>= 0.5.1' # Google Translate
s.add_development_dependency 'yandex-translator', '>= 0.3.3'
s.add_development_dependency 'aws-sdk-translate', '>= 1.63.0' # aws translate

Check failure on line 63 in i18n-tasks.gemspec

View workflow job for this annotation

GitHub Actions / lint

[Correctable] Gemspec/OrderedDependencies: Dependencies should be sorted in an alphabetical order within their section of the gemspec. Dependency aws-sdk-translate should appear before yandex-translator.
end
3 changes: 3 additions & 0 deletions lib/i18n/tasks/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@

# translation config
# @return [Hash{String => String,Hash,Array}]
def translation_config

Check failure on line 62 in lib/i18n/tasks/configuration.rb

View workflow job for this annotation

GitHub Actions / lint

Metrics/AbcSize: Assignment Branch Condition size for translation_config is too high. [<12, 34, 12> 38/30]

Check failure on line 62 in lib/i18n/tasks/configuration.rb

View workflow job for this annotation

GitHub Actions / lint

Metrics/CyclomaticComplexity: Cyclomatic complexity for translation_config is too high. [13/12]

Check failure on line 62 in lib/i18n/tasks/configuration.rb

View workflow job for this annotation

GitHub Actions / lint

Metrics/PerceivedComplexity: Perceived complexity for translation_config is too high. [13/12]
@config_sections[:translation] ||= begin
conf = (config[:translation] || {}).with_indifferent_access
conf[:google_translate_api_key] = ENV['GOOGLE_TRANSLATE_API_KEY'] if ENV.key?('GOOGLE_TRANSLATE_API_KEY')
Expand All @@ -69,6 +69,9 @@
conf[:openai_api_key] = ENV['OPENAI_API_KEY'] if ENV.key?('OPENAI_API_KEY')
conf[:openai_model] = ENV['OPENAI_MODEL'] if ENV.key?('OPENAI_MODEL')
conf[:yandex_api_key] = ENV['YANDEX_API_KEY'] if ENV.key?('YANDEX_API_KEY')
conf[:aws_region] = ENV['AWS_REGION'] if ENV.key?('AWS_REGION')
conf[:aws_access_key_id] = ENV['AWS_ACCESS_KEY_ID'] if ENV.key?('AWS_ACCESS_KEY_ID')
conf[:aws_secret_access_key] = ENV['AWS_SECRET_ACCESS_KEY'] if ENV.key?('AWS_SECRET_ACCESS_KEY')
conf
end
end
Expand Down
3 changes: 3 additions & 0 deletions lib/i18n/tasks/translation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require 'i18n/tasks/translators/google_translator'
require 'i18n/tasks/translators/openai_translator'
require 'i18n/tasks/translators/yandex_translator'
require 'i18n/tasks/translators/aws_translator'

module I18n::Tasks
module Translation
Expand All @@ -21,6 +22,8 @@ def translate_forest(forest, from:, backend: :google)
Translators::OpenAiTranslator.new(self).translate_forest(forest, from)
when :yandex
Translators::YandexTranslator.new(self).translate_forest(forest, from)
when :aws
Translators::AwsTranslator.new(self).translate_forest(forest, from)
else
fail CommandError, "invalid backend: #{backend}"
end
Expand Down
64 changes: 64 additions & 0 deletions lib/i18n/tasks/translators/aws_translator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# frozen_string_literal: true

require 'i18n/tasks/translators/base_translator'

module I18n::Tasks::Translators
class AwsTranslator < BaseTranslator
def initialize(*)
begin
require 'aws-sdk-translate'
rescue LoadError
raise ::I18n::Tasks::CommandError, "Add gem 'aws-sdk-translate' to your Gemfile to use this command"
end
super
end

protected

def translate_values(list, **options)
list.map do |str|

Check failure on line 19 in lib/i18n/tasks/translators/aws_translator.rb

View workflow job for this annotation

GitHub Actions / lint

[Correctable] Layout/TrailingWhitespace: Trailing whitespace detected.
aws_translate_client.translate_text(
options.merge( text: str )

Check failure on line 21 in lib/i18n/tasks/translators/aws_translator.rb

View workflow job for this annotation

GitHub Actions / lint

[Correctable] Layout/SpaceInsideParens: Space inside parentheses detected.

Check failure on line 21 in lib/i18n/tasks/translators/aws_translator.rb

View workflow job for this annotation

GitHub Actions / lint

[Correctable] Layout/SpaceInsideParens: Space inside parentheses detected.
).translated_text
end
end

def options_for_translate_values(from:, to:, **options)
options.merge(
source_language_code: from,
target_language_code: to,

Check failure on line 29 in lib/i18n/tasks/translators/aws_translator.rb

View workflow job for this annotation

GitHub Actions / lint

[Correctable] Style/TrailingCommaInArguments: Avoid comma after the last parameter of a method call.
)
end

def options_for_html
{}
end

def options_for_plain
{}
end

def no_results_error_message
I18n.t('i18n_tasks.aws_translate.errors.no_results')
end

private

def aws_translate_client
@aws_translate_client ||= Aws::Translate::Client.new(region: region, credentials: credentials)
end

def region
@region ||= @i18n_tasks.translation_config[:aws_region]
end

def credentials
@credentials ||= begin
aws_access_key_id = @i18n_tasks.translation_config[:aws_access_key_id]
aws_secret_access_key = @i18n_tasks.translation_config[:aws_secret_access_key]
fail ::I18n::Tasks::CommandError, I18n.t('i18n_tasks.aws_translate.errors.no_api_key') if aws_access_key_id.blank? || aws_secret_access_key.blank?

Check failure on line 59 in lib/i18n/tasks/translators/aws_translator.rb

View workflow job for this annotation

GitHub Actions / lint

[Correctable] Layout/EmptyLineAfterGuardClause: Add empty line after guard clause.

Check failure on line 59 in lib/i18n/tasks/translators/aws_translator.rb

View workflow job for this annotation

GitHub Actions / lint

[Correctable] Style/IfUnlessModifier: Modifier form of if makes the line too long.
Aws::Credentials.new(aws_access_key_id, aws_secret_access_key)
end

Check warning on line 61 in lib/i18n/tasks/translators/aws_translator.rb

View workflow job for this annotation

GitHub Actions / lint

[Correctable] Layout/BeginEndAlignment: end at 61, 23 is not aligned with @credentials ||= begin at 56, 6.
end
end
end
4 changes: 4 additions & 0 deletions templates/config/i18n-tasks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ search:
# # Variables (starting with %%{ and ending with }) must not be changed under any circumstance.
# #
# # Keep in mind the context of all the strings for a more accurate translation.
# # AWS
# # aws_region: "us-east-1"
# # aws_access_key_id: "XXXXXXXX"
# # aws_secret_access_key: "XXXXXXXX"

## Do not consider these keys missing:
# ignore_missing:
Expand Down
Loading