tr8n_core 4.0.4 → 4.0.5

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 (52) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +1 -1
  3. data/README.md +44 -6
  4. data/lib/tr8n/api_client.rb +109 -0
  5. data/lib/tr8n/application.rb +14 -96
  6. data/lib/tr8n/base.rb +7 -2
  7. data/lib/tr8n/cache.rb +2 -1
  8. data/lib/tr8n/cache_adapters/cdb.rb +2 -1
  9. data/lib/tr8n/cache_adapters/file.rb +2 -1
  10. data/lib/tr8n/cache_adapters/memcache.rb +2 -1
  11. data/lib/tr8n/cache_adapters/redis.rb +2 -1
  12. data/lib/tr8n/component.rb +2 -1
  13. data/lib/tr8n/config.rb +13 -3
  14. data/lib/tr8n/decorators/base.rb +2 -1
  15. data/lib/tr8n/decorators/default.rb +2 -1
  16. data/lib/tr8n/decorators/html.rb +2 -1
  17. data/lib/tr8n/exception.rb +2 -1
  18. data/lib/tr8n/language.rb +9 -3
  19. data/lib/tr8n/language_case.rb +5 -1
  20. data/lib/tr8n/language_case_rule.rb +2 -1
  21. data/lib/tr8n/language_context.rb +4 -1
  22. data/lib/tr8n/language_context_rule.rb +2 -1
  23. data/lib/tr8n/logger.rb +3 -2
  24. data/lib/tr8n/rules_engine/evaluator.rb +2 -1
  25. data/lib/tr8n/rules_engine/parser.rb +2 -1
  26. data/lib/tr8n/session.rb +21 -7
  27. data/lib/tr8n/source.rb +3 -2
  28. data/lib/tr8n/tokens/data.rb +319 -325
  29. data/lib/tr8n/tokens/data_tokenizer.rb +6 -1
  30. data/lib/tr8n/tokens/decoration_tokenizer.rb +15 -5
  31. data/lib/tr8n/tokens/hidden.rb +2 -1
  32. data/lib/tr8n/tokens/method.rb +4 -3
  33. data/lib/tr8n/tokens/transform.rb +22 -10
  34. data/lib/tr8n/translation.rb +6 -8
  35. data/lib/tr8n/translation_key.rb +11 -9
  36. data/lib/tr8n/translator.rb +2 -1
  37. data/lib/tr8n/utils.rb +6 -1
  38. data/lib/tr8n_core.rb +1 -1
  39. data/lib/tr8n_core/ext/array.rb +1 -1
  40. data/lib/tr8n_core/ext/date.rb +1 -1
  41. data/lib/tr8n_core/ext/fixnum.rb +1 -1
  42. data/lib/tr8n_core/ext/hash.rb +1 -1
  43. data/lib/tr8n_core/ext/string.rb +1 -1
  44. data/lib/tr8n_core/ext/time.rb +1 -1
  45. data/lib/tr8n_core/generators/cache/base.rb +1 -1
  46. data/lib/tr8n_core/generators/cache/cdb.rb +1 -1
  47. data/lib/tr8n_core/generators/cache/file.rb +1 -1
  48. data/lib/tr8n_core/languages/en-US.json +1362 -0
  49. data/lib/tr8n_core/version.rb +2 -2
  50. data/spec/language_case_spec.rb +3 -3
  51. data/spec/tokens/data_spec.rb +38 -84
  52. metadata +5 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 176e098158a14899141b0806820928ab5a5dca78
4
- data.tar.gz: ec746b4fb3729d02b5689ba15aa6639403977b99
3
+ metadata.gz: 371fea5f20b7e2f8cfb06f4bda1515060fbb3ab6
4
+ data.tar.gz: cf0ae840daf16a690b35243f5cb500b2f4c23548
5
5
  SHA512:
