glossync 0.1.1 → 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 +4 -4
- data/.env.example +1 -0
- data/.gitignore +2 -0
- data/Gemfile.lock +9 -3
- data/glossync.gemspec +1 -0
- data/lib/glossync/config.rb +34 -0
- data/lib/glossync/exceptions.rb +7 -0
- data/lib/glossync/missing_translation_handler.rb +39 -0
- data/lib/glossync/version.rb +1 -1
- data/lib/glossync.rb +23 -16
- metadata +19 -2
- data/lib/glossync/tl_adapter.rb +0 -36
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c9f37796efedf9aee1579ceb60b4c88ac241ad40744a00e10a7563103270c950
|
4
|
+
data.tar.gz: b0ac328d38119d563e8f5193679c147423714640613dc5d5bf041dd26533a613
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ceaf7fbae10d61519576b76f81be6b3bd6c10ef4385a67d0157b239b7fe567366ffca5f2c6458aec1e95cf1bba2340d4d7532a22c73f051703a10d1a060f9a53
|
7
|
+
data.tar.gz: bd725afe42d70c64e61d34021bb960de1956bd53cdb193db5864bc5e542c4637c30b2bc19b1a496d5648e95f288c26e11f8f63b15145a7eb0d0f7e92ebed6587
|
data/.env.example
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
export GOOGLE_TRANSLATE_API_KEY=abcdeFGH1JKLMN0p
|
data/.gitignore
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
glossync (0.
|
4
|
+
glossync (0.2.0)
|
5
|
+
easy_translate (~> 0.5)
|
5
6
|
i18n (~> 1.1)
|
6
7
|
|
7
8
|
GEM
|
@@ -9,7 +10,10 @@ GEM
|
|
9
10
|
specs:
|
10
11
|
concurrent-ruby (1.1.4)
|
11
12
|
diff-lcs (1.3)
|
12
|
-
|
13
|
+
easy_translate (0.5.1)
|
14
|
+
thread
|
15
|
+
thread_safe
|
16
|
+
i18n (1.5.1)
|
13
17
|
concurrent-ruby (~> 1.0)
|
14
18
|
rake (10.5.0)
|
15
19
|
rspec (3.8.0)
|
@@ -25,15 +29,17 @@ GEM
|
|
25
29
|
diff-lcs (>= 1.2.0, < 2.0)
|
26
30
|
rspec-support (~> 3.8.0)
|
27
31
|
rspec-support (3.8.0)
|
32
|
+
thread (0.2.2)
|
33
|
+
thread_safe (0.3.6)
|
28
34
|
|
29
35
|
PLATFORMS
|
30
36
|
ruby
|
31
37
|
|
32
38
|
DEPENDENCIES
|
33
39
|
bundler (~> 1.17)
|
40
|
+
glossync!
|
34
41
|
rake (~> 10.0)
|
35
42
|
rspec (~> 3.0)
|
36
|
-
glossync!
|
37
43
|
|
38
44
|
BUNDLED WITH
|
39
45
|
1.17.1
|
data/glossync.gemspec
CHANGED
@@ -0,0 +1,34 @@
|
|
1
|
+
module Glossync
|
2
|
+
DEFAULT_CONFIG = {
|
3
|
+
reload_backend: false,
|
4
|
+
write_fallback_result: false,
|
5
|
+
missing_translation_handler: MissingTranslationHandler::Default.new,
|
6
|
+
base_locale: :en,
|
7
|
+
}.freeze
|
8
|
+
|
9
|
+
DEFAULT_CONFIG.each do |k, v|
|
10
|
+
(case v
|
11
|
+
when true, false
|
12
|
+
[
|
13
|
+
[:"dont_#{k}", -> { @options[k] = false }],
|
14
|
+
[:"#{k}!", -> { @options[k] = true } ],
|
15
|
+
[:"#{k}?", -> { @options[k] } ],
|
16
|
+
]
|
17
|
+
else
|
18
|
+
[]
|
19
|
+
end + [
|
20
|
+
[:"#{k}=", ->(v) { @options[k] = v }],
|
21
|
+
[:"#{k}", -> { @options[k]} ]
|
22
|
+
]).each { |meth, body| Glossync.define_singleton_method(meth, &body) }
|
23
|
+
end
|
24
|
+
|
25
|
+
class << self
|
26
|
+
attr_reader :options
|
27
|
+
|
28
|
+
def config(&block)
|
29
|
+
@options ||= {}
|
30
|
+
block.call(Glossync)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module Glossync
|
2
|
+
module MissingTranslationHandler
|
3
|
+
class Default
|
4
|
+
def handle(error, _who, _field, _locale)
|
5
|
+
error.message
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
class Raiser
|
10
|
+
def handle(error, _, _, _)
|
11
|
+
raise error
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class GoogleTranslate
|
16
|
+
def initialize(key)
|
17
|
+
@api_key = key
|
18
|
+
@cache = {}
|
19
|
+
end
|
20
|
+
|
21
|
+
def translate(text, to)
|
22
|
+
EasyTranslate.translate(
|
23
|
+
text,
|
24
|
+
from: Glossync.options[:base_locale],
|
25
|
+
to: to,
|
26
|
+
key: @api_key
|
27
|
+
)
|
28
|
+
end
|
29
|
+
|
30
|
+
def handle(_, who, field, locale)
|
31
|
+
key = who.tl_key(field)
|
32
|
+
|
33
|
+
@cache[key] = translate(who[field], locale) unless @cache.key?(key)
|
34
|
+
|
35
|
+
@cache[key]
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/lib/glossync/version.rb
CHANGED
data/lib/glossync.rb
CHANGED
@@ -1,15 +1,20 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'i18n'
|
4
|
-
require '
|
5
|
-
require 'glossync/
|
4
|
+
require 'easy_translate'
|
5
|
+
require 'glossync/exceptions'
|
6
|
+
require 'glossync/missing_translation_handler'
|
6
7
|
require 'glossync/railtie' if defined?(Rails)
|
8
|
+
require 'glossync/version'
|
9
|
+
|
10
|
+
require 'glossync/config'
|
7
11
|
|
8
12
|
# Include into ActiveRecords to obtain some nice I18n features
|
9
13
|
module Glossync
|
10
14
|
# methods that match this pattern will be responded to in method_missing()
|
11
15
|
METHOD_PATTERN = /_translated$/.freeze
|
12
16
|
|
17
|
+
#default configuration
|
13
18
|
class << self
|
14
19
|
attr_writer :tl_store
|
15
20
|
|
@@ -18,18 +23,7 @@ module Glossync
|
|
18
23
|
|
19
24
|
Translation
|
20
25
|
rescue NameError
|
21
|
-
raise
|
22
|
-
end
|
23
|
-
|
24
|
-
# Should we call I18n.backend.reload! on each call to tl_update that
|
25
|
-
# modifies data?
|
26
|
-
def reload_backend_on_tl_update?
|
27
|
-
@reload_backend_on_tl_update ||= true
|
28
|
-
end
|
29
|
-
|
30
|
-
# :nodoc:
|
31
|
-
def reload_backend_on_tl_update=(bool)
|
32
|
-
@reload_backend_on_tl_update = !!bool
|
26
|
+
raise(Glossync::NoTranslationStore)
|
33
27
|
end
|
34
28
|
|
35
29
|
# called when a module is included, add methods to the base class
|
@@ -118,7 +112,20 @@ module Glossync
|
|
118
112
|
# @param field the field to translate
|
119
113
|
# @return [String] the translated string
|
120
114
|
def tl(field)
|
121
|
-
|
115
|
+
tl = catch(:exception) do
|
116
|
+
I18n.translate(tl_key(field), throw: true)
|
117
|
+
end
|
118
|
+
|
119
|
+
if tl.is_a?(I18n::MissingTranslation)
|
120
|
+
tlr = Glossync.missing_translation_handler
|
121
|
+
.handle(tl, self, field, I18n.locale)
|
122
|
+
|
123
|
+
tl_update(field, tlr) if Glossync.options[:write_fallback_result]
|
124
|
+
|
125
|
+
tlr
|
126
|
+
else
|
127
|
+
tl
|
128
|
+
end
|
122
129
|
end
|
123
130
|
|
124
131
|
# Update the translation associated with a field and locale
|
@@ -146,7 +153,7 @@ module Glossync
|
|
146
153
|
tls.create(h.merge(value: value))
|
147
154
|
end
|
148
155
|
|
149
|
-
I18n.backend.reload! if Glossync.
|
156
|
+
I18n.backend.reload! if Glossync.options[:reload_backend]
|
150
157
|
|
151
158
|
true
|
152
159
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: glossync
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stone Tickle
|
@@ -52,6 +52,20 @@ dependencies:
|
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: easy_translate
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.5'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.5'
|
55
69
|
- !ruby/object:Gem::Dependency
|
56
70
|
name: i18n
|
57
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -73,6 +87,7 @@ executables: []
|
|
73
87
|
extensions: []
|
74
88
|
extra_rdoc_files: []
|
75
89
|
files:
|
90
|
+
- ".env.example"
|
76
91
|
- ".gitignore"
|
77
92
|
- ".rspec"
|
78
93
|
- ".travis.yml"
|
@@ -85,8 +100,10 @@ files:
|
|
85
100
|
- bin/setup
|
86
101
|
- glossync.gemspec
|
87
102
|
- lib/glossync.rb
|
103
|
+
- lib/glossync/config.rb
|
104
|
+
- lib/glossync/exceptions.rb
|
105
|
+
- lib/glossync/missing_translation_handler.rb
|
88
106
|
- lib/glossync/railtie.rb
|
89
|
-
- lib/glossync/tl_adapter.rb
|
90
107
|
- lib/glossync/version.rb
|
91
108
|
- lib/tasks/setup_model_translations.rake
|
92
109
|
homepage:
|
data/lib/glossync/tl_adapter.rb
DELETED
@@ -1,36 +0,0 @@
|
|
1
|
-
module Glossync
|
2
|
-
class TlAdapter
|
3
|
-
include I18n::Backend::Base
|
4
|
-
|
5
|
-
TlElem = Struct.new(:value) do
|
6
|
-
def update(value:)
|
7
|
-
self.value = value
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
def initialize
|
12
|
-
super
|
13
|
-
|
14
|
-
@db = {}
|
15
|
-
end
|
16
|
-
|
17
|
-
def exists?(locale:, key:)
|
18
|
-
@db.key?(key) && @db[key].key?(locale)
|
19
|
-
end
|
20
|
-
|
21
|
-
def find_by(locale:, key:)
|
22
|
-
return nil unless exists?(locale: locale, key: key)
|
23
|
-
|
24
|
-
@db[key][locale]
|
25
|
-
end
|
26
|
-
|
27
|
-
def create(locale:, key:, value:)
|
28
|
-
@db[key] ||= {}
|
29
|
-
@db[key][locale] = TlElem.new(value)
|
30
|
-
end
|
31
|
-
|
32
|
-
def lookup(locale, key, *)
|
33
|
-
find_by(locale: locale, key: key).value
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|