r18n-core 1.0.1 → 1.1.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.
data/ChangeLog CHANGED
@@ -1,3 +1,13 @@
1
+ == 1.1.0 (Leipzig)
2
+ * A lot of fixes in Rails I18n compatibility (thanks for Stephan Schubert).
3
+ * Return Untranslted, when user try to call another translation key on
4
+ already translated string.
5
+ * Add Translation#to_hash to get raw translation.
6
+ * Add Translation#inspect to easy debug.
7
+ * Return translation, when pluralization filter didn’t get count.
8
+ * Set R18n backend on Rails plugin init, to use it in console.
9
+ * Allow to use Fixnum in translation keys.
10
+
1
11
  == 1.0.1 (Phuket Town)
2
12
  * Fix translation reloading in Rails and Sinatra.
3
13
  * Use global R18n settings for Sinatra extension.
data/Rakefile CHANGED
@@ -18,7 +18,7 @@ RSpec::Core::RakeTask.new
18
18
 
19
19
  task :spec_syck do
20
20
  ENV['test_syck'] = '1'
21
- sh "#{RUBY} -S rake spec", :verbose => false
21
+ Rake::Task['spec'].execute
22
22
  end
23
23
 
24
24
  require 'yard'
@@ -213,6 +213,10 @@ module R18n
213
213
  eval("proc { #{content} }").call(*params)
214
214
  end
215
215
 
216
+ # Class to mark unpluralized translation.
217
+ class UnpluralizetedTranslation < Translation
218
+ end
219
+
216
220
  Filters.add('pl', :pluralization) do |content, config, param|
217
221
  param = param.to_i if param.is_a? Float
218
222
  if param.is_a? Numeric
@@ -220,7 +224,8 @@ module R18n
220
224
  type = 'n' if not content.has_key? type
221
225
  content[type]
222
226
  else
223
- content
227
+ UnpluralizetedTranslation.new(config[:locale], config[:path],
228
+ :locale => config[:locale], :translations => content)
224
229
  end
225
230
  end
226
231
 
@@ -29,10 +29,11 @@ module R18n
29
29
 
30
30
  # Returns a new string object containing a copy of +str+, which translated
31
31
  # for +path+ to +locale+
32
- def initialize(str, locale, path)
32
+ def initialize(str, locale, path, filters = nil)
33
33
  super(str)
34
- @locale = locale
35
- @path = path
34
+ @filters = filters
35
+ @locale = locale
36
+ @path = path
36
37
  end
37
38
 
38
39
  # Return self for translated string.
@@ -55,8 +56,20 @@ module R18n
55
56
  if respond_to? :html_safe
56
57
  html_safe
57
58
  else
58
- self
59
+ String.new(self)
59
60
  end
60
61
  end
62
+
63
+ # Return untranslated for deeper node `key`. It is in separated methods to
64
+ # be used in R18n I18n backend.
65
+ def get_untranslated(key)
66
+ translated = @path.empty? ? '' : "#{@path}."
67
+ Untranslated.new(translated, key, @locale, @filters)
68
+ end
69
+
70
+ # Return untranslated, when user try to go deeper in translation.
71
+ def method_missing(name, *params)
72
+ get_untranslated(name.to_s)
73
+ end
61
74
  end
62
75
  end
@@ -94,7 +94,7 @@ module R18n
94
94
  when String
95
95
  c = { :locale => locale, :path => path }
96
96
  v = @filters.process_string(:passive, value, c, [])
97
- value = TranslatedString.new(v, locale, path)
97
+ value = TranslatedString.new(v, locale, path, @filters)
98
98
  when Typed
99
99
  value.locale = locale
100
100
  value.path = path
@@ -115,9 +115,25 @@ module R18n
115
115
  [@path, '', @path])
116
116
  end
117
117
 
118
+ # Override inspect to easy debug.
119
+ def inspect
120
+ path = @path.empty? ? 'root' : "`#{@path}`"
121
+ "Translation #{path} for #{@locale.code} #{@data.inspect}"
122
+ end
123
+
118
124
  # Return current translation keys.
125
+ #
126
+ # Deprecated. Use <tt>to_hash.keys</tt>.
119
127
  def translation_keys
120
- @data.keys
128
+ to_hash.keys
129
+ end
130
+
131
+ # Return hash of current translation node.
132
+ def to_hash
133
+ Utils.hash_map(@data) do |key, value|
134
+ value = value.to_hash if value.is_a? Translation
135
+ [key, value]
136
+ end
121
137
  end
122
138
 
123
139
  # Return +default+.
@@ -130,7 +146,8 @@ module R18n
130
146
  # Translation can contain variable part. Just set is as <tt>%1</tt>,
131
147
  # <tt>%2</tt>, etc in translations file and set values in next +params+.
132
148
  def [](name, *params)
133
- value = @data[name.to_s]
149
+ name = name.to_s if not name.is_a? String and not name.is_a? Fixnum
150
+ value = @data[name]
134
151
  case value
135
152
  when TranslatedString
136
153
  @filters.process_string(:active, value, @path, params)
@@ -138,7 +155,7 @@ module R18n
138
155
  @filters.process_typed(:active, value, params)
139
156
  when nil
140
157
  translated = @path.empty? ? '' : "#{@path}."
141
- Untranslated.new(translated, name.to_s, @locale, @filters)
158
+ Untranslated.new(translated, name, @locale, @filters)
142
159
  else
143
160
  value
144
161
  end
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  module R18n
3
- VERSION = '1.0.1'.freeze unless defined? R18n::VERSION
3
+ VERSION = '1.1.0'.freeze unless defined? R18n::VERSION
4
4
  end
@@ -89,14 +89,19 @@ module R18n
89
89
  # Wrap YAML private types to Typed.
90
90
  def transform(a_hash)
91
91
  R18n::Utils.hash_map(a_hash) do |key, value|
92
- [key,
93
- case value
94
- when @private_type_class
95
- Typed.new(value.type_id, value.value)
96
- when Hash
97
- transform(value)
98
- else value
99
- end]
92
+ value = case value
93
+ when @private_type_class
94
+ v = value.value
95
+ if v.respond_to?(:force_encoding) and v.encoding != __ENCODING__
96
+ v = v.force_encoding(__ENCODING__)
97
+ end
98
+ Typed.new(value.type_id, v)
99
+ when Hash
100
+ transform(value)
101
+ else
102
+ value
103
+ end
104
+ [key, value]
100
105
  end
101
106
  end
102
107
  end
data/lib/r18n-core.rb CHANGED
@@ -29,9 +29,9 @@ require dir.join('locale').to_s
29
29
  require dir.join('unsupported_locale').to_s
30
30
  require dir.join('translated_string').to_s
31
31
  require dir.join('untranslated').to_s
32
+ require dir.join('translation').to_s
32
33
  require dir.join('filters').to_s
33
34
  require dir.join('filter_list').to_s
34
- require dir.join('translation').to_s
35
35
  require dir.join('yaml_loader').to_s
36
36
  require dir.join('i18n').to_s
37
37
  require dir.join('helpers').to_s
@@ -84,6 +84,8 @@ module R18n
84
84
  thread[:r18n_i18n] = thread[:r18n_setter] = @i18n = @setter = nil
85
85
  clear_cache!
86
86
  end
87
+
88
+ # Deprecated.
87
89
  alias :reset :reset!
88
90
 
89
91
  # Get the current thread.
data/spec/filters_spec.rb CHANGED
@@ -153,8 +153,10 @@ describe R18n::Filters do
153
153
  end
154
154
 
155
155
  it "shouldn't pluralize without first numeric parameter" do
156
- @i18n.files.should == { 1 => '1 file', 'n' => '%1 files' }
157
- @i18n.files('').should == { 1 => '1 file', 'n' => '%1 files' }
156
+ @i18n.files.should be_a(R18n::UnpluralizetedTranslation)
157
+ @i18n.files('').should be_a(R18n::UnpluralizetedTranslation)
158
+ @i18n.files[1].should == '1 file'
159
+ @i18n.files.n(5).should == '5 files'
158
160
  end
159
161
 
160
162
  it "should convert first float parameter to number" do
@@ -68,4 +68,36 @@ describe R18n::Translation do
68
68
  translation.count(5).should == 'many'
69
69
  end
70
70
 
71
+ it "should return hash of translations" do
72
+ i18n = R18n::I18n.new('en', DIR)
73
+ i18n.in.to_hash.should == {
74
+ 'another' => { 'level' => 'Hierarchical' }
75
+ }
76
+ end
77
+
78
+ it "should return untranslated, when we go deeper string" do
79
+ en = R18n.locale('en')
80
+ translation = R18n::Translation.new(en, '',
81
+ :locale => en, :translations => { 'a' => 'A' })
82
+
83
+ translation.a.b.should be_a(R18n::Untranslated)
84
+ translation.a.b.translated_path.should == 'a.'
85
+ translation.a.b.untranslated_path.should == 'b'
86
+ end
87
+
88
+ it "should inspect translation" do
89
+ en = R18n.locale('en')
90
+
91
+ translation = R18n::Translation.new(en, 'a',
92
+ :locale => en, :translations => { 'a' => 'A' })
93
+ translation.inspect.should == 'Translation `a` for en {"a"=>"A"}'
94
+
95
+ translation = R18n::Translation.new(en, '',
96
+ :locale => en, :translations => { 'a' => 'A' })
97
+ translation.inspect.should == 'Translation root for en {"a"=>"A"}'
98
+
99
+ translation = R18n::Translation.new(en, '')
100
+ translation.inspect.should == 'Translation root for en {}'
101
+ end
102
+
71
103
  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: 1.0.1
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-30 00:00:00.000000000 Z
12
+ date: 2012-07-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -297,7 +297,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
297
297
  version: '0'
298
298
  segments:
299
299
  - 0
300
- hash: -3931827469984591101
300
+ hash: 1266686224539252765
301
301
  required_rubygems_version: !ruby/object:Gem::Requirement
302
302
  none: false
303
303
  requirements:
@@ -306,7 +306,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
306
306
  version: '0'
307
307
  segments:
308
308
  - 0
309
- hash: -3931827469984591101
309
+ hash: 1266686224539252765
310
310
  requirements: []
311
311
  rubyforge_project:
312
312
  rubygems_version: 1.8.23