6
- metadata.gz: bbec300dd025ef1981175efb9b245115c320080bacd001efb8ed2cd5ee0aff0d0ae66a9fd4f6c1a84b40f583a4cb0324bbdfb04e89437e17a79e2bf48033c22d
7
- data.tar.gz: 3a80ed12cccb3ebe9d34ff032fafc59d8b7d8ce5db78f11abe15d23236c2cc33d409718ff03ac12a017b477fbfb5d140d4b5ea89d1a8fb2ec2138165143ab59b
6
+ metadata.gz: 1235999df54ca3cdcd5e959f1a4c71b6aeb92bae769da08bd3100f2654e81ce59cdbbf329000650edcf2e6dd78675e7e260e26f666048b26c3383f0720c3beb9
7
+ data.tar.gz: af88480f1e4d56fbd6dad45503d33c99768581a0be32acb19e34abc08d49814c6e6ae5c62f2156859c2afbb12ead7c2ff9c4534393381a8d7ddceef7b84e177c
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2014 Michael Berkovich, tr8nhub.com
1
+ Copyright (c) 2014 Michael Berkovich, TranslationExchange.com
2
2
 
3
3
  MIT License
4
4
 
data/README.md CHANGED
@@ -17,6 +17,14 @@ The library works in conjunctions with tr8nhub.com service that provides machine
17
17
  In order to use the library, you should sign up at tr8nhub.com, create a new application and copy the application key and secret.
18
18
 
19
19
 
20
+ Rails Integration
21
+ ==================
22
+
23
+ If you are planning on using Tr8n in a Rails application, you should use tr8n_rails_client_sdk gem instead.
24
+
25
+ https://github.com/tr8n/tr8n_rails_clientsdk
26
+
27
+
20
28
  Installation
21
29
  ==================
22
30
 
@@ -34,7 +42,7 @@ The library can be invoked from the IRB. To use tr8n client you must require it,
34
42
 
35
43
  ```ruby
36
44
  irb(main)> require 'tr8n_core'
37
- irb(main)> app = Tr8n::Application.init(KEY, SECRET)
45
+ irb(main)> app = Tr8n.session.init(KEY, SECRET)
38
46
  ```
39
47
 
40
48
  Now you can use the application to get any language registered with your app:
@@ -154,7 +162,6 @@ Or in a simpler form:
154
162
  [bold: {user}] {user| получил, получила} [link: [bold: {count}] {count| сообщение, сообщения, сообщений}]
155
163
  ```
156
164
 
157
-
158
165
  One of the advantages of using TML is the ability to easily switch token values. The above example in a text based email can reuse translations:
159
166
 
160
167
  ```ruby
@@ -189,11 +196,42 @@ irb(main)> russian.translate('This is a new phrase without translations')
189
196
  => "Это новая фраза без перевода"
