tml 5.0.2 → 5.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3bfad1df09bdc30e9dff8ae24cfc73dd044cc1b7
4
- data.tar.gz: db29aecf3e0e719844ed5366262e843fa36cf194
3
+ metadata.gz: 66fdea16ab7a949df955b07b84cff3e12ff4ce6c
4
+ data.tar.gz: c534611d08ce4f130ba280b6b6c4a69567f60ddd
5
5
  SHA512:
6
- metadata.gz: ec1a8dd025cd3b506f519e72550d8c891342bee71127531e8cbf00de7710f90706e2b679df58a8ce3b5a66c38f77d88a327a5fcbfa7c6063e47c663b8e4b3e90
7
- data.tar.gz: b5f76143c7c34632dcd4c3193f822640916b62786b53c4a21fd8ef5dbea3fec83cbadedbf426b923e43df66e4a420a9f3adaa73f27ae91088812aa8801336efe
6
+ metadata.gz: 35e492c0334f46220d7f85fd68465c03f70b11a22106d601176009f92edb21cb52bc55bb0ce1d79f1e310d5f780a1baa8f35e4a2957a43eda0a8278c311fe448
7
+ data.tar.gz: 3d6ae9f883fd21971e928f76aeae05cb969b448f5cee9af26b1b04e6bbc85bb04518d911cc2778ff00af3c567bb58ebead7fd6710c8fec8bac8eaa9d923669a3
@@ -135,8 +135,8 @@ class Tml::Application < Tml::Base
135
135
  end
136
136
 
137
137
  # Normalizes and returns current language
138
- # TODO: verify usage
139
138
  def current_language(locale)
139
+ return Tml.config.default_language unless locale
140
140
  locale = locale.gsub('_', '-') if locale
141
141
  lang = language(locale)
142
142
  lang ||= language(locale.split('-').first) if locale.index('-')
data/lib/tml/config.rb CHANGED
@@ -80,7 +80,7 @@ module Tml
80
80
 
81
81
  # Used by Rails and Sinatra extensions
82
82
  attr_accessor :current_locale_method, :current_user_method, :translator_options, :i18n_backend
83
- attr_accessor :invalidator
83
+ attr_accessor :invalidator, :agent
84
84
 
85
85
  # Used for IRB only
86
86
  attr_accessor :submit_missing_keys_realtime
@@ -100,6 +100,11 @@ module Tml
100
100
  extension: false
101
101
  }
102
102
 
103
+ @agent = {
104
+ enabled: true,
105
+ type: 'agent'
106
+ }
107
+
103
108
  # if running from IRB, make it default to TRUE
104
109
  @submit_missing_keys_realtime = (%w(irb pry).include?($0 || ''))
105
110
 
@@ -131,7 +136,7 @@ module Tml
131
136
  },
