r18n-core 0.4.4 → 0.4.5

Sign up to get free protection for your applications and to get access to all the features.
data/ChangeLog CHANGED
@@ -1,3 +1,9 @@
1
+ == 0.4.5 (Annual)
2
+ * Filters for several types.
3
+ * Global HTML escaping run before Markdown and Textile formatting.
4
+ * Fix active filters after passive filters.
5
+ * Fix human time formatting for dates with same month days.
6
+
1
7
  == 0.4.4 (Frank)
2
8
  * Use before filter to lazy set I18n object in Sinatra extension.
3
9
  * Set I18n object to thread (by Simon Hafner).
@@ -147,8 +147,9 @@ module R18n
147
147
  # Note that passive filters will be always run before active.
148
148
  # * +passive+ – if +true+, filter will process only on translation
149
149
  # loading. Note that you must add all passive before load translation.
150
- def add(type, name = nil, options = {}, &block)
150
+ def add(types, name = nil, options = {}, &block)
151
151
  options, name = name, nil if name.is_a? Hash
152
+ types = Array(types)
152
153
 
153
154
  unless name
154
155
  @last_auto_name ||= 0
@@ -160,17 +161,18 @@ module R18n
160
161
  delete(name)
161
162
  end
162
163
 
163
- @by_type[type] = [] unless @by_type.has_key? type
164
-
165
- filter = Filter.new(name, type, block, true, options[:passive])
164
+ filter = Filter.new(name, types, block, true, options[:passive])
166
165
  @defined[name] = filter
167
166
 
168
- if options.has_key? :position
169
- @by_type[type].insert(options[:position], filter)
170
- else
171
- @by_type[type] << filter
167
+ types.each do |type|
168
+ @by_type[type] = [] unless @by_type.has_key? type
169
+ if options.has_key? :position
170
+ @by_type[type].insert(options[:position], filter)
171
+ else
172
+ @by_type[type] << filter
173
+ end
174
+ rebuild_enabled! type
172
175
  end
173
- rebuild_enabled! type
174
176
 
175
177
  filter
176
178
  end
@@ -181,8 +183,10 @@ module R18n
181
183
  return unless filter
182
184
 
183
185
  @defined.delete(filter.name)
184
- @by_type[filter.type].delete(filter)
185
- rebuild_enabled! filter.type
186
+ filter.types.each do |type|
187
+ @by_type[type].delete(filter)
188
+ rebuild_enabled! type
189
+ end
186
190
  end
187
191
 
188
192
  # Disable +filter+ by name or Filter object.
@@ -191,7 +195,7 @@ module R18n
191
195
  return unless filter
192
196
 
193
197
  filter.enabled = false
194
- rebuild_enabled! filter.type
198
+ filter.types.each { |type| rebuild_enabled! type }
195
199
  end
196
200
 
197
201
  # Turn on disabled +filter+ by name or Filter object.
@@ -200,17 +204,17 @@ module R18n
200
204
  return unless filter
201
205
 
202
206
  filter.enabled = true
203
- rebuild_enabled! filter.type
207
+ filter.types.each { |type| rebuild_enabled! type }
204
208
  end
205
209
  end
206
210
 
207
- Filters.defined = {}
208
- Filters.by_type = Hash.new([])
209
- Filters.active_enabled = Hash.new([])
211
+ Filters.defined = {}
212
+ Filters.by_type = Hash.new([])
213
+ Filters.active_enabled = Hash.new([])
210
214
  Filters.passive_enabled = Hash.new([])
211
- Filters.enabled = Hash.new([])
215
+ Filters.enabled = Hash.new([])
212
216
 
213
- Filter = Struct.new(:name, :type, :block, :enabled, :passive) do
217
+ Filter = Struct.new(:name, :types, :block, :enabled, :passive) do
214
218
  def call(*params); block.call(*params); end
215
219
  def enabled?; enabled; end
216
220
  def passive?; passive; end
@@ -253,10 +257,12 @@ module R18n
253
257
  content
254
258
  end
255
259
 
256
- Filters.add(String, :global_escape_html, :passive => true) do |html, config|
260
+ Filters.add([String, 'markdown', 'textile'],
261
+ :global_escape_html, :passive => true) do |html, config|
257
262
  if config[:dont_escape_html]
258
263
  html
259
264
  else
265
+ config[:dont_escape_html] = true
260
266
  Utils.escape_html(html)
261
267
  end
262
268
  end