190
197
  ```
191
198
 
192
- More Information
199
+ Links
193
200
  ==================
194
201
 
195
- If you are planning to use Tr8n in Rails application, you should use tr8n_rails_client_sdk gem instead.
202
+ * Register on TranslationExchange.com: https://translationexchange.com
196
203
 
197
- https://github.com/tr8n/tr8n_rails_clientsdk
204
+ * Read TranslationExchange's documentation: http://wiki.translationexchange.com
205
+
206
+ * Visit TranslationExchange's blog: http://blog.translationexchange.com
207
+
208
+ * Follow TranslationExchange on Twitter: https://twitter.com/translationx
209
+
210
+ * Connect with TranslationExchange on Facebook: https://www.facebook.com/translationexchange
211
+
212
+ * If you have any questions or suggestions, contact us: info@translationexchange.com
213
+
214
+
215
+ Copyright and license
216
+ ==================
198
217
 
199
- To read more about TML (Translation Markup Language) syntax, please visit http://wiki.tr8nhub.com
218
+ Copyright (c) 2014 Michael Berkovich, TranslationExchange.com
219
+
220
+ Permission is hereby granted, free of charge, to any person obtaining
221
+ a copy of this software and associated documentation files (the
222
+ "Software"), to deal in the Software without restriction, including
223
+ without limitation the rights to use, copy, modify, merge, publish,
224
+ distribute, sublicense, and/or sell copies of the Software, and to
225
+ permit persons to whom the Software is furnished to do so, subject to
226
+ the following conditions:
227
+
228
+ The above copyright notice and this permission notice shall be
229
+ included in all copies or substantial portions of the Software.
230
+
231
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
232
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
233
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
234
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
235
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
236
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
237
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,109 @@
1
+ # encoding: UTF-8
2
+ #--
3
+ # Copyright (c) 2014 Michael Berkovich, TranslationExchange.com
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining
6
+ # a copy of this software and associated documentation files (the
7
+ # "Software"), to deal in the Software without restriction, including
8
+ # without limitation the rights to use, copy, modify, merge, publish,
9
+ # distribute, sublicense, and/or sell copies of the Software, and to
10
+ # permit persons to whom the Software is furnished to do so, subject to
11
+ # the following conditions:
12
+ #
13
+ # The above copyright notice and this permission notice shall be
14
+ # included in all copies or substantial portions of the Software.
15
+ #
16
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+ #++
24
+
25
+ require 'faraday'
26
+
27
+ class Tr8n::ApiClient < Tr8n::Base
28
+ API_HOST = 'https://translationexchange.com'
29
+ API_PATH = '/tr8n/api/'
30
+
31
+ attributes :application
32
+
33
+ def access_token
34
+ application.access_token ||= begin
35
+ api("oauth/request_token",
36
+ {
37
+ :client_id => application.key,
38
+ :client_secret => application.secret,
39
+ :grant_type => :client_credentials
40
+ },
41
+ {
42
+ :host => application.host
43
+ }
44
+ )
45
+ end
46
+ application.access_token["access_token"]
47
+ end
48
+
49
+ def get(path, params = {}, opts = {})
50
+ api(path, params.merge(:access_token => access_token, :t => Time.now.to_i), opts)
51
+ end
52
+
53
+ def post(path, params = {}, opts = {})
54
+ api(path, params.merge(:access_token => access_token, :t => Time.now.to_i), opts.merge(:method => :post))
55
+ end
56
+
57
+ def self.error?(data)
58
+ not data["error"].nil?
59
+ end
60
+
61
+ def host
62
+ application.host || API_HOST
63
+ end
64
+
65
+ def api(path, params = {}, opts = {})
66
+ Tr8n.logger.trace_api_call(path, params) do
67
+ conn = Faraday.new(:url => host) do |faraday|
68
+ faraday.request(:url_encoded) # form-encode POST params
69
+ # faraday.response :logger # log requests to STDOUT
70
+ faraday.adapter(Faraday.default_adapter) # make requests with Net::HTTP
71
+ end
72
+
73
+ if opts[:method] == :post
74
+ response = conn.post("#{API_PATH}#{path}", params)
75
+ else
76
+ response = conn.get("#{API_PATH}#{path}", params)
77
+ end
78
+
79
+ data = JSON.parse(response.body)
80
+
81
+ unless data["error"].nil?
82
+ raise Tr8n::Exception.new("Error: #{data["error"]}")
83
+ end
84
+
85
+ process_response(data, opts)
86
+ end
87
+ end
88
+
89
+ def object_class(opts)
90
+ return unless opts[:class]
91
+ opts[:class].is_a?(String) ? opts[:class].constantize : opts[:class]
92
+ end
93
+
94
+ def process_response(data, opts)
95
+ if data["results"]
96
+ Tr8n.logger.debug("recieved #{data["results"].size} result(s)")
97
+ return data["results"] unless object_class(opts)
98
+ objects = []
99
+ data["results"].each do |data|
100
+ objects << object_class(opts).new(data.merge(opts[:attributes] || {}))
101
+ end
102
+ return objects
103
+ end
104
+
105
+ return data unless object_class(opts)
106
+ Tr8n.logger.debug("constructing #{object_class(opts).name}")
107
+ object_class(opts).new(data.merge(opts[:attributes] || {}))
108
+ end
109
+ end
@@ -1,5 +1,6 @@
1
+ # encoding: UTF-8
1
2
  #--
2
- # Copyright (c) 2014 Michael Berkovich, tr8nhub.com
3
+ # Copyright (c) 2014 Michael Berkovich, TranslationExchange.com
3
4
  #
4
5
  # Permission is hereby granted, free of charge, to any person obtaining
5
6
  # a copy of this software and associated documentation files (the
@@ -23,40 +24,16 @@
23
24
 
24
25
  require 'faraday'
25
26
 
26
- API_HOST = 'https://tr8nhub.com'
27
- API_PATH = '/tr8n/api/'
28
-
29
27
  class Tr8n::Application < Tr8n::Base
30
28
  attributes :host, :key, :secret, :access_token, :name, :description, :threshold, :default_locale, :default_level
31
29
  has_many :features, :languages, :featured_locales, :sources, :components, :tokens, :css, :shortcuts
32
30
 
33
- # Application Initialization
34
- # If host can be provided to point to sandbox or other instance of the service
35
- def self.init(key, secret, host = nil, options = {})
36
- options[:definition] = true if options[:definition].nil?
37
-
38
- host ||= API_HOST
39
-
40
- Tr8n.cache.reset_version
41
-
42
- Tr8n.logger.info("Initializing application...")
43
-
44
- Tr8n.session.application = Tr8n.cache.fetch(cache_key(key)) do
45
- # get access token and store it in the application
46
- access_token = api("oauth/request_token", {:client_id => key, :client_secret => secret, :grant_type => :client_credentials}, {:host => host})
47
-
48
- # get application details
49
- api("application", {:client_id => key, :definition => options[:definition]}, {:host => host, :access_token => access_token["access_token"], :class => Tr8n::Application, :attributes => {
50
- :host => host,
51
- :key => key,
52
- :secret => secret,
53
- :access_token => access_token["access_token"]
54
- }})
55
- end
31
+ def fetch
32
+ update_attributes(api_client.get("application", {:definition => true}))
33
+ self
56
34
  end
57
35
 
58
- # Initializes the application from a set of attributes received from the API
59
- def initialize(attrs = {})
36
+ def update_attributes(attrs)
60
37
  super
61
38
 
62
39
  self.attributes[:languages] = []
@@ -99,8 +76,7 @@ class Tr8n::Application < Tr8n::Base
99
76
 
100
77
  return nil unless fetch
101
78
 
102
- # for translator languages will continue to build application cache
103
- @languages_by_locale[locale] = get("language", {:locale => locale}, {:class => Tr8n::Language, :attributes => {:application => self}})
79
+ @languages_by_locale[locale] = Tr8n::Language.new(:locale => locale, :application => self).fetch
104
80
 
105
81
  if Tr8n.config.cache[:enabled] and not Tr8n.cache.read_only?
106
82
  Tr8n.cache.store(Tr8n::Language.cache_key(locale), @languages_by_locale[locale])
@@ -134,7 +110,7 @@ class Tr8n::Application < Tr8n::Base
134
110
  return @sources_by_key[key] if @sources_by_key[key]
135
111
  return nil unless register
136
112
 
137
- @sources_by_key[key] ||= post("source/register", {:source => key}, {:class => Tr8n::Source, :attributes => {:application => self}})
113
+ @sources_by_key[key] ||= api_client.post("source/register", {:source => key}, {:class => Tr8n::Source, :attributes => {:application => self}})
138
114
  end
139
115
 
140
116
  def component(key, register = true)
@@ -143,7 +119,7 @@ class Tr8n::Application < Tr8n::Base
143
119
  return @components_by_key[key] if @components_by_key[key]
144
120
  return nil unless register
145
121
 
146
- @components_by_key[key] ||= post("component/register", {:component => key}, {:class => Tr8n::Component, :attributes => {:application => self}})
122
+ @components_by_key[key] ||= api_client.post("component/register", {:component => key}, {:class => Tr8n::Component, :attributes => {:application => self}})
147
123
  end
148
124
 
149
125
  def translation_keys
@@ -195,14 +171,14 @@ class Tr8n::Application < Tr8n::Base
195
171
  next unless keys.values.any?
196
172
  params << {:source => source, :keys => keys.values.collect{|tkey| tkey.to_hash(:label, :description, :locale, :level)}}
197
173
  end
198
- post('source/register_keys', {:source_keys => params.to_json}, :method => :post)
174
+ api_client.post('source/register_keys', {:source_keys => params.to_json})
199
175
  @missing_keys_by_sources = nil
200
176
  end
201
177
 
202
178
  def featured_languages
203
179
  @featured_languages ||= begin
204
180
  locales = Tr8n.cache.fetch("featured_locales") do
205
- get("application/featured_locales")
181
+ api_client.get("application/featured_locales")
206
182
  end
207
183
  # use app languages, there is no need for rules for this call
208
184
  (locales.nil? or locales.empty?) ? [] : languages.select{|l| locales.include?(l.locale)}
@@ -210,7 +186,7 @@ class Tr8n::Application < Tr8n::Base
210
186
  end
211
187
 
212
188
  def translators
213
- get("application/translators", {}, {:class => Tr8n::Translator, :attributes => {:application => self}})
189
+ api_client.get("application/translators", {}, {:class => Tr8n::Translator, :attributes => {:application => self}})
214
190
  end
215
191
 
216
192
  def default_decoration_token(token)
@@ -257,66 +233,8 @@ class Tr8n::Application < Tr8n::Base
257
233
  ## API Methods
258
234
  #######################################################################################################
259
235
 
260
- def get(path, params = {}, opts = {})
261
- api(path, params, opts)
262
- end
263
-
264
- def post(path, params = {}, opts = {})
265
- api(path, params, opts.merge(:method => :post))
266
- end
267
-
268
- def self.error?(data)
269
- not data["error"].nil?
270
- end
271
-
272
- def api(path, params = {}, opts = {})
273
- #:access_token => access_token,
274
- params = params.merge(:client_id => key, :t => Time.now.to_i)
275
- self.class.api(path, params, opts.merge(:host => self.host))
276
- end
277
-
278
- def self.api(path, params = {}, opts = {})
279
- Tr8n.logger.trace_api_call(path, params) do
280
- conn = Faraday.new(:url => opts[:host]) do |faraday|
281
- faraday.request(:url_encoded) # form-encode POST params
282
- # faraday.response :logger # log requests to STDOUT
283
- faraday.adapter(Faraday.default_adapter) # make requests with Net::HTTP
284
- end
285
-
286
- if opts[:method] == :post
287
- response = conn.post("#{API_PATH}#{path}", params)
288
- else
289
- response = conn.get("#{API_PATH}#{path}", params)
290
- end
291
-
292
- data = JSON.parse(response.body)
293
-
294
- unless data["error"].nil?
295
- raise Tr8n::Exception.new("Error: #{data["error"]}")
296
- end
297
-
298
- process_response(data, opts)
299
- end
300
- end
301
-
302
- def self.object_class(opts)
303
- return unless opts[:class]
304
- opts[:class].is_a?(String) ? opts[:class].constantize : opts[:class]
236
+ def api_client
237
+ @api_client ||= Tr8n::ApiClient.new(:application => self)
305
238
  end
306
239
 
307
- def self.process_response(data, opts)
308
- if data["results"]
309
- Tr8n.logger.debug("recieved #{data["results"].size} result(s)")
310
- return data["results"] unless object_class(opts)
311
- objects = []
312
- data["results"].each do |data|
313
- objects << object_class(opts).new(data.merge(opts[:attributes] || {}))
314
- end
315
- return objects
316
- end
317
-
318
- return data unless object_class(opts)
319
- Tr8n.logger.debug("constructing #{object_class(opts).name}")
320
- object_class(opts).new(data.merge(opts[:attributes] || {}))
321
- end
322
240
  end
@@ -1,5 +1,6 @@
1
+ # encoding: UTF-8
1
2
  #--
2
- # Copyright (c) 2014 Michael Berkovich, tr8nhub.com
3
+ # Copyright (c) 2014 Michael Berkovich, TranslationExchange.com
3
4
  #
4
5
  # Permission is hereby granted, free of charge, to any person obtaining
5
6
  # a copy of this software and associated documentation files (the
@@ -26,8 +27,12 @@ class Tr8n::Base
26
27
 
27
28
  def initialize(attrs = {})
28
29
  @attributes = {}
30
+ update_attributes(attrs)
31
+ end
32
+
33
+ def update_attributes(attrs = {})
29
34
  attrs.each do |key, value|
30
- # pp [self.class.name, key, self.class.attributes, self.class.attributes.include?(key.to_sym)]
35
+ #pp [self.class.name, key, self.class.attributes, self.class.attributes.include?(key.to_sym)]
31
36
  next unless self.class.attributes.include?(key.to_sym)
32
37
  @attributes[key.to_sym] = value
33
38
  end
@@ -1,5 +1,6 @@
1
+ # encoding: UTF-8
1
2
  #--
2
- # Copyright (c) 2014 Michael Berkovich, tr8nhub.com
3
+ # Copyright (c) 2014 Michael Berkovich, TranslationExchange.com
3
4
  #
4
5
  # Permission is hereby granted, free of charge, to any person obtaining
5
6
  # a copy of this software and associated documentation files (the
@@ -1,5 +1,6 @@
1
+ # encoding: UTF-8
1
2
  #--
2
- # Copyright (c) 2014 Michael Berkovich, tr8nhub.com
3
+ # Copyright (c) 2014 Michael Berkovich, TranslationExchange.com
3
4
  #
4
5
  # Permission is hereby granted, free of charge, to any person obtaining
5
6
  # a copy of this software and associated documentation files (the
@@ -1,5 +1,6 @@
1
+ # encoding: UTF-8
1
2
  #--
2
- # Copyright (c) 2014 Michael Berkovich, tr8nhub.com
3
+ # Copyright (c) 2014 Michael Berkovich, TranslationExchange.com
3
4
  #
4
5
  # Permission is hereby granted, free of charge, to any person obtaining
5
6
  # a copy of this software and associated documentation files (the
@@ -1,5 +1,6 @@
1
+ # encoding: UTF-8
1
2
  #--
2
- # Copyright (c) 2014 Michael Berkovich, tr8nhub.com
3
+ # Copyright (c) 2014 Michael Berkovich, TranslationExchange.com
3
4
  #
4
5
  # Permission is hereby granted, free of charge, to any person obtaining
5
6
  # a copy of this software and associated documentation files (the
@@ -1,5 +1,6 @@
1
+ # encoding: UTF-8
1
2
  #--
2
- # Copyright (c) 2014 Michael Berkovich, tr8nhub.com
3
+ # Copyright (c) 2014 Michael Berkovich, TranslationExchange.com
3
4
  #
4
5
  # Permission is hereby granted, free of charge, to any person obtaining
5
6
  # a copy of this software and associated documentation files (the
@@ -1,5 +1,6 @@
1
+ # encoding: UTF-8
1
2
  #--
2
- # Copyright (c) 2014 Michael Berkovich, tr8nhub.com
3
+ # Copyright (c) 2014 Michael Berkovich, TranslationExchange.com
3
4
  #
4
5
  # Permission is hereby granted, free of charge, to any person obtaining
5
6
  # a copy of this software and associated documentation files (the