132
137
  data_tokens: {
133
138
  special: {
134
- enable: true,
139
+ enabled: true,
135
140
  regex: /(&[^;]*;)/
136
141
  },
137
142
  date: {
@@ -41,12 +41,31 @@ class Tml::Decorators::Base < Tml::Base
41
41
  raise Tml::Exception.new('Must be implemented by the extending class')
42
42
  end
43
43
 
44
+ def decorate_language_case(language_case, rule, original, transformed, options = {})
45
+ raise Tml::Exception.new('Must be implemented by the extending class')
46
+ end
47
+
48
+ def decorate_token(token, value, options = {})
49
+ raise Tml::Exception.new('Must be implemented by the extending class')
50
+ end
51
+
52
+ def decorate_element(token, value, options = {})
53
+ raise Tml::Exception.new('Must be implemented by the extending class')
54
+ end
55
+
44
56
  def inline_mode?
45
57
  Tml.session.current_translator and Tml.session.current_translator.inline?
46
58
  end
47
59
 
48
- def decorate_language_case(language_case, rule, original, transformed, options = {})
49
- raise Tml::Exception.new('Must be implemented by the extending class')
60
+ def enabled?(options)
61
+ return false if options[:skip_decorations]
62
+ inline_mode?
50
63
  end
51
-
64
+
65
+ def decoration_element(default, options)
66
+ return 'span' if options[:use_span]
67
+ return 'div' if options[:use_div]
68
+ default
69
+ end
70
+
52
71
  end
@@ -40,4 +40,12 @@ class Tml::Decorators::Default < Tml::Decorators::Base
40
40
  transformed
41
41
  end
42
42
 
43
+ def decorate_token(token, value, options = {})
44
+ value
45
+ end
46
+
47
+ def decorate_element(token, value, options = {})
48
+ value
49
+ end
50
+
43
51
  end
@@ -35,9 +35,10 @@ class Tml::Decorators::Html < Tml::Decorators::Base
35
35
  def decorate(translated_label, translation_language, target_language, translation_key, options = {})
36
36
  #Tml.logger.info("Decorating #{translated_label} of #{translation_language.locale} to #{target_language.locale}")
37
37
 
38
+ return translated_label unless enabled?(options)
39
+
38
40
  # if translation key language is the same as target language - skip decorations
39
- if options[:skip_decorations] or not inline_mode? or
40
- (translation_key.application.feature_enabled?(:lock_original_content) and translation_key.language == target_language)
41
+ if translation_key.application.feature_enabled?(:lock_original_content) and translation_key.language == target_language
41
42
  return translated_label
42
43
  end
43
44
 
@@ -59,9 +60,7 @@ class Tml::Decorators::Html < Tml::Decorators::Base
59
60
  classes << 'tml_fallback'
60
61
  end
61
62
 
62
- element = 'tml:label'
63
- element = 'span' if options[:use_span]
64
- element = 'div' if options[:use_div]
63
+ element = decoration_element('tml:label', options)
65
64
 
66
65
  html = "<#{element} class='#{classes.join(' ')}' data-translation_key='#{translation_key.key}' data-target_locale='#{target_language.locale}'>"
67
66
  html << translated_label
@@ -70,8 +69,7 @@ class Tml::Decorators::Html < Tml::Decorators::Base
70
69
  end
71
70
 
72
71
  def decorate_language_case(language_case, rule, original, transformed, options = {})
73
- return transformed if options[:skip_decorations] or language_case.language.default?
74
- return transformed unless inline_mode?
72
+ return transformed unless enabled?(options)
75
73
 
76
74
  data = {
77
75
  'keyword' => language_case.keyword,
@@ -93,9 +91,7 @@ class Tml::Decorators::Html < Tml::Decorators::Base
93
91
  attrs << "#{key}=\"#{value.to_s.gsub('"', "\"")}\""
94
92
  end
95
93
 
96
- element = 'tml:label'
97
- element = 'span' if options[:use_span]
98
- element = 'div' if options[:use_div]
94
+ element = decoration_element('tml:case', options)
99
95
 
100
96
  html = "<#{element} #{attrs.join(' ')}>"
101
97
  html << transformed
@@ -103,4 +99,31 @@ class Tml::Decorators::Html < Tml::Decorators::Base
103
99
  html
104
100
  end
105
101
 
102
+ def decorate_token(token, value, options = {})
103
+ return value unless enabled?(options)
104
+
105
+ element = decoration_element('tml:token', options)
106
+
107
+ classes = ['tml_token', "tml_token_#{token.decoration_name}"]
108
+
109
+ html = "<#{element} class='#{classes.join(' ')}' data-name='#{token.name}'"
110
+ html << " data-context='#{token.context_keys.join(',')}'" if token.context_keys.any?
111
+ html << " data-case='#{token.case_keys.join(',')}'" if token.case_keys.any?
112
+ html << '>'
113
+ html << value
114
+ html << "</#{element}>"
115
+ html
116
+ end
117
+
118
+ def decorate_element(token, value, options = {})
119
+ return value unless enabled?(options)
120
+
121
+ element = decoration_element('tml:element', options)
122
+
123
+ html = "<#{element}>"
124
+ html << value
125
+ html << "</#{element}>"
126
+ html
127
+ end
128
+
106
129
  end
data/lib/tml/session.rb CHANGED
@@ -71,6 +71,7 @@ module Tml
71
71
  end
72
72
 
73
73
  def preferred_locale(locales)
74
+ return application.default_locale unless locales
74
75
  locales = locales.is_a?(String) ? locales.split(',') : locales
75
76
  locales.each do |locale|
76
77
  return locale if application.locales.include?(locale)
@@ -259,18 +259,17 @@ module Tml
259
259
  rules = option('data_tokens.rules')
260
260
  if rules
261
261
  rules.each do |rule|
262
- if rule[:enabled]
263
- matches = text.scan(rule[:regex])
264
-
265
- if matches
266
- matches.each do |match|
267
- next if match.first.nil? or match.first == ''
268
- value = match.first.strip
269
-
270
- unless value == ''
271
- token = contextualize(rule[:name], value.gsub(/[.,;\s]/, '').to_i)
272
- text = text.gsub(value, value.gsub(value, "{#{token}}"))
273
- end
262
+ next unless rule[:enabled]
263
+ matches = text.scan(rule[:regex])
264
+
265
+ if matches
266
+ matches.each do |match|
267
+ next if match.first.nil? or match.first == ''
268
+ value = match.first.strip
269
+
270
+ unless value == ''
271
+ token = contextualize(rule[:name], value.gsub(/[.,;\s]/, '').to_i)
272
+ text = text.gsub(value, value.gsub(value, "{#{token}}"))
274
273
  end
275
274
  end
276
275
  end
@@ -160,14 +160,14 @@ module Tml
160
160
 
161
161
  values = objects.collect do |obj|
162
162
  if method.is_a?(String)
163
- method.gsub("{$0}", sanitize(obj.to_s, obj, language, options.merge(:safe => false)))
163
+ element = method.gsub("{$0}", sanitize(obj.to_s, obj, language, options.merge(:safe => false)))
164
164
  elsif method.is_a?(Symbol)
165
165
  if obj.is_a?(Hash)
166
166
  value = Tml::Utils.hash_value(obj, method)
167
167
  else
168
168
  value = obj.send(method)
169
169
  end
170
- sanitize(value, obj, language, options.merge(:safe => false))
170
+ element = sanitize(value, obj, language, options.merge(:safe => false))
171
171
  elsif method.is_a?(Hash)
172
172
  attr = Tml::Utils.hash_value(method, :attribute) || Tml::Utils.hash_value(method, :property)
173
173
  if obj.is_a?(Hash)
@@ -178,13 +178,15 @@ module Tml
178
178
 
179
179
  hash_value = Tml::Utils.hash_value(method, :value)
180
180
  if hash_value
181
- hash_value.gsub("{$0}", sanitize(value, obj, language, options.merge(:safe => false)))
181
+ element = hash_value.gsub("{$0}", sanitize(value, obj, language, options.merge(:safe => false)))
182
182
  else
183
- sanitize(value, obj, language, options.merge(:safe => false))
183
+ element = sanitize(value, obj, language, options.merge(:safe => false))
184
184
  end
185
185
  elsif method.is_a?(Proc)
186
- sanitize(method.call(obj), obj, language, options.merge(:safe => true))
186
+ element = sanitize(method.call(obj), obj, language, options.merge(:safe => true))
187
187
  end
188
+
189
+ Tml::Decorators::Base.decorator.decorate_element(self, element, options)
188
190
  end
189
191
 
190
192
  return values.first if objects.size == 1
@@ -384,13 +386,21 @@ module Tml
384
386
  return label.gsub(full_name, '') if object.nil?
385
387
 
386
388
  value = token_value(object, language, options)
387
- label.gsub(full_name, value)
389
+ label.gsub(full_name, decorate(value, options))
390
+ end
391
+
392
+ def decorate(value, options = {})
393
+ Tml::Decorators::Base.decorator.decorate_token(self, value, options)
388
394
  end
389
395
 
390
396
  def sanitized_name
391
397
  name(:parens => true)
392
398
  end
393
399
 
400
+ def decoration_name
401
+ self.class.name.split('::').last.downcase
402
+ end
403
+
394
404
  def to_s
395
405
  full_name
396
406
  end
@@ -56,6 +56,6 @@ class Tml::Tokens::Method < Tml::Tokens::Data
56
56
  object = Tml::Utils.hash_value(context, object_name)
57
57
  return label unless object
58
58
  object_value = sanitize(object.send(object_method_name), object, language, options.merge(:safe => false))
59
- label.gsub(full_name, object_value)
59
+ label.gsub(full_name, decorate(object_value, options))
60
60
  end
61
61
  end
@@ -208,16 +208,22 @@ class Tml::Tokens::Transform < Tml::Tokens::Data
208
208
 
209
209
  return label unless value
210
210
 
211
+ decorated_value = decorate(token_value(Tml::Utils.hash_value(context, key), language, options), options)
212
+
211
213
  substitution_value = []
212
214
  if displayed_in_translation?
213
- substitution_value << token_value(Tml::Utils.hash_value(context, key), language, options)
215
+ substitution_value << decorated_value
214
216
  substitution_value << ' '
215
217
  else
216
- value = value.gsub("##{short_name}#", token_value(Tml::Utils.hash_value(context, key), language, options))
218
+ value = value.gsub("##{short_name}#", decorated_value)
217
219
  end
218
220
  substitution_value << value
219
221
 
220
222
  label.gsub(full_name, substitution_value.join(''))
221
223
  end
222
-
224
+
225
+ def decoation_name
226
+ 'piped'
227
+ end
228
+
223
229
  end
data/lib/tml/utils.rb CHANGED
@@ -84,24 +84,21 @@ module Tml
84
84
  yaml['defaults'].rmerge(yaml[env] || {})
85
85
  end
86
86
 
87
- def self.decode(data, secret = nil)
88
- payload = URI::decode(data)
87
+ def self.decode(payload, secret = nil)
88
+ # payload = URI::decode(payload)
89
89
  payload = Base64.decode64(payload)
90
90
  data = JSON.parse(payload)
91
-
92
91
  # TODO: Verify signature
93
-
94
92
  data
95
93
  rescue Exception => ex
96
94
  {}
97
95
  end
98
96
 
99
97
  def self.encode(params, secret = nil)
100
-
101
98
  # TODO: Add signature
102
-
103
99
  payload = Base64.encode64(params.to_json)
104
- URI::encode(payload)
100
+ # URI::encode(payload)
101
+ payload
105
102
  rescue Exception => ex
106
103
  ''
107
104
  end
data/lib/tml/version.rb CHANGED
@@ -30,5 +30,5 @@
30
30
  #++
31
31
 
32
32
  module Tml
33
- VERSION = '5.0.2'
33
+ VERSION = '5.1.1'
34
34
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tml
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.0.2
4
+ version: 5.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Berkovich
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-09 00:00:00.000000000 Z
11
+ date: 2015-09-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday