gettext_i18n_rails 1.0.2 → 1.0.3

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.
@@ -1,13 +0,0 @@
1
- $LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
2
- name = "gettext_i18n_rails"
3
- require "#{name}/version"
4
-
5
- Gem::Specification.new name, GettextI18nRails::VERSION do |s|
6
- s.summary = "Simple FastGettext Rails integration."
7
- s.authors = ["Michael Grosser"]
8
- s.email = "michael@grosser.it"
9
- s.homepage = "http://github.com/grosser/#{name}"
10
- s.files = `git ls-files`.split("\n")
11
- s.license = "MIT"
12
- s.add_runtime_dependency "fast_gettext", ">=0.4.8"
13
- end
data/init.rb DELETED
@@ -1,14 +0,0 @@
1
- begin
2
- require 'config/initializers/session_store'
3
- rescue LoadError
4
- # weird bug, when run with rake rails reports error that session
5
- # store is not configured, this fixes it somewhat...
6
- end
7
-
8
- if Rails::VERSION::MAJOR > 2
9
- require 'gettext_i18n_rails'
10
- else
11
- #requires fast_gettext to be present.
12
- #We give rails a chance to install it using rake gems:install, by loading it later.
13
- config.after_initialize { require 'gettext_i18n_rails' }
14
- end
@@ -1,54 +0,0 @@
1
- require "spec_helper"
2
-
3
- FastGettext.silence_errors
4
-
5
- describe ActionController::Base do
6
- def reset!
7
- fake_session = {}
8
- @c.stub(:session).and_return fake_session
9
- fake_cookies = {}
10
- @c.stub(:cookies).and_return fake_cookies
11
- @c.params = {}
12
- @c.request = double(:env => {})
13
- end
14
-
15
- before do
16
- #controller
17
- @c = ActionController::Base.new
18
- reset!
19
-
20
- #locale
21
- FastGettext.available_locales = nil
22
- FastGettext.locale = I18n.default_locale = 'fr'
23
- FastGettext.available_locales = ['fr','en']
24
- end
25
-
26
- it "changes the locale" do
27
- @c.params = {:locale=>'en'}
28
- @c.set_gettext_locale
29
- @c.session[:locale].should == 'en'
30
- FastGettext.locale.should == 'en'
31
- end
32
-
33
- it "stays with default locale when none was found" do
34
- @c.set_gettext_locale
35
- @c.session[:locale].should == 'fr'
36
- FastGettext.locale.should == 'fr'
37
- end
38
-
39
- it "locale isn't cached over request" do
40
- @c.params = {:locale=>'en'}
41
- @c.set_gettext_locale
42
- @c.session[:locale].should == 'en'
43
-
44
- reset!
45
- @c.set_gettext_locale
46
- @c.session[:locale].should == 'fr'
47
- end
48
-
49
- it "reads the locale from the HTTP_ACCEPT_LANGUAGE" do
50
- @c.request.stub(:env).and_return 'HTTP_ACCEPT_LANGUAGE'=>'de-de,de;q=0.8,en-us;q=0.5,en;q=0.3'
51
- @c.set_gettext_locale
52
- FastGettext.locale.should == 'en'
53
- end
54
- end
@@ -1,20 +0,0 @@
1
- # encoding: utf-8
2
- require "spec_helper"
3
-
4
- if ActiveRecord::VERSION::MAJOR >= 3
5
- require "gettext_i18n_rails/active_model/name"
6
-
7
- describe ActiveModel::Name do
8
- before do
9
- FastGettext.current_cache = {}
10
- end
11
-
12
- describe 'human' do
13
- it "is translated through FastGettext" do
14
- name = ActiveModel::Name.new(CarSeat)
15
- name.should_receive(:_).with('Car seat').and_return('Autositz')
16
- name.human.should == 'Autositz'
17
- end
18
- end
19
- end
20
- end
@@ -1,71 +0,0 @@
1
- # coding: utf-8
2
- require "spec_helper"
3
-
4
- describe ActiveRecord::Base do
5
- before do
6
- FastGettext.current_cache = {}
7
- end
8
-
9
- describe :human_name do
10
- it "is translated through FastGettext" do
11
- CarSeat.should_receive(:_).with('Car seat').and_return('Autositz')
12
- CarSeat.human_name.should == 'Autositz'
13
- end
14
- end
15
-
16
- describe :human_attribute_name do
17
- it "translates attributes through FastGettext" do
18
- CarSeat.should_receive(:s_).with('CarSeat|Seat color').and_return('Sitz farbe')
19
- CarSeat.human_attribute_name(:seat_color).should == 'Sitz farbe'
20
- end
21
-
22
- it "translates nested attributes through FastGettext" do
23
- CarSeat.should_receive(:s_).with('CarSeat|Parts|Name').and_return('Handle')
24
- CarSeat.human_attribute_name(:"parts.name").should == 'Handle'
25
- end
26
-
27
- it "translates attributes of STI classes through FastGettext" do
28
- StiChild.should_receive(:s_).with('StiParent|Child attribute').and_return('Kinderattribut')
29
- StiChild.human_attribute_name(:child_attribute).should == 'Kinderattribut'
30
- end
31
-
32
- it "translates attributes of concrete children of abstract parent classes" do
33
- ConcreteChildClass.should_receive(:s_).with('AbstractParentClass|Child attribute').and_return('Kinderattribut')
34
- ConcreteChildClass.human_attribute_name(:child_attribute).should == 'Kinderattribut'
35
- end
36
- end
37
-
38
- describe :gettext_translation_for_attribute_name do
39
- it "translates foreign keys to model name keys" do
40
- Part.gettext_translation_for_attribute_name(:car_seat_id).should == 'Car seat'
41
- end
42
- end
43
-
44
- describe 'error messages' do
45
- let(:model){
46
- c = CarSeat.new
47
- c.valid?
48
- c
49
- }
50
-
51
- it "translates error messages" do
52
- FastGettext.stub(:current_repository).and_return('translate me'=>"Übersetz mich!")
53
- FastGettext._('translate me').should == "Übersetz mich!"
54
- model.errors.full_messages.should == ["Seat color Übersetz mich!"]
55
- end
56
-
57
- it "translates scoped error messages" do
58
- pending 'scope is no longer added in 3.x' if ActiveRecord::VERSION::MAJOR >= 3
59
- FastGettext.stub(:current_repository).and_return('activerecord.errors.translate me'=>"Übersetz mich!")
60
- FastGettext._('activerecord.errors.translate me').should == "Übersetz mich!"
61
- model.errors.full_messages.should == ["Seat color Übersetz mich!"]
62
- end
63
-
64
- it "translates error messages with %{fn}" do
65
- pending
66
- FastGettext.stub(:current_repository).and_return('translate me'=>"Übersetz %{fn} mich!")
67
- FastGettext._('translate me').should == "Übersetz %{fn} mich!"
68
- model.errors[:seat_color].should == ["Übersetz car_seat mich!"]
69
- end
70
- end
71
- end
@@ -1,106 +0,0 @@
1
- # encoding: UTF-8
2
- require "spec_helper"
3
-
4
- FastGettext.silence_errors
5
-
6
- describe GettextI18nRails::Backend do
7
- before do
8
- FastGettext.current_cache = {}
9
- end
10
-
11
- it "redirects calls to another I18n backend" do
12
- subject.backend.should_receive(:xxx).with(1,2)
13
- subject.xxx(1,2)
14
- end
15
-
16
- describe :available_locales do
17
- it "maps them to FastGettext" do
18
- FastGettext.should_receive(:available_locales).and_return [:xxx]
19
- subject.available_locales.should == [:xxx]
20
- end
21
-
22
- it "and returns an empty array when FastGettext.available_locales is nil" do
23
- FastGettext.should_receive(:available_locales).and_return nil
24
- subject.available_locales.should == []
25
- end
26
- end
27
-
28
- describe :translate do
29
- it "uses gettext when the key is translatable" do
30
- FastGettext.should_receive(:current_repository).and_return 'xy.z.u'=>'a'
31
- subject.translate('xx','u',:scope=>['xy','z']).should == 'a'
32
- end
33
-
34
- it "interpolates options" do
35
- FastGettext.should_receive(:current_repository).and_return 'ab.c'=>'a%{a}b'
36
- subject.translate('xx','c',:scope=>['ab'], :a => 'X').should == 'aXb'
37
- end
38
-
39
- it "will not try and interpolate when there are no options given" do
40
- FastGettext.should_receive(:current_repository).and_return 'ab.d' => 'a%{a}b'
41
- subject.translate('xx', 'd', :scope=>['ab']).should == 'a%{a}b'
42
- end
43
-
44
- it "uses plural translation if count is given" do
45
- repo = {'ab.e' => 'existing'}
46
- repo.should_receive(:plural).and_return %w(single plural)
47
- repo.stub(:pluralisation_rule).and_return nil
48
- FastGettext.stub(:current_repository).and_return repo
49
- subject.translate('xx', 'ab.e', :count => 1).should == 'single'
50
- subject.translate('xx', 'ab.e', :count => 2).should == 'plural'
51
- end
52
-
53
- it "interpolates a count without plural translations" do
54
- repo = {'ab.e' => 'existing %{count}'}
55
- repo.should_receive(:plural).and_return []
56
- repo.stub(:pluralisation_rule).and_return lambda { |i| true }
57
- FastGettext.stub(:current_repository).and_return repo
58
- subject.translate('xx', 'ab.e', :count => 1).should == 'existing 1'
59
- end
60
-
61
- it "can translate with gettext using symbols" do
62
- FastGettext.should_receive(:current_repository).and_return 'xy.z.v'=>'a'
63
- subject.translate('xx',:v ,:scope=>['xy','z']).should == 'a'
64
- end
65
-
66
- it "can translate with gettext using a flat scope" do
67
- FastGettext.should_receive(:current_repository).and_return 'xy.z.x'=>'a'
68
- subject.translate('xx',:x ,:scope=>'xy.z').should == 'a'
69
- end
70
-
71
- it "passes non-gettext keys to default backend" do
72
- subject.backend.should_receive(:translate).with('xx', 'c', {}).and_return 'd'
73
- # TODO track down why this is called 3 times on 1.8 (only 1 time on 1.9)
74
- FastGettext.stub(:current_repository).and_return 'a'=>'b'
75
- subject.translate('xx', 'c', {}).should == 'd'
76
- end
77
-
78
- if RUBY_VERSION > "1.9"
79
- it "produces UTF-8 when not using FastGettext to fix weird encoding bug" do
80
- subject.backend.should_receive(:translate).with('xx', 'c', {}).and_return 'ü'.force_encoding("US-ASCII")
81
- FastGettext.should_receive(:current_repository).and_return 'a'=>'b'
82
- result = subject.translate('xx', 'c', {})
83
- result.should == 'ü'
84
- end
85
-
86
- it "does not force_encoding on non-strings" do
87
- subject.backend.should_receive(:translate).with('xx', 'c', {}).and_return ['aa']
88
- FastGettext.should_receive(:current_repository).and_return 'a'=>'b'
89
- result = subject.translate('xx', 'c', {})
90
- result.should == ['aa']
91
- end
92
- end
93
-
94
- # TODO NameError is raised <-> wtf ?
95
- xit "uses the super when the key is not translatable" do
96
- lambda{subject.translate('xx','y',:scope=>['xy','z'])}.should raise_error(I18n::MissingTranslationData)
97
- end
98
- end
99
-
100
- describe :interpolate do
101
- it "act as an identity function for an array" do
102
- translation = [:month, :day, :year]
103
- subject.send(:interpolate, translation, {}).should == translation
104
- end
105
- end
106
- end
@@ -1,50 +0,0 @@
1
- require "spec_helper"
2
- require "gettext_i18n_rails/haml_parser"
3
-
4
- describe GettextI18nRails::HamlParser do
5
- let(:parser){ GettextI18nRails::HamlParser }
6
-
7
- describe "#target?" do
8
- it "targets .haml" do
9
- parser.target?('foo/bar/xxx.haml').should == true
10
- end
11
-
12
- it "does not target anything else" do
13
- parser.target?('foo/bar/xxx.erb').should == false
14
- end
15
- end
16
-
17
- describe "#parse" do
18
- it "finds messages in haml" do
19
- with_file '= _("xxxx")' do |path|
20
- parser.parse(path, []).should == [
21
- ["xxxx", "#{path}:1"]
22
- ]
23
- end
24
- end
25
-
26
- it "finds messages with concatenation" do
27
- with_file '= _("xxxx" + "yyyy" + "zzzz")' do |path|
28
- parser.parse(path, []).should == [
29
- ["xxxxyyyyzzzz", "#{path}:1"]
30
- ]
31
- end
32
- end
33
-
34
- it "should parse the 1.9 if ruby_version is 1.9" do
35
- if RUBY_VERSION =~ /^1\.9/
36
- with_file '= _("xxxx", x: 1)' do |path|
37
- parser.parse(path, []).should == [
38
- ["xxxx", "#{path}:1"]
39
- ]
40
- end
41
- end
42
- end
43
-
44
- it "does not find messages in text" do
45
- with_file '_("xxxx")' do |path|
46
- parser.parse(path, []).should == []
47
- end
48
- end
49
- end
50
- end
@@ -1,42 +0,0 @@
1
- # coding: utf-8
2
- require "spec_helper"
3
- require "gettext_i18n_rails/model_attributes_finder"
4
-
5
- if Rails::VERSION::MAJOR > 2
6
- module Test
7
- class Application < Rails::Application
8
- end
9
- end
10
- end
11
-
12
- describe GettextI18nRails::ModelAttributesFinder do
13
- let(:finder) { GettextI18nRails::ModelAttributesFinder.new }
14
-
15
- before do
16
- Rails.application rescue nil
17
- end
18
-
19
- # Rails < 3.0 doesn't have DescendantsTracker.
20
- # Instead of iterating over ObjectSpace (slow) the decision was made NOT to support
21
- # class hierarchies with abstract base classes in Rails 2.x
22
- describe :find do
23
- it "returns all AR models" do
24
- keys = finder.find({}).keys
25
- if Rails::VERSION::MAJOR > 2
26
- keys.should == [AbstractParentClass, CarSeat, NotConventional, Part, StiParent]
27
- else
28
- keys.should == [CarSeat, Part, StiParent]
29
- end
30
- end
31
-
32
- it "returns all columns for each model" do
33
- attributes = finder.find({})
34
- attributes[CarSeat].should == ['id', 'seat_color']
35
- attributes[NotConventional].should == ['id', 'name'] if Rails::VERSION::MAJOR > 2
36
- attributes[Part].should == ['car_seat_id', 'id', 'name']
37
- attributes[StiParent].should == ['child_attribute', 'id', 'type']
38
- attributes[AbstractParentClass].should ==
39
- ['another_child_attribute', 'child_attribute', 'id'] if Rails::VERSION::MAJOR > 2
40
- end
41
- end
42
- end
@@ -1,40 +0,0 @@
1
- require "spec_helper"
2
- require "gettext_i18n_rails/slim_parser"
3
-
4
- describe GettextI18nRails::SlimParser do
5
- let(:parser){ GettextI18nRails::SlimParser }
6
-
7
- describe "#target?" do
8
- it "targets .slim" do
9
- parser.target?('foo/bar/xxx.slim').should == true
10
- end
11
-
12
- it "does not target anything else" do
13
- parser.target?('foo/bar/xxx.erb').should == false
14
- end
15
- end
16
-
17
- describe "#parse" do
18
- it "finds messages in slim" do
19
- with_file 'div = _("xxxx")' do |path|
20
- parser.parse(path, []).should == [
21
- ["xxxx", "#{path}:1"]
22
- ]
23
- end
24
- end
25
-
26
- xit "can parse 1.9 syntax" do
27
- with_file 'div = _("xxxx", foo: :bar)' do |path|
28
- parser.parse(path, []).should == [
29
- ["xxxx", "#{path}:1"]
30
- ]
31
- end
32
- end
33
-
34
- it "does not find messages in text" do
35
- with_file 'div _("xxxx")' do |path|
36
- parser.parse(path, []).should == []
37
- end
38
- end
39
- end
40
- end
@@ -1,32 +0,0 @@
1
- require "spec_helper"
2
- require "gettext_i18n_rails/string_interpolate_fix"
3
-
4
- describe "String#%" do
5
- it "is not safe if it was not safe" do
6
- result = ("<br/>%{x}" % {:x => 'a'})
7
- result.should == '<br/>a'
8
- result.html_safe?.should == false
9
- end
10
-
11
- xit "stays safe if it was safe" do
12
- result = ("<br/>%{x}".html_safe % {:x => 'a'})
13
- result.should == '<br/>a'
14
- result.html_safe?.should == true
15
- end
16
-
17
- xit "escapes unsafe added to safe" do
18
- result = ("<br/>%{x}".html_safe % {:x => '<br/>'})
19
- result.should == '<br/>&lt;br/&gt;'
20
- result.html_safe?.should == true
21
- end
22
-
23
- it "does not escape unsafe if it was unsafe" do
24
- result = ("<br/>%{x}" % {:x => '<br/>'})
25
- result.should == '<br/><br/>'
26
- result.html_safe?.should == false
27
- end
28
-
29
- it "does not break array replacement" do
30
- "%ssd" % ['a'].should == "asd"
31
- end
32
- end