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.
- checksums.yaml +4 -4
- data/.gitignore +15 -0
- data/Gemfile +19 -0
- data/LICENSE.txt +22 -0
- data/README.md +144 -0
- data/Rakefile +12 -0
- data/app/controllers/translations_controller.rb +42 -0
- data/config/initializers/babbel.rb +2 -0
- data/config/routes.rb +3 -0
- data/inline_translation.gemspec +28 -0
- data/lib/generators/inline_translation/install/install_generator.rb +24 -0
- data/lib/generators/inline_translation/install/templates/add_inline_translations.rb +16 -0
- data/lib/generators/inline_translation/install/templates/create.js.erb +4 -0
- data/lib/generators/inline_translation/install/templates/inline_translation.rb +1 -0
- data/lib/inline_translation.rb +42 -0
- data/lib/inline_translation/concerns/acts_as_translatable.rb +17 -0
- data/lib/inline_translation/concerns/translatable.rb +17 -0
- data/lib/inline_translation/config/routes.rb +3 -0
- data/lib/inline_translation/engine.rb +4 -0
- data/lib/inline_translation/helpers/translations_helper.rb +26 -0
- data/lib/inline_translation/models/translation.rb +9 -0
- data/lib/inline_translation/services/translation_service.rb +36 -0
- data/lib/inline_translation/translators/base.rb +25 -0
- data/lib/inline_translation/translators/bing.rb +21 -0
- data/lib/inline_translation/translators/null.rb +18 -0
- data/lib/inline_translation/version.rb +3 -0
- metadata +50 -48
- data/test/babbel_integration_test.rb +0 -34
- data/test/fixtures/application_controller.rb +0 -3
- data/test/fixtures/rails.rb +0 -36
- data/test/lib/babbel_test.rb +0 -9
- data/test/lib/concerns/acts_as_translatable_test.rb +0 -62
- data/test/lib/concerns/translatable_test.rb +0 -31
- data/test/lib/controllers/translations_controller_test.rb +0 -62
- data/test/lib/generators/babbel_generator_install_test.rb +0 -22
- data/test/lib/helpers/translations_helper_test.rb +0 -61
- data/test/lib/models/translation_test.rb +0 -55
- data/test/lib/services/translation_service_test.rb +0 -69
- data/test/lib/translators/base_test.rb +0 -65
- data/test/lib/translators/bing_test.rb +0 -39
- data/test/lib/translators/null_test.rb +0 -20
- data/test/test_helper.rb +0 -74
- data/test/test_types/controller_test.rb +0 -6
- data/test/test_types/integration_test.rb +0 -2
- data/test/test_types/unit_test.rb +0 -3
@@ -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
|
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.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James Kiesel (gdpelican)
|
8
8
|
autorequire:
|
9
|
-
bindir:
|
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:
|
14
|
+
name: rails
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '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.
|
26
|
+
version: '4.1'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
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.
|
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.
|
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
|
85
|
+
- james@loomio.org
|
72
86
|
executables: []
|
73
87
|
extensions: []
|
74
88
|
extra_rdoc_files: []
|
75
89
|
files:
|
76
|
-
-
|
77
|
-
-
|
78
|
-
-
|
79
|
-
-
|
80
|
-
-
|
81
|
-
-
|
82
|
-
-
|
83
|
-
-
|
84
|
-
-
|
85
|
-
-
|
86
|
-
-
|
87
|
-
-
|
88
|
-
-
|
89
|
-
-
|
90
|
-
-
|
91
|
-
-
|
92
|
-
-
|
93
|
-
-
|
94
|
-
|
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
|
data/test/fixtures/rails.rb
DELETED
@@ -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
|
data/test/lib/babbel_test.rb
DELETED
@@ -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
|