inline_translation 0.1.0 → 0.1.1

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.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +15 -0
  3. data/Gemfile +19 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +144 -0
  6. data/Rakefile +12 -0
  7. data/app/controllers/translations_controller.rb +42 -0
  8. data/config/initializers/babbel.rb +2 -0
  9. data/config/routes.rb +3 -0
  10. data/inline_translation.gemspec +28 -0
  11. data/lib/generators/inline_translation/install/install_generator.rb +24 -0
  12. data/lib/generators/inline_translation/install/templates/add_inline_translations.rb +16 -0
  13. data/lib/generators/inline_translation/install/templates/create.js.erb +4 -0
  14. data/lib/generators/inline_translation/install/templates/inline_translation.rb +1 -0
  15. data/lib/inline_translation.rb +42 -0
  16. data/lib/inline_translation/concerns/acts_as_translatable.rb +17 -0
  17. data/lib/inline_translation/concerns/translatable.rb +17 -0
  18. data/lib/inline_translation/config/routes.rb +3 -0
  19. data/lib/inline_translation/engine.rb +4 -0
  20. data/lib/inline_translation/helpers/translations_helper.rb +26 -0
  21. data/lib/inline_translation/models/translation.rb +9 -0
  22. data/lib/inline_translation/services/translation_service.rb +36 -0
  23. data/lib/inline_translation/translators/base.rb +25 -0
  24. data/lib/inline_translation/translators/bing.rb +21 -0
  25. data/lib/inline_translation/translators/null.rb +18 -0
  26. data/lib/inline_translation/version.rb +3 -0
  27. metadata +50 -48
  28. data/test/babbel_integration_test.rb +0 -34
  29. data/test/fixtures/application_controller.rb +0 -3
  30. data/test/fixtures/rails.rb +0 -36
  31. data/test/lib/babbel_test.rb +0 -9
  32. data/test/lib/concerns/acts_as_translatable_test.rb +0 -62
  33. data/test/lib/concerns/translatable_test.rb +0 -31
  34. data/test/lib/controllers/translations_controller_test.rb +0 -62
  35. data/test/lib/generators/babbel_generator_install_test.rb +0 -22
  36. data/test/lib/helpers/translations_helper_test.rb +0 -61
  37. data/test/lib/models/translation_test.rb +0 -55
  38. data/test/lib/services/translation_service_test.rb +0 -69
  39. data/test/lib/translators/base_test.rb +0 -65
  40. data/test/lib/translators/bing_test.rb +0 -39
  41. data/test/lib/translators/null_test.rb +0 -20
  42. data/test/test_helper.rb +0 -74
  43. data/test/test_types/controller_test.rb +0 -6
  44. data/test/test_types/integration_test.rb +0 -2
  45. data/test/test_types/unit_test.rb +0 -3
