translated 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cf06c93932fadce4e676acef5b3521c15bb62522a28bcec98fc92f9e94f5819e
4
- data.tar.gz: 8ebdbdf3b47b23b45c023cc23afc25036e52fd2595d156d58799d1d4099965a7
3
+ metadata.gz: d8e76f6a921836b0f1eaab3a9c216876050d70b2376b936c641863c88aa8979c
4
+ data.tar.gz: 6e2cb9f18f06f3d0408812eb42bd57053ccc9b2acc766a2d5f78dd249e503c7f
5
5
  SHA512:
6
- metadata.gz: add8b99d4d294c79d754388c8a5f6ccc07baec15b7fcb9708c69830e63b0dd73a1359d979a7a641bbe578ac726303b8ad5fe5764f642c73e104379569475625d
7
- data.tar.gz: 1cc585e85c0914c07d8c032830898467a94dffd40013763ac4aeba98dbad0c7432031c8331945bc888cac040fc19906474bbf2d068b0b4364c4c255cf64228c9
6
+ metadata.gz: f6bc895fb490fac8fe2c23a9bf7e000dde58330909c36a956d288a0b325583d21cf45dc6c7a897f0752a38e904f33486033e22b4f80efc4c7a7d548d4a79479b
7
+ data.tar.gz: c4912b58c78943cdf884be2d3bc5a993fdd7b04984adfba97eca41e00a999cf27e40e81e799f793966fe7f5ff8000a0780b1ce40a2c67565e3eeed1f8a9f886e
data/README.md CHANGED
@@ -54,5 +54,9 @@ Translated.api_key = 'API KEY from translatedrb.com'
54
54
  # Translated.environments = %w(development production)
55
55
  ```
56
56
 
57
+ ## Example Project
58
+
59
+ Check out an example Rails project with Translated already installed [here](https://github.com/getcomfortly/translated-example).
60
+
57
61
  ## License
58
62
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -4,8 +4,8 @@ module Translated
4
4
  class UpdateRichTranslationsJob < ApplicationJob
5
5
  queue_as :default
6
6
 
7
- def perform(record, attribute_name, from_locale, to_locale)
8
- record.public_send(:"generate_translation_for_#{attribute_name}", from_locale, to_locale)
7
+ def perform(record, attribute_name)
8
+ record.public_send(:"generate_translations_for_#{attribute_name}")
9
9
  end
10
10
  end
11
11
  end
@@ -55,32 +55,30 @@ module Translated
55
55
 
56
56
  def #{name}=(body)
57
57
  self.public_send(:"#{name}_\#{I18n.locale}=", body)
58
- @_needs_rich_translation ||= []
58
+ @_#{name}_translation_changed = true
59
+ body
60
+ end
59
61
 
62
+ def generate_translations_for_#{name}
60
63
  I18n.available_locales.each do |locale|
61
64
  next if locale == I18n.locale
62
65
 
63
- @_needs_rich_translation << [:#{name}, I18n.locale, locale]
66
+ from = I18n.locale
67
+ to = locale
68
+ content = public_send(:"#{name}_\#{from}").body&.to_html
69
+ translation = content.present? ? Translator.new.translate(content, from:, to:) : nil
70
+ update("#{name}_\#{to}" => translation)
64
71
  end
65
-
66
- body
67
- end
68
-
69
- def generate_translation_for_#{name}(from, to)
70
- self.public_send(:"#{name}_\#{to}=", Translator.new.translate(public_send(:"#{name}_\#{from}").body.to_html, from:, to:))
71
- save!
72
72
  end
73
73
 
74
74
  private
75
75
 
76
- def needs_rich_translations?
77
- defined?(:@_needs_rich_translation) && @_needs_rich_translation.present?
76
+ def #{name}_translation_changed?
77
+ defined?(:@_#{name}_translation_changed) && @_#{name}_translation_changed
78
78
  end
79
79
 
80
- def update_rich_translations_later
81
- @_needs_rich_translation.each do |args|
82
- UpdateRichTranslationsJob.perform_later(self, *args)
83
- end
80
+ def generate_translations_for_#{name}_later
81
+ UpdateRichTranslationsJob.perform_later(self, :#{name})
84
82
  end
85
83
  CODE
86
84
 
@@ -88,7 +86,7 @@ module Translated
88
86
  has_rich_text :"#{name}_#{locale}"
89
87
  end
90
88
 
91
- after_commit :update_rich_translations_later, if: :needs_rich_translations?
89
+ after_save :"generate_translations_for_#{name}_later", if: :"#{name}_translation_changed?"
92
90
 
93
91
  scope :"with_rich_text_#{name}", lambda {
94
92
  includes(I18n.available_locales.map do |locale|
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Translated
4
- VERSION = '0.2.0'
4
+ VERSION = '0.3.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: translated
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Trae Robrock
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2024-05-09 00:00:00.000000000 Z
12
+ date: 2024-10-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activejob