subdomain_locale 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1,7 +1,11 @@
1
+ gem "hoe", "~> 3.0"
1
2
  require "hoe"
2
3
 
3
4
  Hoe.spec 'subdomain_locale' do
4
5
  developer 'Semyon Perepelitsa', 'sema@sema.in'
6
+ self.urls = %w(https://github.com/semaperepelitsa/subdomain_locale)
7
+ self.summary = "Moves current locale into subdomain in your Rails app"
8
+ dependency "i18n", "~> 0.2"
5
9
  end
6
10
 
7
11
  require "rake/testtask"
data/Readme.md ADDED
@@ -0,0 +1,22 @@
1
+ # Subdomain Locale (for Rails)
2
+
3
+ Moves current locale into subdomain. Installation is quick and simple:
4
+
5
+ # Gemfile
6
+ gem 'subdomain_locale', '~> 0.0.3'
7
+
8
+ # config/application.rb
9
+ config.i18n.available_locales = [:ru, :az, :en] # Put your own available locales
10
+ config.i18n.default_locale = :az # and make one default
11
+
12
+ Now, start your web server at localhost:3000 and navigate:
13
+
14
+ http://lvh.me:3000/ - the default locale (:az)
15
+ http://ru.lvh.me:3000/ - I18n.locale is set to :ru
16
+ http://www.lvh.me:3000/ - again, default
17
+
18
+ You can also put links to all locales in your view:
19
+
20
+ <% I18n.available_locales.each do |locale| %>
21
+ <%= link_to locale, params.merge(locale: locale) %>
22
+ <% end %>
@@ -1,3 +1,5 @@
1
+ require "subdomain_locale/locale"
2
+
1
3
  module SubdomainLocale
2
4
  module Controller
3
5
  def self.included(base)
@@ -1,6 +1,14 @@
1
+ require "i18n"
2
+
1
3
  module SubdomainLocale
2
4
  class Locale
3
5
  class << self
6
+ # Sets I18n.locale based on passed subdomain.
7
+ # Defaults to the I18n.default_locale if the subdomain isn't in I18n.available_locales
8
+ # I18n.default_locale = :az
9
+ # Locale.set('ru') # => :ru
10
+ # Locale.set('az') # => :az
11
+ # Locale.set('www')# => :az
4
12
  def set(subdomain)
5
13
  I18n.locale = find(subdomain).to_sym
6
14
  end
@@ -28,6 +36,10 @@ module SubdomainLocale
28
36
  @sym ||= @str.to_sym
29
37
  end
30
38
 
39
+ # Returns subdomain for the locale. If the locale is default, "www" is returned.
40
+ # I18n.default_locale = :az
41
+ # Locale.new(:ru).subdomain # => 'ru'
42
+ # Locale.new(:az).subdomain # => 'www'
31
43
  def subdomain
32
44
  if default?
33
45
  'www'
@@ -1,3 +1,6 @@
1
+ require "subdomain_locale/url_for"
2
+ require "subdomain_locale/controller"
3
+
1
4
  module SubdomainLocale
2
5
  class Railtie < ::Rails::Railtie
3
6
 
@@ -1,7 +1,18 @@
1
+ require "subdomain_locale/locale"
2
+
1
3
  module SubdomainLocale
2
4
  module UrlFor
5
+ # Makes url_for(locale: 'ru') the same as url_for(subdomain: 'ru', only_path: false)
6
+ # That way you can easily swap locale in subdomain with locale in the path.
7
+ #
8
+ # E. g. assuming you have <tt>scope ":locale"</tt> in your routes:
9
+ # url_for params.merge(locale: 'ru') # => /ru/current_path
10
+ # After including this module:
11
+ # url_for params.merge(locale: 'ru') # => http://ru.example.com/current_path
3
12
  def url_for(options=nil)
4
- if options.is_a?(Hash) and locale = options.delete(:locale)
13
+ if options.is_a?(Hash) and options.has_key?(:locale)
14
+ options = options.dup
15
+ locale = options.delete(:locale)
5
16
  options[:subdomain] = Locale.new(locale).subdomain
6
17
  options[:only_path] = false
7
18
  end
@@ -0,0 +1,3 @@
1
+ module SubdomainLocale
2
+ VERSION = "0.1.0"
3
+ end
@@ -1,9 +1,2 @@
1
+ require "subdomain_locale/version"
1
2
  require "subdomain_locale/railtie" if defined?(Rails)
2
-
3
- module SubdomainLocale
4
- VERSION = "0.0.3"
5
- autoload :UrlFor, "subdomain_locale/url_for"
6
- autoload :Controller, "subdomain_locale/controller"
7
- autoload :Locale, "subdomain_locale/locale"
8
- autoload :Railtie, "subdomain_locale/railtie"
9
- end
@@ -1,4 +1,6 @@
1
- require "test_helper"
1
+ require "minitest/autorun"
2
+ $LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
3
+ require "subdomain_locale/controller"
2
4
 
3
5
  class ControllerTest < MiniTest::Unit::TestCase
4
6
  class Controller
File without changes
data/test/locale_test.rb CHANGED
@@ -1,4 +1,6 @@
1
- require "test_helper"
1
+ require "minitest/autorun"
2
+ $LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
3
+ require "subdomain_locale/locale"
2
4
 
3
5
  class LocaleTest < MiniTest::Unit::TestCase
4
6
  include SubdomainLocale
data/test/url_for_test.rb CHANGED
@@ -1,9 +1,11 @@
1
- require "test_helper"
1
+ require "minitest/autorun"
2
+ $LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
3
+ require "subdomain_locale/url_for"
2
4
 
3
5
  class UrlForTest < MiniTest::Unit::TestCase
4
6
  module UrlFor
5
7
  def url_for(*args)
6
- @actual = args
8
+ args
7
9
  end
8
10
  end
9
11
 
@@ -11,27 +13,34 @@ class UrlForTest < MiniTest::Unit::TestCase
11
13
  include SubdomainLocale::UrlFor
12
14
 
13
15
  def test_does_not_affect_if_there_is_no_locale
14
- url_for(foo: 'bar')
16
+ @actual = url_for(foo: 'bar')
15
17
  assert_equal [{foo: 'bar'}], @actual
16
18
  end
17
19
 
18
20
  def test_does_not_affect_string_argument
19
- url_for('/bar')
21
+ @actual = url_for('/bar')
20
22
  assert_equal ['/bar'], @actual
21
23
  end
22
24
 
23
25
  def test_replaces_locale_with_subdomain_and_forces_not_only_path
24
- url_for(foo: 'bar', locale: :ru)
26
+ @actual = url_for(foo: 'bar', locale: :ru)
25
27
  assert_equal [{foo: 'bar', subdomain: 'ru', only_path: false}], @actual
26
28
  end
27
29
 
28
30
  def test_replaces_locale_with_subdomain_and_overrides_only_path
29
- url_for(foo: 'bar', locale: :ru, only_path: true)
31
+ @actual = url_for(foo: 'bar', locale: :ru, only_path: true)
30
32
  assert_equal [{foo: 'bar', subdomain: 'ru', only_path: false}], @actual
31
33
  end
32
34
 
33
35
  def test_sets_subdomain_to_www_for_default_locale
34
- url_for(foo: 'bar', locale: :az)
36
+ @actual = url_for(foo: 'bar', locale: :az)
35
37
  assert_equal [{foo: 'bar', subdomain: 'www', only_path: false}], @actual
36
38
  end
39
+
40
+ def test_original_options_not_modified
41
+ orig_params = { foo: 'bar', locale: :ru }
42
+ params = orig_params.dup
43
+ url_for(params)
44
+ assert_equal orig_params, params
45
+ end
37
46
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: subdomain_locale
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,22 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-09 00:00:00.000000000 Z
12
+ date: 2012-03-29 00:00:00.000000000 Z
13
13
  dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: i18n
16
+ requirement: &70346611712320 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '0.2'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70346611712320
14
25
  - !ruby/object:Gem::Dependency
15
26
  name: rdoc
16
- requirement: &70149124254240 !ruby/object:Gem::Requirement
27
+ requirement: &70346611711540 !ruby/object:Gem::Requirement
17
28
  none: false
18
29
  requirements:
19
30
  - - ~>
@@ -21,18 +32,18 @@ dependencies:
21
32
  version: '3.10'
22
33
  type: :development
23
34
  prerelease: false
24
- version_requirements: *70149124254240
35
+ version_requirements: *70346611711540
25
36
  - !ruby/object:Gem::Dependency
26
37
  name: hoe
27
- requirement: &70149124253660 !ruby/object:Gem::Requirement
38
+ requirement: &70346611700380 !ruby/object:Gem::Requirement
28
39
  none: false
29
40
  requirements:
30
41
  - - ~>
31
42
  - !ruby/object:Gem::Version
32
- version: '2.12'
43
+ version: '3.0'
33
44
  type: :development
34
45
  prerelease: false
35
- version_requirements: *70149124253660
46
+ version_requirements: *70346611700380
36
47
  description: ''
37
48
  email:
38
49
  - sema@sema.in
@@ -41,18 +52,19 @@ extensions: []
41
52
  extra_rdoc_files: []
42
53
  files:
43
54
  - Rakefile
55
+ - Readme.md
44
56
  - lib/subdomain_locale.rb
45
57
  - lib/subdomain_locale/controller.rb
46
58
  - lib/subdomain_locale/locale.rb
47
59
  - lib/subdomain_locale/railtie.rb
48
60
  - lib/subdomain_locale/url_for.rb
61
+ - lib/subdomain_locale/version.rb
49
62
  - test/controller_test.rb
50
- - test/i18n_stub.rb
63
+ - test/lib/i18n.rb
51
64
  - test/locale_test.rb
52
- - test/test_helper.rb
53
65
  - test/url_for_test.rb
54
66
  - .gemtest
55
- homepage:
67
+ homepage: https://github.com/semaperepelitsa/subdomain_locale
56
68
  licenses: []
57
69
  post_install_message:
58
70
  rdoc_options:
@@ -74,9 +86,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
74
86
  version: '0'
75
87
  requirements: []
76
88
  rubyforge_project: subdomain_locale
77
- rubygems_version: 1.8.13
89
+ rubygems_version: 1.8.11
78
90
  signing_key:
79
91
  specification_version: 3
80
- summary: ''
81
- test_files:
82
- - test/test_helper.rb
92
+ summary: Moves current locale into subdomain in your Rails app
93
+ test_files: []
data/test/test_helper.rb DELETED
@@ -1,6 +0,0 @@
1
- $LOAD_PATH.unshift File.expand_path('..', __FILE__)
2
- $LOAD_PATH.unshift File.expand_path('../../../lib', __FILE__)
3
- require "minitest/unit"
4
- require "subdomain_locale"
5
- require "i18n_stub"
6
- MiniTest::Unit.autorun