@@ -221,7 +221,8 @@ module R18n
221
221
  # time. For special cases you can replace it in locale’s class.
222
222
  def format_time_human(time, i18n, now = Time.now, *params)
223
223
  minutes = (time - now) / 60.0
224
- if time.mday != now.mday and minutes.abs > 720 # 12 hours
224
+ diff = minutes.abs
225
+ if (diff > 24 * 60) or (time.mday != now.mday and diff > 12 * 24)
225
226
  format_date_human(R18n::Utils.to_date(time), i18n,
226
227
  R18n::Utils.to_date(now)) + format_time(time)
227
228
  else
@@ -233,7 +234,7 @@ module R18n
233
234
  when -1..1
234
235
  i18n.human_time.now
235
236
  else
236
- hours = (minutes / 60.0).abs.floor
237
+ hours = (diff / 60.0).floor
237
238
  if time > now
238
239
  i18n.human_time.after_hours(hours)
239
240
  else
@@ -32,7 +32,7 @@ module R18n
32
32
  def initialize(str, locale, path)
33
33
  super(str)
34
34
  @locale = locale
35
- @path = path
35
+ @path = path
36
36
  end
37
37
 
38
38
  # Return self for translated string.
@@ -91,8 +91,9 @@ module R18n
91
91
  when Hash
92
92
  value = Translation.new(@locale, path, locale, value)
93
93
  when String
94
- v = TranslatedString.new(value, locale, path)
95
- value = Filters.process_string(Filters.passive_enabled, v, path, {})
94
+ c = { :locale => locale, :path => path }
95
+ v = Filters.process_string(Filters.passive_enabled, value, c, [])
96
+ value = TranslatedString.new(v, locale, path)
96
97
  when Typed
97
98
  value.locale = locale
98
99
  value.path = path
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  module R18n
3
- VERSION = '0.4.4' unless defined? R18n::VERSION
3
+ VERSION = '0.4.5' unless defined? R18n::VERSION
4
4
  end
data/spec/filters_spec.rb CHANGED
@@ -24,7 +24,7 @@ describe R18n::Filters do
24
24
 
25
25
  filter.should be_a(R18n::Filters::Filter)
26
26
  filter.name.should == :my_filter
27
- filter.type.should == 'my'
27
+ filter.types.should == ['my']
28
28
  filter.should be_enabled
29
29
 
30
30
  R18n::Filters.defined.should have_key(:my_filter)
@@ -32,6 +32,12 @@ describe R18n::Filters do
32
32
  @i18n.my_tree_filter.should == {'name' => 'value'}
33
33
  end
34
34
 
35
+ it "should add filter for several types" do
36
+ filter = R18n::Filters.add(['my', 'your']) { |i, config| i + '1' }
37
+ @i18n.my_filter.should == 'value1'
38
+ @i18n.your_filter.should == 'another1'
39
+ end
40
+
35
41
  it "should use passive filters" do
36
42
  filter = mock()
37
43
  filter.should_receive(:process).twice.and_return(1)
@@ -187,11 +193,11 @@ describe R18n::Filters do
187
193
  end
188
194
 
189
195
  it "should have disabled global filter for escape HTML" do
190
- @i18n.greater.should == '1 < 2 is true'
196
+ @i18n.greater('true').should == '1 < 2 is true'
191
197
 
192
198
  R18n::Filters.on(:global_escape_html)
193
199
  @i18n.reload!
194
- @i18n.greater.should == '1 &lt; 2 is true'
200
+ @i18n.greater('true').should == '1 &lt; 2 is true'
195
201
  @i18n.html.should == '&lt;script&gt;true &amp;&amp; false&lt;/script&gt;'
196
202
  end
197
203
 
@@ -204,11 +210,21 @@ describe R18n::Filters do
204
210
  end
205
211
 
206
212
  it "should have Markdown filter" do
207
- @i18n.markdown.should == '<p><strong>Hi!</strong></p>'
213
+ @i18n.markdown.simple.should == '<p><strong>Hi!</strong></p>'
208
214
  end
209
215
 
210
216
  it "should have Textile filter" do
