active_translation 0.7.6 → 0.7.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/app/lib/active_translation/google_translate.rb +36 -18
- data/lib/active_translation/configuration.rb +3 -1
- data/lib/active_translation/translatable.rb +8 -16
- data/lib/active_translation/version.rb +1 -1
- data/lib/generators/active_translation/templates/README +2 -0
- data/lib/generators/active_translation/templates/active_translation.rb +2 -0
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: d7c30d8d827617a8cc52aae8809f5e6a12e703bc06452309fe4ecacefcfecfd7
|
|
4
|
+
data.tar.gz: e69abc9036b9ba190fc7663f5f41bd7d748a8c3d5cd861c184617c7ba6272d6f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 8dbdf785bcb4057b5e7f9498ade435c2b075a7da297d2df7ab50ca788b3a3879fdc2345cb6250410e8554692ed03f72b47b8eff2ca8518c4feeaf681202e9baf
|
|
7
|
+
data.tar.gz: 632bb0bce0180f3ee791875d56cbed286de15ebcd86c08dbf9853b8b34d498bd6079577ebaa40ff64a62c20e2cd629d90af4c13dff04cffc95144a71f0c49ae4
|
|
@@ -5,28 +5,46 @@
|
|
|
5
5
|
module ActiveTranslation
|
|
6
6
|
class GoogleTranslate
|
|
7
7
|
class << self
|
|
8
|
+
def mock_api_responses?
|
|
9
|
+
!ActiveTranslation.configuration.non_mock_environments.map(&:to_s).include? Rails.env
|
|
10
|
+
rescue
|
|
11
|
+
!Rails.env.production?
|
|
12
|
+
end
|
|
13
|
+
|
|
8
14
|
def translate(target_language_code:, text:, source: "en-US", obj: nil)
|
|
9
|
-
|
|
15
|
+
translated_text = if mock_api_responses?
|
|
16
|
+
text
|
|
17
|
+
else
|
|
18
|
+
conn = Faraday.new(url: "https://translation.googleapis.com/") do |faraday|
|
|
19
|
+
faraday.request :json
|
|
20
|
+
faraday.response :json
|
|
21
|
+
faraday.request :authorization, "Bearer", -> { token }
|
|
22
|
+
end
|
|
10
23
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
24
|
+
response =
|
|
25
|
+
conn.post(
|
|
26
|
+
"language/translate/v2",
|
|
27
|
+
{
|
|
28
|
+
q: text,
|
|
29
|
+
target: target_language_code,
|
|
30
|
+
source: source,
|
|
31
|
+
}
|
|
32
|
+
)
|
|
16
33
|
|
|
17
|
-
|
|
18
|
-
conn.post(
|
|
19
|
-
"language/translate/v2",
|
|
20
|
-
{
|
|
21
|
-
q: text,
|
|
22
|
-
target: target_language_code,
|
|
23
|
-
source: source,
|
|
24
|
-
}
|
|
25
|
-
)
|
|
34
|
+
return nil unless response.success?
|
|
26
35
|
|
|
27
|
-
|
|
36
|
+
parse_response(response)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
if text.match?(/\A[A-Z]/)
|
|
40
|
+
translated_text.sub!(/\A./, &:upcase)
|
|
41
|
+
end
|
|
28
42
|
|
|
29
|
-
|
|
43
|
+
if mock_api_responses?
|
|
44
|
+
"[#{target_language_code}] #{translated_text}"
|
|
45
|
+
else
|
|
46
|
+
translated_text
|
|
47
|
+
end
|
|
30
48
|
end
|
|
31
49
|
|
|
32
50
|
private
|
|
@@ -36,7 +54,7 @@ module ActiveTranslation
|
|
|
36
54
|
end
|
|
37
55
|
|
|
38
56
|
def token
|
|
39
|
-
return "fake_access_token" if
|
|
57
|
+
return "fake_access_token" if mock_api_responses?
|
|
40
58
|
|
|
41
59
|
Rails.cache.fetch("google_access_token", expires_in: 55.minutes) do
|
|
42
60
|
google_oauth_credentials = ActiveTranslation.configuration.to_json
|
|
@@ -10,7 +10,8 @@ module ActiveTranslation
|
|
|
10
10
|
:token_uri,
|
|
11
11
|
:auth_provider_x509_cert_url,
|
|
12
12
|
:client_x509_cert_url,
|
|
13
|
-
:universe_domain
|
|
13
|
+
:universe_domain,
|
|
14
|
+
:non_mock_environments
|
|
14
15
|
|
|
15
16
|
def initialize
|
|
16
17
|
@type = nil
|
|
@@ -24,6 +25,7 @@ module ActiveTranslation
|
|
|
24
25
|
@auth_provider_x509_cert_url = "https://www.googleapis.com/oauth2/v1/certs"
|
|
25
26
|
@client_x509_cert_url = nil
|
|
26
27
|
@universe_domain = "googleapis.com"
|
|
28
|
+
@non_mock_environments = [ :production ]
|
|
27
29
|
end
|
|
28
30
|
end
|
|
29
31
|
end
|
|
@@ -31,7 +31,7 @@ module ActiveTranslation
|
|
|
31
31
|
attribute = method_name.to_s.split("_").last
|
|
32
32
|
|
|
33
33
|
# if something like "model.fr_title" is being called (no =)
|
|
34
|
-
if translation_config[:manual_attributes].include? attribute
|
|
34
|
+
if (translation_config[:attributes] + translation_config[:manual_attributes]).include? attribute
|
|
35
35
|
translation = translations.find_by(locale: locale)
|
|
36
36
|
return read_attribute(attribute) unless translation
|
|
37
37
|
|
|
@@ -168,22 +168,14 @@ module ActiveTranslation
|
|
|
168
168
|
|
|
169
169
|
translated_text = cached_translation&.translated_text || ActiveTranslation::GoogleTranslate.translate(target_language_code: locale, text: send(attribute))
|
|
170
170
|
|
|
171
|
-
case translation_config[:cache]
|
|
172
|
-
when TrueClass
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
when String, Symbol
|
|
178
|
-
return unless attribute.to_s == translation_config[:cache].to_s
|
|
179
|
-
|
|
180
|
-
ActiveTranslation::Cache.find_or_create_by(
|
|
181
|
-
checksum: text_checksum(send(attribute)),
|
|
182
|
-
locale:,
|
|
183
|
-
).update(translated_text:,)
|
|
184
|
-
when Array
|
|
185
|
-
return unless translation_config[:cache].map(&:to_s).include? attribute.to_s
|
|
171
|
+
should_cache = case translation_config[:cache]
|
|
172
|
+
when TrueClass then true
|
|
173
|
+
when String, Symbol then attribute.to_s == translation_config[:cache].to_s
|
|
174
|
+
when Array then translation_config[:cache].map(&:to_s).include?(attribute.to_s)
|
|
175
|
+
else false
|
|
176
|
+
end
|
|
186
177
|
|
|
178
|
+
if should_cache
|
|
187
179
|
ActiveTranslation::Cache.find_or_create_by(
|
|
188
180
|
checksum: text_checksum(send(attribute)),
|
|
189
181
|
locale:,
|
|
@@ -21,6 +21,8 @@ Next steps:
|
|
|
21
21
|
config.token_uri = ENV.fetch("GOOGLE_TRANSLATION_TOKEN_URI", "https://oauth2.googleapis.com/token")
|
|
22
22
|
config.auth_provider_x509_cert_url = ENV.fetch("GOOGLE_TRANSLATION_AUTH_PROVIDER_CERT_URL", "https://www.googleapis.com/oauth2/v1/certs")
|
|
23
23
|
config.universe_domain = ENV.fetch("GOOGLE_TRANSLATION_UNIVERSE_DOMAIN", "googleapis.com")
|
|
24
|
+
|
|
25
|
+
config.non_mock_environments = [ :production ]
|
|
24
26
|
end
|
|
25
27
|
|
|
26
28
|
3. Add to your models:
|
|
@@ -11,4 +11,6 @@ ActiveTranslation.configure do |config|
|
|
|
11
11
|
config.token_uri = ENV.fetch("GOOGLE_TRANSLATION_TOKEN_URI", "https://oauth2.googleapis.com/token")
|
|
12
12
|
config.auth_provider_x509_cert_url = ENV.fetch("GOOGLE_TRANSLATION_AUTH_PROVIDER_CERT_URL", "https://www.googleapis.com/oauth2/v1/certs")
|
|
13
13
|
config.universe_domain = ENV.fetch("GOOGLE_TRANSLATION_UNIVERSE_DOMAIN", "googleapis.com")
|
|
14
|
+
|
|
15
|
+
config.non_mock_environments = [ :production ]
|
|
14
16
|
end
|