rails-localization 0.2.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,22 @@
1
+ module RailsLocalization
2
+ module Controller
3
+ def self.included(base)
4
+ base.class_eval do
5
+ around_filter :process_with_locale
6
+
7
+ protected
8
+
9
+ def process_with_locale
10
+ begin
11
+ if params.has_key?(:locale)
12
+ ::I18n.locale = params[:locale].blank? ? ::I18n.default_locale : params[:locale].to_s
13
+ end
14
+ yield
15
+ ensure
16
+ ::I18n.locale = ::I18n.default_locale
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,6 @@
1
+ require 'rails/engine'
2
+
3
+ module RailsLocalization
4
+ class Engine < ::Rails::Engine
5
+ end
6
+ end
@@ -0,0 +1,9 @@
1
+ module RailsLocalization
2
+ module I18n
3
+ def self.extended(base)
4
+ def base.locales
5
+ @locales ||= {}
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,12 @@
1
+ module RailsLocalization
2
+ module RouteSet
3
+ def self.included(base)
4
+ base.class_eval do
5
+ def url_for_with_locale(options = {})
6
+ url_for_without_locale RailsLocalization::UrlOptions.formatter(options)
7
+ end
8
+ alias_method_chain :url_for, :locale
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,15 @@
1
+ module ActionDispatch::Routing::Mapper::Localization
2
+ def self.included(base)
3
+ base.class_eval do
4
+ def localized(locales)
5
+ if @set == Rails.application.routes
6
+ I18n.locales[:main_app] = locales
7
+ else
8
+ sub_app_name = @scope[:module] || :main_app
9
+ I18n.locales[sub_app_name] = locales
10
+ end
11
+ scope("(:locale)", constraints: {locale: /#{locales.keys.join('|')}/}, defaults: {locale: ""}) { yield }
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,34 @@
1
+ module RailsLocalization
2
+ module UrlOptions
3
+ def self.formatter opts
4
+ if opts[:_recall] && opts[:_recall].has_key?(:locale)
5
+ opts[:locale] = ::I18n.locale.to_s if opts[:locale].blank?
6
+
7
+ if !opts[:script_name].blank?
8
+ locale = opts.delete(:locale).to_s
9
+ affect_script_name(opts, locale) unless opts[:locale].to_s == ::I18n.default_locale.to_s
10
+ else
11
+ opts[:locale] = nil if opts[:locale].to_s == ::I18n.default_locale.to_s
12
+ end
13
+ end
14
+
15
+ if opts.has_key?(:_positional_keys) && opts[:_positional_keys].first == :locale
16
+ i = opts[:_positional_keys].index(:locale)
17
+ opts[:_positional_args].insert i, (opts.delete(:locale) || ::I18n.locale.to_s)
18
+ end
19
+
20
+ opts
21
+ end
22
+
23
+ protected
24
+
25
+ def self.affect_script_name opts, locale
26
+ app_locales = ::I18n.locales[:main_app]
27
+ if opts[:script_name] =~ /^\/(#{::I18n.locales[:main_app].keys.join('|')})/
28
+ opts[:script_name].gsub!($1, locale)
29
+ else
30
+ opts[:script_name] = "/#{locale}#{opts[:script_name]}"
31
+ end
32
+ end
33
+ end
34
+ end
@@ -2,14 +2,16 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
+ # stub: rails-localization 1.2.0 ruby lib
5
6
 
6
7
  Gem::Specification.new do |s|
7
8
  s.name = "rails-localization"
8
- s.version = "0.2.0"
9
+ s.version = "1.2.0"
9
10
 
10
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
+ s.require_paths = ["lib"]
11
13
  s.authors = ["E-Max"]
12
- s.date = "2013-02-25"
14
+ s.date = "2014-01-31"
13
15
  s.description = "This gem allows you to localize your rails application with ease"
14
16
  s.email = "max@studentify.nl"
15
17
  s.extra_rdoc_files = [
@@ -18,7 +20,8 @@ Gem::Specification.new do |s|
18
20
  ]
19
21
  s.files = [
20
22
  ".document",
21
- ".rvmrc",
23
+ ".ruby-gemset",
24
+ ".ruby-version",
22
25
  ".travis.yml",
23
26
  "Gemfile",
24
27
  "Gemfile.lock",
@@ -27,13 +30,22 @@ Gem::Specification.new do |s|
27
30
  "Rakefile",
28
31
  "VERSION",
29
32
  "lib/rails-localization.rb",
30
- "lib/rails-localization/middleware.rb",
31
- "lib/rails-localization/routes_set_ext.rb",
33
+ "lib/rails-localization/controller.rb",
34
+ "lib/rails-localization/engine.rb",
35
+ "lib/rails-localization/i18n.rb",
36
+ "lib/rails-localization/route_set.rb",
37
+ "lib/rails-localization/router.rb",
38
+ "lib/rails-localization/url_options.rb",
32
39
  "lib/rails_localization.rb",
33
40
  "rails-localization.gemspec",
41
+ "test/i18n_integration_test.rb",
42
+ "test/mapper_integration_test.rb",
34
43
  "test/rails-localization_test.rb",
35
44
  "test/support/capybara_helper.rb",
36
45
  "test/test_app/.gitignore",
46
+ "test/test_app/Gemfile",
47
+ "test/test_app/Gemfile.lock",
48
+ "test/test_app/Rakefile",
37
49
  "test/test_app/app/assets/images/rails.png",
38
50
  "test/test_app/app/assets/javascripts/application.js",
39
51
  "test/test_app/app/assets/stylesheets/application.css",
@@ -73,56 +85,25 @@ Gem::Specification.new do |s|
73
85
  ]
74
86
  s.homepage = "http://github.com/kot-begemot/rails-localization"
75
87
  s.licenses = ["MIT"]
76
- s.require_paths = ["lib"]
77
- s.rubygems_version = "1.8.10"
88
+ s.rubygems_version = "2.2.1"
78
89
  s.summary = "Rails localization Gem"
79
90
 
80
91
  if s.respond_to? :specification_version then
81
- s.specification_version = 3
92
+ s.specification_version = 4
82
93
 
83
94
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
84
- s.add_runtime_dependency(%q<rails>, [">= 0"])
85
- s.add_runtime_dependency(%q<localization-middleware>, [">= 0"])
95
+ s.add_runtime_dependency(%q<rails>, [">= 4.0.0"])
86
96
  s.add_runtime_dependency(%q<i18n>, [">= 0"])
87
- s.add_development_dependency(%q<test-unit>, [">= 0"])
88
- s.add_development_dependency(%q<test-spec>, [">= 0"])
89
- s.add_development_dependency(%q<shoulda>, [">= 0"])
90
- s.add_development_dependency(%q<yard>, ["~> 0.6.0"])
91
- s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
92
- s.add_development_dependency(%q<simplecov>, ["~> 0.6.4"])
93
- s.add_development_dependency(%q<rcov>, ["~> 1.0.0"])
94
- s.add_development_dependency(%q<debugger>, ["~> 1.1.3"])
95
- s.add_development_dependency(%q<ruby-debug>, [">= 0"])
96
- s.add_development_dependency(%q<capybara>, [">= 0"])
97
+ s.add_development_dependency(%q<jeweler>, ["~> 1.8.0"])
97
98
  else
98
- s.add_dependency(%q<rails>, [">= 0"])
99
- s.add_dependency(%q<localization-middleware>, [">= 0"])
99
+ s.add_dependency(%q<rails>, [">= 4.0.0"])
100
100
  s.add_dependency(%q<i18n>, [">= 0"])
101
- s.add_dependency(%q<test-unit>, [">= 0"])
102
- s.add_dependency(%q<test-spec>, [">= 0"])
103
- s.add_dependency(%q<shoulda>, [">= 0"])
104
- s.add_dependency(%q<yard>, ["~> 0.6.0"])
105
- s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
106
- s.add_dependency(%q<simplecov>, ["~> 0.6.4"])
107
- s.add_dependency(%q<rcov>, ["~> 1.0.0"])
108
- s.add_dependency(%q<debugger>, ["~> 1.1.3"])
109
- s.add_dependency(%q<ruby-debug>, [">= 0"])
110
- s.add_dependency(%q<capybara>, [">= 0"])
101
+ s.add_dependency(%q<jeweler>, ["~> 1.8.0"])
111
102
  end
112
103
  else
113
- s.add_dependency(%q<rails>, [">= 0"])
114
- s.add_dependency(%q<localization-middleware>, [">= 0"])
104
+ s.add_dependency(%q<rails>, [">= 4.0.0"])
115
105
  s.add_dependency(%q<i18n>, [">= 0"])
116
- s.add_dependency(%q<test-unit>, [">= 0"])
117
- s.add_dependency(%q<test-spec>, [">= 0"])
118
- s.add_dependency(%q<shoulda>, [">= 0"])
119
- s.add_dependency(%q<yard>, ["~> 0.6.0"])
120
- s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
121
- s.add_dependency(%q<simplecov>, ["~> 0.6.4"])
122
- s.add_dependency(%q<rcov>, ["~> 1.0.0"])
123
- s.add_dependency(%q<debugger>, ["~> 1.1.3"])
124
- s.add_dependency(%q<ruby-debug>, [">= 0"])
125
- s.add_dependency(%q<capybara>, [">= 0"])
106
+ s.add_dependency(%q<jeweler>, ["~> 1.8.0"])
126
107
  end
127
108
  end
128
109
 
@@ -0,0 +1,28 @@
1
+ # encoding: utf-8
2
+ require File.expand_path("test_helper", File.dirname(__FILE__))
3
+
4
+ I18n.extend RailsLocalization::I18n
5
+
6
+ class I18nIntegrationTest < Test::Unit::TestCase
7
+ setup do
8
+ @i18n_locales = I18n.instance_variable_get(:@locales)
9
+ I18n.instance_variable_set(:@locales, nil)
10
+ end
11
+
12
+ teardown do
13
+ I18n.instance_variable_set(:@locales, @i18n_locales)
14
+ end
15
+
16
+ test "should Respond to locales" do
17
+ assert_respond_to I18n, :locales
18
+ end
19
+
20
+ test "should Return locales" do
21
+ I18n.instance_variable_set(:@locales, {"en" => "English"})
22
+ assert_equal({"en" => "English"}, I18n.locales)
23
+ end
24
+
25
+ test "should Return empty hash if no locales" do
26
+ assert_equal({}, I18n.locales)
27
+ end
28
+ end
@@ -0,0 +1,45 @@
1
+ # encoding: utf-8
2
+ require File.expand_path("test_helper", File.dirname(__FILE__))
3
+ require "action_dispatch"
4
+ require 'active_support/core_ext'
5
+ require 'rails-localization/router'
6
+
7
+ I18n.extend RailsLocalization::I18n
8
+ ActionDispatch::Routing::Mapper.send :include, ActionDispatch::Routing::Mapper::Localization
9
+
10
+ class MapperIntegrationTest < Test::Unit::TestCase
11
+ setup do
12
+ @route_set = ActionDispatch::Routing::RouteSet.new
13
+ @i18n_locales = I18n.instance_variable_get(:@locales)
14
+ I18n.instance_variable_set(:@locales, nil)
15
+ end
16
+
17
+ teardown do
18
+ I18n.instance_variable_set(:@locales, @i18n_locales)
19
+ end
20
+
21
+ test "should Respond to localized" do
22
+ mapper = ActionDispatch::Routing::Mapper.new @route_set
23
+ assert_respond_to mapper, :localized
24
+ end
25
+
26
+ test "should Define locales for I18n" do
27
+ @route_set.draw do
28
+ localized({"en" => "English"}) do; end
29
+ end
30
+
31
+ assert_kind_of(Hash, I18n.locales[:main_app])
32
+ end
33
+
34
+ # test "should recognize path" do
35
+ # @route_set.draw do
36
+ # localized({"en" => "English"}) do
37
+ # resources :posts
38
+ # root :to => "welcome#index"
39
+ # end
40
+ # end
41
+
42
+ # assert_equal({"en" => "English"}, @route_set.recognize_path("/", {:method => :get}))
43
+ # end
44
+ end
45
+
@@ -1,22 +1,19 @@
1
1
  # encoding: utf-8
2
2
  require File.expand_path("test_helper", File.dirname(__FILE__))
3
+ require File.expand_path("support/capybara_helper", File.dirname(__FILE__))
3
4
 
4
5
  class RailsLocalizationTest < ActionDispatch::IntegrationTest
5
6
  include CapybaraHelper
6
7
 
7
- def teardown
8
- after_teardown
9
- end
10
-
11
- context "sub app" do
12
- should "access without locale" do
8
+ #context "sub app" do
9
+ test "should access without locale" do
13
10
  visit "/sub"
14
11
 
15
12
  assert_equal 200, page.status_code
16
13
  assert page.has_content? "translation missing: en.sub_app.main.index"
17
14
  end
18
15
 
19
- should "access with english locale" do
16
+ test "should access with english locale" do
20
17
  visit "/en/sub"
21
18
  assert_equal 200, page.status_code
22
19
  assert page.has_content? "translation missing: en.sub_app.main.index"
@@ -26,7 +23,7 @@ class RailsLocalizationTest < ActionDispatch::IntegrationTest
26
23
  assert page.has_content? "translation missing: en.sub_app.main.index"
27
24
  end
28
25
 
29
- should "access with russian locale" do
26
+ test "should access with russian locale" do
30
27
  visit "/ru/sub"
31
28
  assert_equal 200, page.status_code
32
29
  assert page.has_content? "translation missing: ru.sub_app.main.index"
@@ -36,29 +33,29 @@ class RailsLocalizationTest < ActionDispatch::IntegrationTest
36
33
  assert page.has_content? "translation missing: ru.sub_app.main.index"
37
34
  end
38
35
 
39
- context "url_for" do
40
- should "correctly print path" do
36
+ #context "url_for" do
37
+ test "should correctly print path" do
41
38
  visit "/sub/print_redirect"
42
39
 
43
40
  assert_equal 200, page.status_code
44
41
  assert page.has_content?("/sub/print_redirect"), "Failed: #{page.source}"
45
42
  end
46
43
 
47
- should "correctly print path with english locale" do
48
- visit "en/sub/print_redirect"
44
+ test "should correctly print path with english locale" do
45
+ visit "/en/sub/print_redirect"
49
46
 
50
47
  assert_equal 200, page.status_code
51
- assert page.has_content?("/sub/print_redirect"), "Failed: #{page.source}"
48
+ assert page.has_content?("/en/sub/print_redirect"), "Failed: #{page.source}"
52
49
  end
53
50
 
54
- should "correctly print path with russian locale" do
51
+ test "should correctly print path with russian locale" do
55
52
  visit "/ru/sub/print_redirect"
56
53
 
57
54
  assert_equal 200, page.status_code
58
55
  assert page.has_content?("/ru/sub/print_redirect"), "Failed: #{page.source}"
59
56
  end
60
57
 
61
- should "return path with russian locale" do
58
+ test "should return path with russian locale" do
62
59
  visit "/sub/welcome"
63
60
 
64
61
  assert_equal 200, page.status_code
@@ -74,18 +71,18 @@ class RailsLocalizationTest < ActionDispatch::IntegrationTest
74
71
  assert_equal 200, page.status_code
75
72
  assert page.has_content?("/ru/sub/print_redirect"), "Failed: #{page.source}"
76
73
  end
77
- end
78
- end
74
+ #end
75
+ #end
79
76
 
80
- context "root page" do
81
- should "access without locale" do
77
+ #context "root page" do
78
+ test "root page should access without locale" do
82
79
  visit "/"
83
80
 
84
81
  assert_equal 200, page.status_code
85
82
  assert page.has_content? "translation missing: en.main.index"
86
83
  end
87
84
 
88
- should "access with english locale" do
85
+ test "root page should access with english locale" do
89
86
  visit "/en"
90
87
  assert_equal 200, page.status_code
91
88
  assert page.has_content? "translation missing: en.main.index"
@@ -95,7 +92,7 @@ class RailsLocalizationTest < ActionDispatch::IntegrationTest
95
92
  assert page.has_content? "translation missing: en.main.index"
96
93
  end
97
94
 
98
- should "access with russian locale" do
95
+ test "root page should access with russian locale" do
99
96
  visit "/ru"
100
97
  assert_equal 200, page.status_code
101
98
  assert page.has_content? "translation missing: ru.main.index"
@@ -104,9 +101,9 @@ class RailsLocalizationTest < ActionDispatch::IntegrationTest
104
101
  assert_equal 200, page.status_code
105
102
  assert page.has_content? "translation missing: ru.main.index"
106
103
  end
107
- end
104
+ #end
108
105
 
109
- context "welcome page" do
106
+ #context "welcome page" do
110
107
  def test_welcome_with_locale_1
111
108
  visit "/en/welcome"
112
109
  assert_equal 200, page.status_code
@@ -118,9 +115,9 @@ class RailsLocalizationTest < ActionDispatch::IntegrationTest
118
115
  assert_equal 200, page.status_code
119
116
  assert page.has_content? "translation missing: ru.main.welcome"
120
117
  end
121
- end
118
+ #end
122
119
 
123
- context "404" do
120
+ #context "404" do
124
121
  def test_wrong_page_without_locale
125
122
  assert_raise ActionController::RoutingError do
126
123
  visit "/test"
@@ -132,9 +129,9 @@ class RailsLocalizationTest < ActionDispatch::IntegrationTest
132
129
  visit "en/test"
133
130
  end
134
131
  end
135
- end
132
+ #end
136
133
 
137
- context "users page" do
134
+ #context "users page" do
138
135
  def test_user_controller_index
139
136
  visit "/en/users"
140
137
  assert page.has_content?("translation missing: en.users.index"), "Page body was: #{page.body}"
@@ -145,41 +142,44 @@ class RailsLocalizationTest < ActionDispatch::IntegrationTest
145
142
  visit "/ru/users"
146
143
  assert page.has_content? "translation missing: ru.users.index"
147
144
  end
148
- end
145
+ #end
149
146
 
150
- context "user path" do
151
- def test_locale_included_into_users_path
147
+ #context "user path" do
148
+ test "should include locale into users_path" do
152
149
  visit "/users/with_locale"
153
- assert_equal "/users", page.source, "Page body was: #{page.body}"
150
+ assert_equal "/users", page.source, "1. Page body was: #{page.body}"
154
151
 
155
152
  visit "/en/users/with_locale"
156
- assert_equal "/users", page.source, "Page body was: #{page.body}"
153
+ assert_equal "/users", page.source, "2. Page body was: #{page.body}"
157
154
 
158
155
  visit "/ru/users/with_locale"
159
- assert_equal "/ru/users", page.source, "Page body was: #{page.body}"
156
+ assert_equal "/ru/users", page.source, "3. Page body was: #{page.body}"
160
157
  end
161
158
 
162
- def test_locale_ignored_in_users_path
159
+ test "should ignore locale in users_path" do
163
160
  visit "/users/without_locale"
164
- assert_equal "/users", page.source, "Page body was: #{page.body}"
161
+ assert_equal "/users", page.source, "1. Page body was: #{page.body}"
165
162
 
166
- visit "/en/users/without_locale"
167
- assert_equal "/users", page.source, "Page body was: #{page.body}"
163
+ assert_raise ActionController::RoutingError do
164
+ visit "/en/users/without_locale"
165
+ end
168
166
 
169
- visit "/ru/users/without_locale"
170
- assert_equal "/users", page.source, "Page body was: #{page.body}"
167
+ assert_raise ActionController::RoutingError do
168
+ visit "/ru/users/without_locale"
169
+ end
171
170
  end
172
171
 
173
- def test_locale_forced_in_users_path
172
+ test "should forced locale in users_path" do
174
173
  visit "/users/with_defined_locale"
175
- assert_equal "/ru/users", page.source, "Page body was: #{page.body}"
174
+ assert_equal "/ru/users", page.source, "1. Page body was: #{page.body}"
176
175
 
177
- visit "/en/users/with_defined_locale"
178
- assert_equal "/ru/users", page.source, "Page body was: #{page.body}"
176
+ assert_raise ActionController::RoutingError do
177
+ visit "/en/users/with_defined_locale"
178
+ end
179
179
 
180
- visit "/ru/users/with_defined_locale"
181
- assert_equal "/ru/users", page.source, "Page body was: #{page.body}"
180
+ assert_raise ActionController::RoutingError do
181
+ visit "/ru/users/with_defined_locale"
182
+ end
182
183
  end
183
-
184
- end
184
+ #end
185
185
  end