translated 0.1.0 → 0.2.0

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: 29074d491110db08e39419d73a816b72827cbcf43376d6767ca8b51db29ac282
4
- data.tar.gz: 87902d648e5d79b93792174219dcc2b1aa4d5f8e2d8c165964bf9942f70ce87f
3
+ metadata.gz: cf06c93932fadce4e676acef5b3521c15bb62522a28bcec98fc92f9e94f5819e
4
+ data.tar.gz: 8ebdbdf3b47b23b45c023cc23afc25036e52fd2595d156d58799d1d4099965a7
5
5
  SHA512:
6
- metadata.gz: 6864357eddf64bb99813934c9984d35fa77a819c779eb109192ef18c414a70ec53cb9893f555277c102f82427be55464a41cd3ee2a07d0d4004ba3c98eaa06ef
7
- data.tar.gz: a8cf2bc3c1193fa62067ccaf58fc18e469c8d939efa906933ee40502b050b1ddda87320b32625796026e2c3eabd66c41b0c8697682634da179c1b1af7da36af3
6
+ metadata.gz: add8b99d4d294c79d754388c8a5f6ccc07baec15b7fcb9708c69830e63b0dd73a1359d979a7a641bbe578ac726303b8ad5fe5764f642c73e104379569475625d
7
+ data.tar.gz: 1cc585e85c0914c07d8c032830898467a94dffd40013763ac4aeba98dbad0c7432031c8331945bc888cac040fc19906474bbf2d068b0b4364c4c255cf64228c9
data/README.md CHANGED
@@ -1,16 +1,18 @@
1
1
  # Translated
2
- Simple, automatic translations for your Rails app.
2
+ Simple, automatic translations for your Rails app with https://translatedrb.com.
3
3
 
4
4
  ## Usage
5
- Translated can handle both plain text and rich text (using actiontext).
5
+ Translated can handle both plain text and rich text (using Action Text).
6
6
 
7
- These helpers will let you work with the content attribute like you normally would. The return of this attribute will always be in the language specified by I18n.current_locale. So if it's set to :es, then you'll get Spanish. When you set the value it will save that for the current_locale and then create translations for every locale listed in the I18n.available_locales list.
7
+ These helpers will let you work with the content attribute like you normally would. The return value of this attribute will always be in the language specified by `I18n.locale`. So if it's set to `:es`, then you'll get Spanish.
8
+
9
+ When you set the value, it will save that for the current locale and then create translations for every locale listed in the `I18n.available_locales` list.
8
10
 
9
11
  For plain text, simply add this to your model:
10
12
 
11
13
  ```ruby
12
14
  class Message < ApplicationRecord
13
- has_translated_text :content
15
+ has_translated_text_field :content
14
16
  end
15
17
  ```
16
18
 
@@ -22,7 +24,7 @@ class Message < ApplicationRecord
22
24
  end
23
25
  ```
24
26
 
25
- You can use this attribute just like any other actiontext attribute.
27
+ You can use this attribute just like any other Action Text attribute.
26
28
 
27
29
  ## Installation
28
30
  Add this line to your application's Gemfile:
@@ -33,12 +35,23 @@ gem "translated"
33
35
 
34
36
  And then execute:
35
37
  ```bash
36
- $ bundle
38
+ bundle
37
39
  ```
38
40
 
39
41
  Install the migrations:
40
42
  ```bash