211
- @i18n.textile.should == '<p><em>Hi!</em></p>'
217
+ @i18n.textile.simple.should == '<p><em>Hi!</em></p>'
218
+ end
219
+
220
+ it "should HTML escape before Markdown and Textile filters" do
221
+ @i18n.markdown.html.should == '<p><strong>Hi!</strong> <br /></p>'
222
+ @i18n.textile.html.should == '<p><em>Hi!</em><br /></p>'
223
+
224
+ R18n::Filters.on(:global_escape_html)
225
+ @i18n.reload!
226
+ @i18n.markdown.html.should == '<p><strong>Hi!</strong> &#60;br /&#62;</p>'
227
+ @i18n.textile.html.should == '<p><em>Hi!</em>&lt;br /&gt;</p>'
212
228
  end
213
229
 
214
230
  end
data/spec/locale_spec.rb CHANGED
@@ -105,7 +105,7 @@ describe R18n::Locale do
105
105
  zero = Time.at(0).utc
106
106
  params = [:human, R18n::I18n.new('ru'), zero]
107
107
 
108
- @ru.localize( zero + 7 * day, *params).should == ' 8 января 00:00'
108
+ @ru.localize( zero + 7 * day, *params).should == ' 8 января 00:00'
109
109
  @ru.localize( zero + 50 * hour, *params).should == 'через 2 дня 02:00'
110
110
  @ru.localize( zero + 25 * hour, *params).should == 'завтра 01:00'
111
111
  @ru.localize( zero + 70 * minute, *params).should == 'через 1 час'
@@ -113,10 +113,12 @@ describe R18n::Locale do
113
113
  @ru.localize( zero + 5, *params).should == 'сейчас'
114
114
  @ru.localize( zero - 15, *params).should == 'сейчас'
115
115
  @ru.localize( zero - minute, *params).should == '1 минуту назад'
116
- @ru.localize( zero - 2 * hour, *params).should == '2 часа назад'
116
+ @ru.localize( zero - 2 * hour, *params).should == '2 часа назад'
117
117
  @ru.localize( zero - 13 * hour, *params).should == 'вчера 11:00'
118
118
  @ru.localize( zero - 50 * hour, *params).should == '3 дня назад 22:00'
119
- @ru.localize( zero - 9 * day, *params).should == '23 декабря 1969 00:00'
119
+ @ru.localize( zero - 9 * day, *params).should == '23 декабря 1969 00:00'
120
+
121
+ @ru.localize( zero - 365 * day, *params).should == ' 1 января 1969 00:00'
120
122
  end
121
123
 
122
124
  it "should use standard formatter by default" do
@@ -21,16 +21,21 @@ files: !!pl
21
21
  1: 1 file
22
22
  n: %1 files
23
23
 
24
+ your_filter: !!your another
24
25
  my_filter: !!my value
25
26
  my_tree_filter: !!my
26
27
  name: value
27
28
 
28
29
  html: !!escape
29
30
  <script>true && false</script>
30
- greater: 1 < 2 is true
31
+ greater: 1 < 2 is %1
31
32
  no_escape: !!html
32
33
  <b>Warning</b>
33
34
 
34
- markdown: !!markdown **Hi!**
35
+ markdown:
36
+ simple: !!markdown **Hi!**
37
+ html: !!markdown **Hi!** <br />
35
38
 
36
- textile: !!textile _Hi!_
39
+ textile:
40
+ simple: !!textile _Hi!_
41
+ html: !!textile _Hi!_<br />
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: r18n-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.4
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 4
8
+ - 5
9
+ version: 0.4.5
5
10
  platform: ruby
6
11
  authors:
7
12
  - Andrey "A.I." Sitnik
@@ -9,7 +14,7 @@ autorequire:
9
14
  bindir: bin
10
15
  cert_chain: []
11
16
 
12
- date: 2010-03-29 00:00:00 +04:00
17
+ date: 2010-04-19 00:00:00 +04:00
13
18
  default_executable:
14
19
  dependencies: []
15
20
 
@@ -82,18 +87,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
82
87
  requirements:
83
88
  - - ">="
84
89
  - !ruby/object:Gem::Version
90
+ segments:
91
+ - 0
85
92
  version: "0"
86
- version:
87
93
  required_rubygems_version: !ruby/object:Gem::Requirement
88
94
  requirements:
89
95
  - - ">="
90
96
  - !ruby/object:Gem::Version
97
+ segments:
98
+ - 0
91
99
  version: "0"
92
- version:
93
100
  requirements: []
94
101
 
95
102
  rubyforge_project: r18n-core
96
- rubygems_version: 1.3.5
103
+ rubygems_version: 1.3.6
97
104
  signing_key:
98
105
  specification_version: 3
99
106
  summary: I18n tool to translate your Ruby application.