@@ -0,0 +1,3 @@
1
+ Rails.application.routes.draw do
2
+ resources :translations, only: :create
3
+ end
@@ -0,0 +1,4 @@
1
+ module InlineTranslation
2
+ class Engine < ::Rails::Engine
3
+ end
4
+ end
@@ -0,0 +1,26 @@
1
+ require 'action_view/helpers'
2
+
3
+ module InlineTranslation
4
+ module Helpers
5
+ module TranslationsHelper
6
+ include ActionView::Helpers
7
+
8
+ def translate_link_for(translatable, to: I18n.locale, text: "Translate")
9
+ link_to text, path_for(translatable, to), method: :post, remote: true if translatable.language != to
10
+ end
11
+
12
+ def translated_element_for(translatable, field, element: :span, to: I18n.locale)
13
+ content_tag element, '', class: "#{field}-translated to-#{to} inline-translation-translated"
14
+ end
15
+
16
+ private
17
+
18
+ def path_for(translatable, to)
19
+ translations_path translatable_id: translatable.id,
20
+ translatable_type: translatable.class.to_s,
21
+ to: to,
22
+ action: :create
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,9 @@
1
+ module InlineTranslation
2
+ module Models
3
+ class Translation < ActiveRecord::Base
4
+ belongs_to :translatable, polymorphic: true
5
+ scope :to_language, ->(language) { where language: language }
6
+ validates_presence_of :translatable, :language, :field, :translation
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,36 @@
1
+ module InlineTranslation
2
+ module Services
3
+ class TranslationService
4
+ attr_reader :translator
5
+
6
+ def initialize(translator_class = InlineTranslation.translator)
7
+ raise InvalidTranslatorError.new unless translator_class.ready?
8
+ @translator = translator_class.new
9
+ end
10
+
11
+ def translate(translatable, to: I18n.locale)
12
+ translate!(translatable, to: to) rescue false
13
+ end
14
+
15
+ def translate!(translatable, to: I18n.locale)
16
+ translatable.class.translatable_fields.map { |field| translate_field(translatable, field, to: to) }
17
+ translatable.save
18
+ end
19
+
20
+ def translate_field(translatable, field, to: I18n.locale)
21
+ translatable.translations.build(
22
+ field: field,
23
+ language: to,
24
+ translation: @translator.translate(translatable.send(field), from: translatable.language_field, to: to)
25
+ ) if @translator.can_translate?(translatable, field, to)
26
+ end
27
+
28
+ end
29
+
30
+ class InvalidTranslatorError < StandardError
31
+ def to_s
32
+ "Unable to instantiate translator: Please ensure that the appropriate ENV variables are set"
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,25 @@
1
+ module InlineTranslation
2
+ module Translators
3
+ class Base
4
+ attr_reader :translator
5
+
6
+ def self.ready?
7
+ false
8
+ end
9
+
10
+ def can_translate?(translatable, field, to)
11
+ self.class.ready? &&
12
+ to.present? &&
13
+ translatable.respond_to?(field) &&
14
+ translatable.language_field.present? &&
15
+ translatable.language_field.to_s != to.to_s &&
16
+ translatable.translations.where(field: field, language: to).empty?
17
+ end
18
+
19
+ def translate(text, from: nil, to: I18n.locale)
20
+ raise NotImplementedError.new
21
+ end
22
+
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,21 @@
1
+ require 'bing_translator'
2
+
3
+ module InlineTranslation
4
+ module Translators
5
+ class Bing < Base
6
+
7
+ def self.ready?
8
+ ENV['BING_TRANSLATOR_APP_ID'] && ENV['BING_TRANSLATOR_SECRET'] && true
9
+ end
10
+
11
+ def initialize
12
+ @translator = ::BingTranslator.new ENV['BING_TRANSLATOR_APP_ID'], ENV['BING_TRANSLATOR_SECRET']
13
+ end
14
+
15
+ def translate(text, from: nil, to: I18n.locale)
16
+ @translator.translate text, from: from, to: to
17
+ end
18
+
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,18 @@
1
+ module InlineTranslation
2
+ module Translators
3
+ class Null < Base
4
+
5
+ def self.ready?
6
+ true
7
+ end
8
+
9
+ def initialize
10
+ end
11
+
12
+ def translate(text, from: nil, to: I18n.locale)
13
+ nil
14
+ end
15
+
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ module InlineTranslation
2
+ VERSION = "0.1.1"
3
+ end
metadata CHANGED
@@ -1,43 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: inline_translation
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Kiesel (gdpelican)
8
8
  autorequire:
9
- bindir: bin
9
+ bindir: exe
10
10
  cert_chain: []
11
11
  date: 2015-08-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: bing_translator
14
+ name: rails
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '4.4'
19
+ version: '4.1'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '4.4'
26
+ version: '4.1'
27
27
  - !ruby/object:Gem::Dependency
28
- name: rails
28
+ name: bing_translator
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '4.1'
33
+ version: '4.4'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '4.1'
40
+ version: '4.4'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: bundler
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -66,32 +66,53 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '5.7'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '5.7'
69
83
  description: Sets up a framework for allowing inline translation of database content
70
84
  email:
71
- - james.kiesel@gmail.com
85
+ - james@loomio.org
72
86
  executables: []
73
87
  extensions: []
74
88
  extra_rdoc_files: []
75
89
  files:
