Skip to content

Commit

Permalink
Add support for AWS Translate
Browse files Browse the repository at this point in the history
  • Loading branch information
brianmock committed Apr 23, 2024
1 parent 3bfe80c commit 0afbb42
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 0 deletions.
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 @@ Gem::Specification.new do |s|
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 @@ -69,6 +69,9 @@ def translation_config
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

0 comments on commit 0afbb42

Please sign in to comment.