gettext_i18n_rails 0.2.8 → 0.2.9

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/Rakefile CHANGED
@@ -1,5 +1,5 @@
1
1
  require 'spec/rake/spectask'
2
- Spec::Rake::SpecTask.new {|t| t.spec_opts = ['--color']}
2
+ Spec::Rake::SpecTask.new {|t| t.spec_opts = ['--color', '--backtrace']}
3
3
 
4
4
  task :default do
5
5
  puts `rake spec VERSION=2.3.9 RSPEC_COLOR=1`
@@ -20,4 +20,4 @@ begin
20
20
  Jeweler::GemcutterTasks.new
21
21
  rescue LoadError
22
22
  puts "Jeweler, or one of its dependencies, is not available. Install it with: sudo gem install jeweler"
23
- end
23
+ end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.8
1
+ 0.2.9
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{gettext_i18n_rails}
8
- s.version = "0.2.8"
8
+ s.version = "0.2.9"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Michael Grosser"]
12
- s.date = %q{2010-10-13}
12
+ s.date = %q{2010-10-20}
13
13
  s.email = %q{grosser.michael@gmail.com}
14
14
  s.files = [
15
15
  ".gitignore",
@@ -14,21 +14,10 @@ module GettextI18nRails
14
14
  end
15
15
 
16
16
  def translate(locale, key, options)
17
- flat_key = flatten_key key, options
18
- if FastGettext.key_exist?(flat_key)
19
- raise "no yet build..." if options[:locale]
20
- _(flat_key)
17
+ if gettext_key = gettext_key(key, options)
18
+ translation = FastGettext._(gettext_key)
19
+ interpolate(translation, options)
21
20
  else
22
- if self.class.translate_defaults
23
- [*options[:default]].each do |default|
24
- #try the more specific key first e.g. 'activerecord.errors.my custom message'
25
- flat_key = flatten_key default, options
26
- return FastGettext._(flat_key) if FastGettext.key_exist?(flat_key)
27
-
28
- #try the short key thereafter e.g. 'my custom message'
29
- return FastGettext._(default) if FastGettext.key_exist?(default)
30
- end
31
- end
32
21
  backend.translate locale, key, options
33
22
  end
34
23
  end
@@ -39,6 +28,27 @@ module GettextI18nRails
39
28
 
40
29
  protected
41
30
 
31
+ def gettext_key(key, options)
32
+ flat_key = flatten_key key, options
33
+ if FastGettext.key_exist?(flat_key)
34
+ flat_key
35
+ elsif self.class.translate_defaults
36
+ [*options[:default]].each do |default|
37
+ #try the scoped(more specific) key first e.g. 'activerecord.errors.my custom message'
38
+ flat_key = flatten_key default, options
39
+ return flat_key if FastGettext.key_exist?(flat_key)
40
+
41
+ #try the short key thereafter e.g. 'my custom message'
42
+ return default if FastGettext.key_exist?(default)
43
+ end
44
+ return nil
45
+ end
46
+ end
47
+
48
+ def interpolate(string, values)
49
+ string % values.except(*I18n::Backend::Base::RESERVED_KEYS)
50
+ end
51
+
42
52
  def flatten_key key, options
43
53
  scope = [*(options[:scope] || [])]
44
54
  scope.empty? ? key.to_s : "#{scope*'.'}.#{key}"
@@ -21,4 +21,18 @@ module I18n
21
21
  end
22
22
  end
23
23
  end
24
+
25
+ # backport I18n.with_locale if it does not exist
26
+ unless respond_to?(:with_locale)
27
+ # Executes block with given I18n.locale set.
28
+ def with_locale(tmp_locale = nil)
29
+ if tmp_locale
30
+ current_locale = self.locale
31
+ self.locale = tmp_locale
32
+ end
33
+ yield
34
+ ensure
35
+ self.locale = current_locale if tmp_locale
36
+ end
37
+ end
24
38
  end
@@ -12,29 +12,34 @@ describe GettextI18nRails::Backend do
12
12
  subject.available_locales.should == [:xxx]
13
13
  end
14
14
 
15
- it "and_return an epmty array when FastGettext.available_locales is nil" do
16
- FastGettext.should_receive(:available_locales)
15
+ it "and returns an empty array when FastGettext.available_locales is nil" do
16
+ FastGettext.should_receive(:available_locales).and_return nil
17
17
  subject.available_locales.should == []
18
18
  end
19
19
  end
20
20
 
21
21
  describe :translate do
22
- it "uses gettext when the key is translateable" do
23
- FastGettext.should_receive(:current_repository).and_return 'xy.z.u'=>'a'
22
+ it "uses gettext when the key is translatable" do
23
+ FastGettext.stub(:current_repository).and_return 'xy.z.u'=>'a'
24
24
  subject.translate('xx','u',:scope=>['xy','z']).should == 'a'
25
25
  end
26
26
 
27
+ it "interpolates options" do
28
+ FastGettext.stub(:current_repository).and_return 'ab.c'=>'a%{a}b'
29
+ subject.translate('xx','c',:scope=>['ab'], :a => 'X').should == 'aXb'
30
+ end
31
+
27
32
  it "can translate with gettext using symbols" do
28
- FastGettext.should_receive(:current_repository).and_return 'xy.z.v'=>'a'
33
+ FastGettext.stub(:current_repository).and_return 'xy.z.v'=>'a'
29
34
  subject.translate('xx',:v ,:scope=>['xy','z']).should == 'a'
30
35
  end
31
36
 
32
37
  it "can translate with gettext using a flat scope" do
33
- FastGettext.should_receive(:current_repository).and_return 'xy.z.x'=>'a'
38
+ FastGettext.stub(:current_repository).and_return 'xy.z.x'=>'a'
34
39
  subject.translate('xx',:x ,:scope=>'xy.z').should == 'a'
35
40
  end
36
41
 
37
- it "uses the super when the key is not translateable" do
42
+ it "uses the super when the key is not translatable" do
38
43
  lambda{subject.translate('xx','y',:scope=>['xy','z'])}.should raise_error(I18n::MissingTranslationData)
39
44
  end
40
45
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 8
9
- version: 0.2.8
8
+ - 9
9
+ version: 0.2.9
10
10
  platform: ruby
11
11
  authors:
12
12
  - Michael Grosser
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-10-13 00:00:00 +02:00
17
+ date: 2010-10-20 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency