r18n-core 0.4.2 → 0.4.3

Sign up to get free protection for your applications and to get access to all the features.
data/ChangeLog CHANGED
@@ -1,3 +1,10 @@
1
+ == 0.4.3 (Flange)
2
+ * Add R18n style methods to Rails controllers.
3
+ * Fix for non-string translations in Rails I18n.
4
+ * Use default locale from Rails I18n config.
5
+ * Load translations recursively.
6
+ * Add Slovak locale (by Ahmed Al Hafoudh)
7
+
1
8
  == 0.4.2 (EMS)
2
9
  * Fixes for Ruby 1.8.6 (by Akzhan Abdulin).
3
10
  * Add method to get translation keys.
data/README.rdoc CHANGED
@@ -39,7 +39,7 @@ Filters are cascade and can communicate with each other.
39
39
 
40
40
  R18n already has filters for HTML escaping, lambdas, Textile and Markdown:
41
41
 
42
- hi: !!markdown
42
+ hi: !!markdown |
43
43
  **Hi**, people!
44
44
  greater: !!escape
45
45
  1 < 2 is true
data/base/sk.yml ADDED
@@ -0,0 +1,34 @@
1
+ ok: OK
2
+ save: Uložiť
3
+ cancel: Zrušiť
4
+ 'yes': Áno
5
+ 'no': Nie
6
+ exit: Koniec
7
+ delete: Zmazať
8
+
9
+ human_time:
10
+ after_days: !!pl
11
+ 1: o %1 deň
12
+ 2: o %1 dni
13
+ n: o %1 dní
14
+ tomorrow: zajtra
15
+ after_hours: !!pl
16
+ 1: o %1 hodinu
17
+ 2: o %1 hodiny
18
+ n: o %1 hodín
19
+ after_minutes: !!pl
20
+ 1: o %1 minútu
21
+ 2: o %1 minúty
22
+ n: o %1 minút
23
+ now: teraz
24
+ today: dnes
25
+ minutes_ago: !!pl
26
+ 1: pred %1 minútou
27
+ n: pred %1 minútami
28
+ hours_ago: !!pl
29
+ 1: pred %1 hodinou
30
+ n: pred %1 hodinami
31
+ yesterday: včera
32
+ days_ago: !!pl
33
+ 1: pred %1 dňom
34
+ n: pred %1 dňami
@@ -163,43 +163,60 @@ module R18n
163
163
  @locales_codes = locales
164
164
  @locales = locales.map { |i| Locale.load(i) }
165
165
 
166
- @locale = @locales.first
167
- unless @locale.supported?
168
- @locales.each do |locale|
169
- if locale.supported?
170
- @locale.base = locale
171
- break
172
- end
173
- end
174
- end
175
-
176
166
  if translation_places
177
167
  @original_places = translation_places
178
168
  else
179
169
  @original_places = R18n.extension_places
170
+ @locale = @locales.first
180
171
  end
181
172
 
182
173
  @translation_places = self.class.convert_places(@original_places)
183
174
 
184
- reload! unless @translation = R18n.cache[translation_cache_key]
175
+ key = translation_cache_key
176
+ if R18n.cache.has_key? key
177
+ @locale, @translation = *R18n.cache[key]
178
+ else
179
+ reload!
180
+ end
185
181
  end
186
182
 
187
-
188
183
  # Return unique key for current locales in translation and places.
189
184
  def translation_cache_key
190
- available = @translation_places.inject([]) { |all, i|
185
+ @available_codes ||= @translation_places.inject([]) { |all, i|
191
186
  all + i.available }.uniq.map { |i| i.code }
192
- (@locales_codes & available).join(',') + '@' +
187
+ (@locales_codes & @available_codes).join(',') + '@' +
193
188
  R18n.default_loader.hash.to_s +
194
189
  @translation_places.hash.to_s + R18n.extension_places.hash.to_s
195
190
  end
196
191
 
197
192
  # Reload translations.
198
193
  def reload!
194
+ @available = @available_codes = nil
199
195
  @translation_places = self.class.convert_places(@original_places)
200
196
 
201
197
  available_in_places = @translation_places.map { |i| [i, i.available] }
202
- available_in_extensions = R18n.extension_places.map { |i| [i, i.available] }
198
+ available_in_extensions = R18n.extension_places.map {|i| [i, i.available]}
199
+
200
+ unless @locale
201
+ available_in_places.each do |place, available|
202
+ @locales.each do |locale|
203
+ if available.include? locale
204
+ @locale = locale
205
+ break
206
+ end
207
+ end
208
+ break if @locale
209
+ end
210
+ end
211
+ @locale ||= @locales.first
212
+ unless @locale.supported?
213
+ @locales.each do |locale|
214
+ if locale.supported?
215
+ @locale.base = locale
216
+ break
217
+ end
218
+ end
219
+ end
203
220
 
204
221
  @translation = Translation.new @locale
205
222
  @locales.each do |locale|
@@ -218,7 +235,8 @@ module R18n
218
235
  end
219
236
  end
220
237
  end
221
- R18n.cache[translation_cache_key] = @translation
238
+
239
+ R18n.cache[translation_cache_key] = [@locale, @translation]
222
240
  end
223
241
 
224
242
  # Return Array of locales with available translations.
@@ -73,8 +73,8 @@ module R18n
73
73
 
74
74
  # Load locale by RFC 3066 +code+.
75
75
  def self.load(code)
76
- original = code.to_s.gsub(/[^-a-zA-Z]/, '')
77
- code = original.downcase
76
+ original = code.to_s.gsub(/[^-_a-zA-Z]/, '')
77
+ code = original.gsub('_', '-').downcase
78
78
 
79
79
  @@loaded[code] ||= begin
80
80
  if exists? code
@@ -27,20 +27,35 @@ module R18n
27
27
  Date.new!(Date.send(:jd_to_ajd, jd, 0, 0), 0, Date::ITALY)
28
28
  end
29
29
 
30
- HTML_ENTRIES = { '&'=>'&amp;', '<'=>'&lt;', '>'=>'&gt;' }
30
+ HTML_ENTRIES = { '&' => '&amp;', '<' => '&lt;', '>' => '&gt;' }
31
31
 
32
32
  # Escape HTML entries (<, >, &). Copy from HAML helper.
33
33
  def self.escape_html(content)
34
34
  content.to_s.gsub(/[><&]/) { |s| HTML_ENTRIES[s] }
35
35
  end
36
36
 
37
- def self.hash_map(hash)
37
+ # Invokes +block+ once for each key and value of +hash+. Creates a new hash
38
+ # with the keys and values returned by the +block+.
39
+ def self.hash_map(hash, &block)
38
40
  result = {}
39
41
  hash.each_pair do |key, val|
40
- new_key, new_value = yield key, val
42
+ new_key, new_value = block.call(key, val)
41
43
  result[new_key] = new_value
42
44
  end
43
45
  result
44
46
  end
47
+
48
+ # Recursively hash merge.
49
+ def self.deep_merge!(a, b)
50
+ b.each_pair do |key, value|
51
+ another = a[key]
52
+ if another.nil?
53
+ a[key] = value
54
+ elsif another.is_a?(Hash) and value.is_a?(Hash)
55
+ a[key] = deep_merge!(another, value)
56
+ end
57
+ end
58
+ a
59
+ end
45
60
  end
46
61
  end
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  module R18n
3
- VERSION = '0.4.2' unless defined? R18n::VERSION
3
+ VERSION = '0.4.3' unless defined? R18n::VERSION
4
4
  end
@@ -43,15 +43,18 @@ module R18n
43
43
 
44
44
  # Array of locales, which has translations in +dir+.
45
45
  def available
46
- Dir.glob(File.join(@dir, '*.yml')).map do |i|
47
- R18n::Locale.load(File.basename(i, '.yml'))
48
- end
46
+ Dir.glob(File.join(@dir, '**/*.yml')).
47
+ map { |i| File.basename(i, '.yml') }.uniq.
48
+ map { |i| R18n::Locale.load(i) }
49
49
  end
50
50
 
51
51
  # Return Hash with translations for +locale+.
52
52
  def load(locale)
53
- file = File.join(@dir, "#{locale.code.downcase}.yml")
54
- transform(::YAML::load(IO.read(file)))
53
+ translations = {}
54
+ Dir.glob(File.join(@dir, "**/#{locale.code.downcase}.yml")).each do |i|
55
+ Utils.deep_merge!(translations, ::YAML::load_file(i))
56
+ end
57
+ transform(translations)
55
58
  end
56
59
 
57
60
  # YAML loader with same +dir+ will be have same +hash+.
data/locales/cs.rb CHANGED
@@ -7,15 +7,16 @@ module R18n
7
7
  :wday_names => %w{Neděle Pondělí Úterý Středa Čtvrtek Pátek Sobota},
8
8
  :wday_abbrs => %w{Ne Po Út St Čt Pá So},
9
9
 
10
- :month_names => %w{Ledna Února Března Dubna Května Června Července Srpna
11
- Září Října Listopadu Prosince},
12
- :month_abbrs => %w{Led Úno Bře Dub Kvě Čer Čvc Srp Zář Říj Lis Pro},
10
+ :month_names => %w{ledna února března dubna května června července srpna
11
+ září října listopadu prosince},
12
+ :month_abbrs => %w{led úno bře dub kvě čer čvc srp zář říj lis pro},
13
13
  :month_standalone => %w{Leden Únor Březen Duben Květen Červen Červenec
14
14
  Srpen Září Říjen Listopad Prosinec},
15
15
 
16
16
  :time_am => 'dop.',
17
17
  :time_pm => 'odp.',
18
- :date_format => '%d.%m.%Y',
18
+ :date_format => '%e. %m. %Y',
19
+ :full_format => '%e. %B',
19
20
 
20
21
  :number_decimal => ",",
21
22
  :number_group => " "
data/locales/sk.rb ADDED
@@ -0,0 +1,38 @@
1
+ # encoding: utf-8
2
+ module R18n
3
+ class Locales::Sk < Locale
4
+ set :title => 'Slovenský',
5
+ :sublocales => %w{cs cz en},
6
+
7
+ :wday_names => %w{Nedeľa Pondelok Utorok Streda Štvrtok Piatok Sobota},
8
+ :wday_abbrs => %w{Ne Po Ut St Št Pi So},
9
+
10
+ :month_names => %w{januára februára marca apríla mája júna júla augusta
11
+ septembra októbra novembra decembra},
12
+ :month_abbrs => %w{jan feb mar apr máj jún júl aug sep okt nov dec},
13
+ :month_standalone => %w{Január Február Marec Apríl Máj Jún Júl August
14
+ September Október November December},
15
+
16
+ :time_am => 'dop.',
17
+ :time_pm => 'odp.',
18
+ :date_format => '%d.%m.%Y',
19
+ :full_format => '%e. %B',
20
+
21
+ :number_decimal => ",",
22
+ :number_group => " "
23
+
24
+ def pluralize(n)
25
+ case n
26
+ when 0
27
+ 0
28
+ when 1
29
+ 1
30
+ when 2..4
31
+ 2
32
+ else
33
+ 'n'
34
+ end
35
+ end
36
+ end
37
+ end
38
+
data/spec/i18n_spec.rb CHANGED
@@ -4,7 +4,6 @@ require File.join(File.dirname(__FILE__), 'spec_helper')
4
4
  describe R18n::I18n do
5
5
  before do
6
6
  @extension_places = R18n.extension_places.clone
7
- R18n.cache.clear
8
7
  end
9
8
 
10
9
  after do
@@ -194,7 +193,7 @@ describe R18n::I18n do
194
193
  end
195
194
 
196
195
  it "should return first locale with locale file" do
197
- i18n = R18n::I18n.new(['no-LC', 'ru', 'en'], DIR)
196
+ i18n = R18n::I18n.new(['no-TR', 'no-LC', 'ru', 'en'], DIR)
198
197
  i18n.locale.should == R18n::Locale.load('no-LC')
199
198
  i18n.locale.base.should == R18n::Locale.load('ru')
200
199
  end
data/spec/locale_spec.rb CHANGED
@@ -154,5 +154,9 @@ describe R18n::Locale do
154
154
  upcase.code.should == 'no-lc'
155
155
  downcase.code.should == 'no-lc'
156
156
  end
157
+
158
+ it "should load locale with underscore" do
159
+ R18n::Locale.load('no_LC').code.should == 'no-lc'
160
+ end
157
161
 
158
162
  end
@@ -3,22 +3,22 @@ require File.join(File.dirname(__FILE__), '..', '..', 'locales', 'cs')
3
3
 
4
4
  describe R18n::Locales::Cs do
5
5
  it "should use Czech pluralization" do
6
- ru = R18n::Locale.load('cs')
7
- ru.pluralize(0).should == 0
8
- ru.pluralize(1).should == 1
6
+ cs = R18n::Locale.load('cs')
7
+ cs.pluralize(0).should == 0
8
+ cs.pluralize(1).should == 1
9
9
 
10
- ru.pluralize(2).should == 2
11
- ru.pluralize(3).should == 2
12
- ru.pluralize(4).should == 2
10
+ cs.pluralize(2).should == 2
11
+ cs.pluralize(3).should == 2
12
+ cs.pluralize(4).should == 2
13
13
 
14
- ru.pluralize(5).should == 'n'
15
- ru.pluralize(21).should == 'n'
16
- ru.pluralize(11).should == 'n'
17
- ru.pluralize(12).should == 'n'
18
- ru.pluralize(22).should == 'n'
19
- ru.pluralize(57).should == 'n'
20
- ru.pluralize(101).should == 'n'
21
- ru.pluralize(102).should == 'n'
22
- ru.pluralize(111).should == 'n'
14
+ cs.pluralize(5).should == 'n'
15
+ cs.pluralize(21).should == 'n'
16
+ cs.pluralize(11).should == 'n'
17
+ cs.pluralize(12).should == 'n'
18
+ cs.pluralize(22).should == 'n'
19
+ cs.pluralize(57).should == 'n'
20
+ cs.pluralize(101).should == 'n'
21
+ cs.pluralize(102).should == 'n'
22
+ cs.pluralize(111).should == 'n'
23
23
  end
24
24
  end
@@ -0,0 +1,24 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper')
2
+ require File.join(File.dirname(__FILE__), '..', '..', 'locales', 'sk')
3
+
4
+ describe R18n::Locales::Sk do
5
+ it "should use Slovak pluralization" do
6
+ sk = R18n::Locale.load('Sk')
7
+ sk.pluralize(0).should == 0
8
+ sk.pluralize(1).should == 1
9
+
10
+ sk.pluralize(2).should == 2
11
+ sk.pluralize(3).should == 2
12
+ sk.pluralize(4).should == 2
13
+
14
+ sk.pluralize(5).should == 'n'
15
+ sk.pluralize(21).should == 'n'
16
+ sk.pluralize(11).should == 'n'
17
+ sk.pluralize(12).should == 'n'
18
+ sk.pluralize(22).should == 'n'
19
+ sk.pluralize(57).should == 'n'
20
+ sk.pluralize(101).should == 'n'
21
+ sk.pluralize(102).should == 'n'
22
+ sk.pluralize(111).should == 'n'
23
+ end
24
+ end
data/spec/r18n_spec.rb CHANGED
@@ -44,5 +44,23 @@ describe R18n do
44
44
  it "should convert Time to Date" do
45
45
  R18n::Utils.to_date(Time.now).should == Date.today
46
46
  end
47
+
48
+ it "should map hash" do
49
+ R18n::Utils.hash_map({'a' => 1, 'b' => 2}) { |k, v| [k + 'a', v + 1] }.
50
+ should == { 'aa' => 2, 'ba' => 3 }
51
+ end
52
+
53
+ it "should merge hash recursively" do
54
+ a = { :a => 1,
55
+ :b => {:ba => 1, :bb => 1},
56
+ :c => 1 }
57
+ b = { :b => {:bb => 2, :bc => 2},
58
+ :c => 2 }
59
+
60
+ R18n::Utils.deep_merge!(a, b)
61
+ a.should == { :a => 1,
62
+ :b => { :ba => 1, :bb => 1, :bc => 2 },
63
+ :c => 1 }
64
+ end
47
65
 
48
66
  end
data/spec/spec_helper.rb CHANGED
@@ -6,9 +6,14 @@ dir = Pathname(__FILE__).dirname
6
6
 
7
7
  require dir + '../lib/r18n-core'
8
8
 
9
- DIR = dir + 'translations/general' unless defined? DIR
10
- TWO = dir + 'translations/two' unless defined? TWO
11
- EXT = R18n::Loader::YAML.new(dir + 'translations/extension') unless defined? EXT
9
+ TRANSALTIONS = dir + 'translations'
10
+ DIR = TRANSALTIONS + 'general' unless defined? DIR
11
+ TWO = TRANSALTIONS + 'two' unless defined? TWO
12
+ EXT = R18n::Loader::YAML.new(TRANSALTIONS + 'extension') unless defined? EXT
13
+
14
+ Spec::Runner.configure do |config|
15
+ config.before { R18n.cache.clear }
16
+ end
12
17
 
13
18
  gem 'maruku'
14
19
  gem 'RedCloth'
@@ -31,4 +31,18 @@ describe R18n::Loader::YAML do
31
31
  @loader.hash.should == R18n::Loader::YAML.new(DIR).hash
32
32
  end
33
33
 
34
+ it "should load in dir recursively" do
35
+ loader = R18n::Loader::YAML.new(TRANSALTIONS)
36
+ loader.available.should =~ [R18n::Locale.load('ru'),
37
+ R18n::Locale.load('en'),
38
+ R18n::Locale.load('fr'),
39
+ R18n::Locale.load('no-tr'),
40
+ R18n::Locale.load('no-lc')]
41
+
42
+ translation = loader.load(R18n::Locale.load('en'))
43
+ translation['one'].should == 'One'
44
+ translation['in']['two'].should == 'Two'
45
+ translation['ext'].should == 'Extension'
46
+ end
47
+
34
48
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: r18n-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
4
+ version: 0.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrey "A.I." Sitnik
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-02-19 00:00:00 +03:00
12
+ date: 2010-02-26 00:00:00 +03:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -28,6 +28,7 @@ files:
28
28
  - base/it.yml
29
29
  - base/ru.yml
30
30
  - base/pl.yml
31
+ - base/sk.yml
31
32
  - base/de.yml
32
33
  - base/eo.yml
33
34
  - base/cs.yml
@@ -58,6 +59,7 @@ files:
58
59
  - locales/zh.rb
59
60
  - locales/fr.rb
60
61
  - locales/ru.rb
62
+ - locales/sk.rb
61
63
  - locales/pt-br.rb
62
64
  - locales/de.rb
63
65
  - locales/eo.rb
@@ -96,6 +98,7 @@ test_files:
96
98
  - spec/translated_spec.rb
97
99
  - spec/locales/it_spec.rb
98
100
  - spec/locales/fr_spec.rb
101
+ - spec/locales/sk_spec.rb
99
102
  - spec/locales/pl_spec.rb
100
103
  - spec/locales/cs_spec.rb
101
104
  - spec/locales/ru_spec.rb