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 +4 -4
- data/README.md +13 -0
- data/app/models/active_translation/cache.rb +19 -0
- data/lib/active_translation/translatable.rb +8 -7
- data/lib/active_translation/version.rb +1 -1
- data/lib/active_translation.rb +16 -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: 753b6bfc3922bc72e78ad59a1632dd63082aec905d9e9ac9743be33ffd6baf5f
|
|
4
|
+
data.tar.gz: 49cde87e86c0992a6189b7b574ab542db9de59891f89bc1073469603f4f23e9e
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
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 =
|
|
165
|
-
|
|
166
|
-
|
|
164
|
+
cached_translation = Cache.lookup(
|
|
165
|
+
locale:,
|
|
166
|
+
text: send(attribute),
|
|
167
167
|
)
|
|
168
168
|
|
|
169
|
-
translated_text = cached_translation
|
|
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
|
-
|
|
180
|
-
checksum: text_checksum(send(attribute)),
|
|
179
|
+
Cache.add!(
|
|
181
180
|
locale:,
|
|
182
|
-
|
|
181
|
+
original_text: send(attribute),
|
|
182
|
+
translated_text:,
|
|
183
|
+
)
|
|
183
184
|
end
|
|
184
185
|
|
|
185
186
|
translated_text
|
data/lib/active_translation.rb
CHANGED
|
@@ -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
|