41
- $ bin/rails translated:install:migrations
43
+ bin/rails translated:install:migrations
44
+ ```
45
+
46
+ Get your API key from https://translatedrb.com
47
+
48
+ Create an initializer `config/initializers/translated.rb`
49
+ ```ruby
50
+ Translated.api_key = 'API KEY from translatedrb.com'
51
+
52
+ # Optionally, you can configure which environments you would like translated.
53
+ # By default, it will translate development and production
54
+ # Translated.environments = %w(development production)
42
55
  ```
43
56
 
44
57
  ## License
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Translated
4
+ class UpdateRichTranslationsJob < ApplicationJob
5
+ queue_as :default
6
+
7
+ def perform(record, attribute_name, from_locale, to_locale)
8
+ record.public_send(:"generate_translation_for_#{attribute_name}", from_locale, to_locale)
9
+ end
10
+ end
11
+ end
@@ -7,27 +7,27 @@ module Translated
7
7
  class_methods do
8
8
  def has_translated_text_field(name, validates: {}) # rubocop:disable Naming/PredicateName
9
9
  class_eval <<-CODE, __FILE__, __LINE__ + 1 # rubocop:disable Style/DocumentDynamicEvalDefinition
10
- def #{name}
11
- translation = #{name}_translation || build_#{name}_translation
12
- translation&.for_locale(I18n.locale)
13
- end
10
+ def #{name}
11
+ translation = #{name}_translation || build_#{name}_translation
12
+ translation&.for_locale(I18n.locale)
13
+ end
14
14
 
15
- def #{name}?
16
- #{name}.present?
17
- end
15
+ def #{name}?
16
+ #{name}.present?
17
+ end
18
18
 
19
- def #{name}=(content)
20
- translation = self.#{name}_translation || build_#{name}_translation
21
- translation.set_locale(I18n.locale, content)
22
- @_#{name}_translation_changed = true
23
- content
24
- end
19
+ def #{name}=(content)
20
+ translation = self.#{name}_translation || build_#{name}_translation
21
+ translation.set_locale(I18n.locale, content)
22
+ @_#{name}_translation_changed = true
23
+ content
24
+ end
25
25
 
26
- private
26
+ private
27
27
 
28
- def #{name}_translation_changed?
29
- defined?(:@_#{name}_translation_changed) && @_#{name}_translation_changed
30
- end
28
+ def #{name}_translation_changed?
29
+ defined?(:@_#{name}_translation_changed) && @_#{name}_translation_changed
30
+ end
31
31
  CODE
32
32
 
33
33
  has_one :"#{name}_translation",
@@ -55,20 +55,32 @@ module Translated
55
55
 
56
56
  def #{name}=(body)
57
57
  self.public_send(:"#{name}_\#{I18n.locale}=", body)
58
+ @_needs_rich_translation ||= []
58
59
 
59
60
  I18n.available_locales.each do |locale|
60
61
  next if locale == I18n.locale
61
62
 
62
- generate_translation_for_#{name}(I18n.locale, locale)
63
+ @_needs_rich_translation << [:#{name}, I18n.locale, locale]
63
64
  end
64
65
 
65
66
  body
66
67
  end
67
68
 
68
- private
69
-
70
69
  def generate_translation_for_#{name}(from, to)
71
70
  self.public_send(:"#{name}_\#{to}=", Translator.new.translate(public_send(:"#{name}_\#{from}").body.to_html, from:, to:))
71
+ save!
72
+ end
73
+
74
+ private
75
+
76
+ def needs_rich_translations?
77
+ defined?(:@_needs_rich_translation) && @_needs_rich_translation.present?
78
+ end
79
+
80
+ def update_rich_translations_later
81
+ @_needs_rich_translation.each do |args|
82
+ UpdateRichTranslationsJob.perform_later(self, *args)
83
+ end
72
84
  end
73
85
  CODE
74
86
 
@@ -76,6 +88,8 @@ module Translated
76
88
  has_rich_text :"#{name}_#{locale}"
77
89
  end
78
90
 
91
+ after_commit :update_rich_translations_later, if: :needs_rich_translations?
92
+
79
93
  scope :"with_rich_text_#{name}", lambda {
80
94
  includes(I18n.available_locales.map do |locale|
81
95
  :"rich_text_#{name}_#{locale}"
@@ -25,7 +25,7 @@ module Translated
25
25
  self.content ||= {}
26
26
  self.content[locale] = value
27
27
 
28
- @_needs_translations = true
28
+ @_needs_translations = content_changed?
29
29
  value
30
30
  end
31
31
 
@@ -44,7 +44,7 @@ module Translated
44
44
  private
45
45
 
46
46
  def needs_translations?
47
- defined?(:@_needs_translations) && @_needs_translations
47
+ defined?(:@_needs_translations) && @_needs_translations && Translated.environments.include?(Rails.env)
48
48
  end
49
49
 
50
50
  def update_translations_later
@@ -4,16 +4,25 @@ require 'rest-client'
4
4
 
5
5
  module Translated
6
6
  class Translator
7
- API_HOST = ENV.fetch('TRANSLATOR_API_HOST', 'http://localhost:3000')
7
+ API_HOST = ENV.fetch('TRANSLATED_API_HOST', 'https://translatedrb.com')
8
8
 
9
9
  def translate(text, from:, to:)
10
10
  response = RestClient.post(
11
11
  "#{API_HOST}/translate",
12
12
  { text: text, from: from, to: to }.to_json,
13
- content_type: :json,
14
- accept: :json
13
+ accept: :json,
14
+ authorization: "Token token=\"#{api_key}\"",
15
+ content_type: :json
15
16
  )
16
17
  JSON.parse(response.body)['translated_text']
17
18
  end
19
+
20
+ private
21
+
22
+ def api_key
23
+ fail 'Translated API key is required' if Translated.api_key.blank?
24
+
25
+ Translated.api_key
26
+ end
18
27
  end
19
28
  end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Translated
4
+ class Configuration
5
+ attr_accessor :api_key, :environments
6
+
7
+ def initialize
8
+ @environments = %w(development production)
9
+ end
10
+ end
11
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Translated
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.0'
5
5
  end
data/lib/translated.rb CHANGED
@@ -2,6 +2,17 @@
2
2
 
3
3
  require 'translated/version'
4
4
  require 'translated/engine'
5
+ require 'translated/configuration'
5
6
 
6
7
  module Translated
8
+ @config = Configuration.new
9
+
10
+ class << self
11
+ extend Forwardable
12
+
13
+ attr_reader :config
14
+
15
+ def_delegators :@config, :api_key, :api_key=
16
+ def_delegators :@config, :environments, :environments=
17
+ end
7
18
  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.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Trae Robrock
@@ -92,6 +92,7 @@ files:
92
92
  - MIT-LICENSE
93
93
  - README.md
94
94
  - Rakefile
95
+ - app/jobs/translated/update_rich_translations_job.rb
95
96
  - app/jobs/translated/update_translations_job.rb
96
97
  - app/models/concerns/translated/translatable.rb
97
98
  - app/models/translated/translated_text_field.rb
@@ -99,6 +100,7 @@ files:
99
100
  - db/migrate/202405031152_create_translated_translated_text_fields.rb
100
101
  - lib/tasks/translated_tasks.rake
101
102
  - lib/translated.rb
103
+ - lib/translated/configuration.rb
102
104
  - lib/translated/engine.rb
103
105
  - lib/translated/version.rb
104
106
  homepage: https://github.com/getcomfortly/translated