active_translation 0.7.9 → 0.7.10

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: 20c120b29981a823b9e6d0e6615de1d8449a62c05fed30457c838c467e809ea2
4
- data.tar.gz: 196f8ad25bca2b0d26519fb93e99a399e96847b50bd86bf8af4b153407951a1a
3
+ metadata.gz: 753b6bfc3922bc72e78ad59a1632dd63082aec905d9e9ac9743be33ffd6baf5f
4
+ data.tar.gz: 49cde87e86c0992a6189b7b574ab542db9de59891f89bc1073469603f4f23e9e
5
5
  SHA512:
6
- metadata.gz: 94a210afa88f3807a79925b16068c378505284ce287c509726bdd8ad3dad15a1ed6fd2fb0365d4d15873d13756b55c6163ab24d7e2f25e3abdb2231fcc38a09d
7
- data.tar.gz: 6e2e94578b455ba0a2d6db6a47bb5bd36f809c30bca8cbcbcf20eab1c9c35bbf522a6c63a5521c6752b073347fc23e8a97ed4ee217625dff62d48fce415abb42
6
+ metadata.gz: 3f6d9d0fd884299a0f579cc4063a9c212b98170cd4dbc89f3735660baa1ea74e8b9b2c2e7d4f114971d86ec4bd7505c09a07c6898c20410f9041a967ca5f63ed
7
+ data.tar.gz: c3a177a2a5f06f69b2eed28208bf1190e283bc318d1e6da61a62648ae5ed083f7f25a5a628a77ca85784efdb5dcb75875cd83949d1f092277a895f351647ba53
data/README.md CHANGED
@@ -354,6 +354,19 @@ You can call `translation_config` on a model or instance to see what you've set
354
354
  => {attributes: [:profile_html], manual_attributes: ["name"], locales: :method_that_returns_locales, unless: nil, if: nil}
355
355
  ```
356
356
 
357
+
358
+ ### Translation Without Objectification
359
+
360
+ You might want to use ActiveTranslation to translate a random string that has no connection to an ActiveRecord model. You can do that with `ActiveTranslation.translate` method.
361
+
362
+ ```ruby
363
+ ActiveTranslation.translate("some string to translate", locale: :fr)
364
+ ```
365
+
366
+ This will return the translated string, and it will cache the result so future calls to translate won't hit the API. You can pass `cache: false` to not cache the result, and also to not return an already cached result.
367
+
368
+ ```ruby
369
+
357
370
  #### Disclaimer
358
371
 
359
372
  ActiveTranslation doesn't check the accuracy of translations in any way. It assumes that the response from Google is always perfect. If you are translating sensitive content where accuracy is critical in a legal or existential sense, you must handle translation auditing separately.
@@ -4,5 +4,24 @@ module ActiveTranslation
4
4
 
5
5
  validates :locale, presence: true
6
6
  validates :checksum, presence: true
7
+
8
+ class << self
9
+ def add!(locale:, original_text:, translated_text:)
10
+ find_or_create_by(
11
+ checksum: Digest::MD5.hexdigest(original_text),
12
+ locale:,
13
+ ).update(translated_text:,)
14
+ end
15
+
16
+ def lookup(locale:, text:)
17
+ text = text.to_s
18
+ locale = locale.to_s
19
+
20
+ find_by(
21
+ checksum: Digest::MD5.hexdigest(text),
22
+ locale:,
23
+ )&.translated_text
24
+ end
25
+ end
7
26
  end
8
27
  end
@@ -161,12 +161,12 @@ module ActiveTranslation
161
161
  def translate_attribute(attribute, locale)
162
162
  return nil if send(attribute).nil?
163
163
 
164
- cached_translation = ActiveTranslation::Cache.find_by(
165
- checksum: text_checksum(send(attribute)),
166
- locale: locale,
164
+ cached_translation = Cache.lookup(
165
+ locale:,
166
+ text: send(attribute),
167
167
  )
168
168
 
169
- translated_text = cached_translation&.translated_text || ActiveTranslation::GoogleTranslate.translate(target_language_code: locale, text: send(attribute))
169
+ translated_text = cached_translation || ActiveTranslation::GoogleTranslate.translate(target_language_code: locale, text: send(attribute))
170
170
 
171
171
  should_cache = case translation_config[:cache]
172
172
  when TrueClass then true
@@ -176,10 +176,11 @@ module ActiveTranslation
176
176
  end
177
177
 
178
178
  if should_cache
179
- ActiveTranslation::Cache.find_or_create_by(
180
- checksum: text_checksum(send(attribute)),
179
+ Cache.add!(
181
180
  locale:,
182
- ).update(translated_text:,)
181
+ original_text: send(attribute),
182
+ translated_text:,
183
+ )
183
184
  end
184
185
 
185
186
  translated_text
@@ -1,3 +1,3 @@
1
1
  module ActiveTranslation
2
- VERSION = "0.7.9"
2
+ VERSION = "0.7.10"
3
3
  end
@@ -17,5 +17,21 @@ module ActiveTranslation
17
17
  def configure
18
18
  yield(configuration)
19
19
  end
20
+
21
+ def translate(text:, locale:, cache: true)
22
+ text = text.to_s
23
+ locale = locale.to_s
24
+
25
+ if cache
26
+ cached_translation = Cache.find_by(locale:, checksum: Digest::MD5.hexdigest(text))
27
+ return cached_translation.translated_text if cached_translation
28
+ end
29
+
30
+ translated_text = GoogleTranslate.translate(target_language_code: locale, text:)
31
+
32
+ Cache.add!(locale:, original_text: text, translated_text:) if cache
33
+
34
+ translated_text
35
+ end
20
36
  end
21
37
  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.9
4
+ version: 0.7.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Talentronic