76
- - test/babbel_integration_test.rb
77
- - test/fixtures/application_controller.rb
78
- - test/fixtures/rails.rb
79
- - test/lib/babbel_test.rb
80
- - test/lib/concerns/acts_as_translatable_test.rb
81
- - test/lib/concerns/translatable_test.rb
82
- - test/lib/controllers/translations_controller_test.rb
83
- - test/lib/generators/babbel_generator_install_test.rb
84
- - test/lib/helpers/translations_helper_test.rb
85
- - test/lib/models/translation_test.rb
86
- - test/lib/services/translation_service_test.rb
87
- - test/lib/translators/base_test.rb
88
- - test/lib/translators/bing_test.rb
89
- - test/lib/translators/null_test.rb
90
- - test/test_helper.rb
91
- - test/test_types/controller_test.rb
92
- - test/test_types/integration_test.rb
93
- - test/test_types/unit_test.rb
94
- homepage: http://www.github.com/gdpelican/InlineTranslation
90
+ - ".gitignore"
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - app/controllers/translations_controller.rb
96
+ - config/initializers/babbel.rb
97
+ - config/routes.rb
98
+ - inline_translation.gemspec
99
+ - lib/generators/inline_translation/install/install_generator.rb
100
+ - lib/generators/inline_translation/install/templates/add_inline_translations.rb
101
+ - lib/generators/inline_translation/install/templates/create.js.erb
102
+ - lib/generators/inline_translation/install/templates/inline_translation.rb
103
+ - lib/inline_translation.rb
104
+ - lib/inline_translation/concerns/acts_as_translatable.rb
105
+ - lib/inline_translation/concerns/translatable.rb
106
+ - lib/inline_translation/config/routes.rb
107
+ - lib/inline_translation/engine.rb
108
+ - lib/inline_translation/helpers/translations_helper.rb
109
+ - lib/inline_translation/models/translation.rb
110
+ - lib/inline_translation/services/translation_service.rb
111
+ - lib/inline_translation/translators/base.rb
112
+ - lib/inline_translation/translators/bing.rb
113
+ - lib/inline_translation/translators/null.rb
114
+ - lib/inline_translation/version.rb
115
+ homepage: http://www.github.com/gdpelican/inline_translation
95
116
  licenses:
96
117
  - MIT
97
118
  metadata: {}
@@ -100,7 +121,6 @@ rdoc_options: []
100
121
  require_paths:
101
122
  - app
102
123
  - lib
103
- - test
104
124
  required_ruby_version: !ruby/object:Gem::Requirement
105
125
  requirements:
106
126
  - - ">="
@@ -117,22 +137,4 @@ rubygems_version: 2.4.6
117
137
  signing_key:
118
138
  specification_version: 4
119
139
  summary: Store on-the-fly translations using Bing (or others!)
