active_translation 0.7.7 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c5060f0fe015b2284bcb3099e6a9287a5dac0bbc8f0cb8f8172c0d068dcfb714
4
- data.tar.gz: 25abebd6fff9da4101bf2aca097085b9314c0b217b3de1547b597f2f641e95c9
3
+ metadata.gz: d7c30d8d827617a8cc52aae8809f5e6a12e703bc06452309fe4ecacefcfecfd7
4
+ data.tar.gz: e69abc9036b9ba190fc7663f5f41bd7d748a8c3d5cd861c184617c7ba6272d6f
5
5
  SHA512:
6
- metadata.gz: be37b5420e08aa497dc213784b4101eb09de9fd645fe14c974dbd0659453251242a55e90ffba89afd6194067edfdf3c3a344294b20a63be7fda7a0deb1ad18b4
7
- data.tar.gz: e9afd718cec5a0a67035c8e6656579e57993caf67ff6e8b39254e6f9b3301c31c894ffeb41e16375b8dd30554cd30063f8d74f37ad200747feecdb3308ba8618
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
- return "[#{target_language_code}] #{text}" if Rails.env.test?
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
- conn = Faraday.new(url: "https://translation.googleapis.com/") do |faraday|
12
- faraday.request :json
13
- faraday.response :json
14
- faraday.request :authorization, "Bearer", -> { token }
15
- end
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
- response =
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
- return nil unless response.success?
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
- parse_response(response)
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 Rails.env.test?
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
 
@@ -1,3 +1,3 @@
1
1
  module ActiveTranslation
2
- VERSION = "0.7.7"
2
+ VERSION = "0.7.8"
3
3
  end
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_translation
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.7
4
+ version: 0.7.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Talentronic