translated 0.1.0 → 0.3.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: d8e76f6a921836b0f1eaab3a9c216876050d70b2376b936c641863c88aa8979c
4
+ data.tar.gz: 6e2cb9f18f06f3d0408812eb42bd57053ccc9b2acc766a2d5f78dd249e503c7f
5
5
  SHA512:
6
- metadata.gz: 6864357eddf64bb99813934c9984d35fa77a819c779eb109192ef18c414a70ec53cb9893f555277c102f82427be55464a41cd3ee2a07d0d4004ba3c98eaa06ef
7
- data.tar.gz: a8cf2bc3c1193fa62067ccaf58fc18e469c8d939efa906933ee40502b050b1ddda87320b32625796026e2c3eabd66c41b0c8697682634da179c1b1af7da36af3
6
+ metadata.gz: f6bc895fb490fac8fe2c23a9bf7e000dde58330909c36a956d288a0b325583d21cf45dc6c7a897f0752a38e904f33486033e22b4f80efc4c7a7d548d4a79479b
7
+ data.tar.gz: c4912b58c78943cdf884be2d3bc5a993fdd7b04984adfba97eca41e00a999cf27e40e81e799f793966fe7f5ff8000a0780b1ce40a2c67565e3eeed1f8a9f886e
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,13 +35,28 @@ 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
 
57
+ ## Example Project
58
+
59
+ Check out an example Rails project with Translated already installed [here](https://github.com/getcomfortly/translated-example).
60
+
44
61
  ## License
45
62
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -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)
8
+ record.public_send(:"generate_translations_for_#{attribute_name}")
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,30 @@ module Translated
55
55
 
56
56
  def #{name}=(body)
57
57
  self.public_send(:"#{name}_\#{I18n.locale}=", body)
58
+ @_#{name}_translation_changed = true
59
+ body
60
+ end
58
61
 
62
+ def generate_translations_for_#{name}
59
63
  I18n.available_locales.each do |locale|
60
64
  next if locale == I18n.locale
61
65
 
62
- generate_translation_for_#{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)
63
71
  end
64
-
65
- body
66
72
  end
67
73
 
68
74
  private
69
75
 
70
- def generate_translation_for_#{name}(from, to)
71
- self.public_send(:"#{name}_\#{to}=", Translator.new.translate(public_send(:"#{name}_\#{from}").body.to_html, from:, to:))
76
+ def #{name}_translation_changed?
77
+ defined?(:@_#{name}_translation_changed) && @_#{name}_translation_changed
78
+ end
79
+
80
+ def generate_translations_for_#{name}_later
81
+ UpdateRichTranslationsJob.perform_later(self, :#{name})
72
82
  end
73
83
  CODE
74
84
 
@@ -76,6 +86,8 @@ module Translated
76
86
  has_rich_text :"#{name}_#{locale}"
77
87
  end
78
88
 
89
+ after_save :"generate_translations_for_#{name}_later", if: :"#{name}_translation_changed?"
90
+
79
91
  scope :"with_rich_text_#{name}", lambda {
80
92
  includes(I18n.available_locales.map do |locale|
81
93
  :"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.3.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.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
@@ -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