120
- test_files:
121
- - test/babbel_integration_test.rb
122
- - test/fixtures/application_controller.rb
123
- - test/fixtures/rails.rb
124
- - test/lib/babbel_test.rb
125
- - test/lib/concerns/acts_as_translatable_test.rb
126
- - test/lib/concerns/translatable_test.rb
127
- - test/lib/controllers/translations_controller_test.rb
128
- - test/lib/generators/babbel_generator_install_test.rb
129
- - test/lib/helpers/translations_helper_test.rb
130
- - test/lib/models/translation_test.rb
131
- - test/lib/services/translation_service_test.rb
132
- - test/lib/translators/base_test.rb
133
- - test/lib/translators/bing_test.rb
134
- - test/lib/translators/null_test.rb
135
- - test/test_helper.rb
136
- - test/test_types/controller_test.rb
137
- - test/test_types/integration_test.rb
138
- - test/test_types/unit_test.rb
140
+ test_files: []
@@ -1,34 +0,0 @@
1
- require 'test_helper'
2
- class InlineTranslationIntegrationTest < IntegrationTest
3
- describe InlineTranslation do
4
- setup_model :integration_model
5
-
6
- let(:model) { IntegrationModel.create! column1: "column one", column2: "column2", language: :en }
7
-
8
- setup do
9
- @controller ||= InlineTranslation::Controllers::TranslationsController.new
10
- IntegrationModel.acts_as_translatable on: [:column1, :column2]
11
- InlineTranslation::Translators::Null.stubs(:ready?).returns(true)
12
- InlineTranslation::Translators::Null.any_instance.stubs(:translate).returns("this is a translation", "this is another translation")
13
- end
14
-
15
- describe "creating translations" do
16
- it "can create translations" do
17
- post :create, translatable_type: "IntegrationModel", translatable_id: model.id, to: :fr, format: :json
18
-
19
- created = Translation.where(translatable_type: "IntegrationModel")
20
-
21
- assert_equal created.where(translatable_id: model.id).size, 2
22
- assert_equal created.where(language: :fr).size, 2
23
- assert_equal created.where(translatable_type: "IntegrationModel").size, 2
24
- assert_equal created.where(translation: "this is a translation").size, 1
25
- assert_equal created.where(translation: "this is another translation").size, 1
26
-
27
-
28
- before_count = Translation.count
29
- post :create, translatable_type: "IntegrationModel", translatable_id: model.id, to: :fr, format: :json
30
- assert_equal Translation.count, before_count
31
- end
32
- end
33
- end
34
- end
@@ -1,3 +0,0 @@
1
- class ApplicationController < ActionController::Base
2
- define_method :_routes, ->{}
3
- end
@@ -1,36 +0,0 @@
1
- module Rails
2
- def self.application
3
- OpenStruct.new routes: routes,
4
- env_config: {}
5
- end
6
-
7
- def self.root
8
- Dir['../../../']
9
- end
10
-
11
- def self.env
12
- OpenStruct.new to_s: "test",
13
- development?: false,
14
- test?: true,
15
- production?: false
16
- end
17
-
18
- def self.backtrace_cleaner
19
- ActiveSupport::BacktraceCleaner.new
20
- end
21
-
22
- class Engine
23
- end
24
-
25
- private
26
-
27
- def self.routes
28
- @routes ||= ActionDispatch::Routing::RouteSet.new.tap do |routes|
29
- routes.draw { resources :translations }
30
- end
31
- end
32
-
33
- def self.draw_routes
34
-
35
- end
36
- end
@@ -1,9 +0,0 @@
1
- require 'test_helper'
2
-
3
- class InlineTranslationTest < UnitTest
4
- describe "InlineTranslation version" do
5
- it "return the correct version" do
6
- InlineTranslation::VERSION.must_equal '0.0.1'
7
- end
8
- end
9
- end
@@ -1,62 +0,0 @@
1
- require 'test_helper'
2
-
3
- class ActsAsTranslatableTest < UnitTest
4
- describe InlineTranslation::Concerns::ActsAsTranslatable do
5
-
6
- before do
7
- setup_model :concern_model
8
- ConcernModel.define_singleton_method(:find_alt) { |id| "found #{id}!" }
9
- end
10
-
11
- it "includes Translatable" do
12
- ConcernModel.class_eval "acts_as_translatable on: [:column1, :column2]"
13
- assert ConcernModel.included_modules.include?(InlineTranslation::Concerns::Translatable)
14
- end
15
-
16
- it "defines a translatable_fields class method" do
17
- ConcernModel.class_eval "acts_as_translatable on: [:column1, :column2]"
18
- assert_equal ConcernModel.translatable_fields, [:column1, :column2]
19
- end
20
-
21
- it "defines a single translatable_field correctly" do
22
- ConcernModel.class_eval "acts_as_translatable on: :column1"
23
- assert_equal ConcernModel.translatable_fields, [:column1]
24
- end
25
-
26
- it "defines a custom get_instance class method" do
27
- ConcernModel.class_eval "acts_as_translatable on: [:column1, :column2], load_via: :find_alt"
28
- assert_equal ConcernModel.get_instance(42), ConcernModel.find_alt(42)
29
- end
30
-
31
- it "defines a get_instance class method as :find by default" do
32
- ConcernModel.class_eval "acts_as_translatable on: [:column1, :column2]"
33
- model = ConcernModel.create
34
- assert_equal ConcernModel.get_instance(model.id), ConcernModel.find(model.id)
35
- end
36
-
37
- it "defines an id_field method as :id by default" do
38
- ConcernModel.class_eval "acts_as_translatable on: [:column1, :column2]"
39
- model = ConcernModel.new id: 42
40
- assert_equal model.id_field, model.id
41
- end
42
-
43
- it "defines a custom id_field method" do
44
- ConcernModel.class_eval "acts_as_translatable on: [:column1, :column2], id_field: :id_alt"
45
- model = ConcernModel.new id_alt: 42
46
- assert_equal model.id_field, model.id_alt
47
- end
48
-
49
- it "defines a language_field method as :language by default" do
50
- ConcernModel.class_eval "acts_as_translatable on: [:column1, :column2]"
51
- model = ConcernModel.new language: :en
52
- assert_equal model.language_field, model.language
53
- end
54
-
55
- it "defines a custom language_field method" do
56
- ConcernModel.class_eval "acts_as_translatable on: [:column1, :column2], language_field: :language_alt"
57
- model = ConcernModel.new language_alt: :en
58
- assert_equal model.language_field, model.language_alt
59
- end
60
- end
61
-
62
- end