subdomain_locale 0.1.1 → 1.0.beta

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: faeaa8c630da1a8aaea5ec82125d459d60cc026f
4
+ data.tar.gz: c2d984aaa1f3027ea35641ff757913b694c6242f
5
+ SHA512:
6
+ metadata.gz: 20f97bd6745d902651120fbcc8dfb2bc44b21ee7e10a9e5b939071d9a84c8457f6ddd28ccd9b1d433e4f02bfab0cb521bfd20b89d2a2e2e500bfcba684d516ef
7
+ data.tar.gz: 8847cd159a0b90131bebf98ff93bfbd042b9beedfdf082ca93e912a5d0a0bbb4c895cd37e28fc8e31525e4ac40b5f6854ee41cd9b1430ffd37050fc31191c799
data/Isolate ADDED
@@ -0,0 +1,20 @@
1
+ options system: false, cleanup: false
2
+
3
+ gem 'bundler', '~> 1.5.0'
4
+
5
+ case ENV['RAILS']
6
+ when '3'
7
+ version = '~> 3.2.16'
8
+ gem 'actionpack', version
9
+ gem 'actionmailer', version
10
+ gem 'railties', version
11
+ gem 'i18n', '0.6.5'
12
+ when nil
13
+ version = '~> 4.0.0'
14
+ gem 'actionpack', version
15
+ gem 'actionmailer', version
16
+ gem 'railties', version
17
+ gem 'i18n', '~> 0.6.9'
18
+ else
19
+ raise "Unrecognized Rails version: #{ENV['RAILS']}"
20
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Semyon Perepelitsa
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile CHANGED
@@ -1,18 +1,17 @@
1
- gem "hoe", "~> 3.0"
2
- require "hoe"
3
-
4
- Hoe.spec 'subdomain_locale' do
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"
9
- end
10
-
11
1
  require "rake/testtask"
2
+ require "isolate/now"
3
+ require "bundler/gem_tasks"
4
+
12
5
  Rake::TestTask.new do |t|
13
6
  t.libs << "test"
14
7
  t.test_files = Dir['test/**/*_test.rb']
15
8
  t.verbose = true
16
9
  end
17
10
 
11
+ task "test:all" do
12
+ sh "rake test"
13
+ puts
14
+ sh "rake test RAILS=3"
15
+ end
16
+
18
17
  task :default => :test
data/Readme.md CHANGED
@@ -2,27 +2,57 @@
2
2
 
3
3
  Moves current locale into subdomain. Installation is quick and simple:
4
4
 
5
- # Gemfile
6
- gem 'subdomain_locale', '~> 0.1.0'
5
+ ```ruby
6
+ # Gemfile
7
+ gem 'subdomain_locale', '~> 0.1.0'
7
8
 
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
9
+ # config/application.rb
10
+ config.i18n.available_locales = [:ru, :az, :en, "en-US"] # Put your own available locales
11
+ config.i18n.default_locale = :az # and make one default
12
+ ```
11
13
 
12
14
  Now, start your web server at localhost:3000 and navigate:
13
15
 
14
16
  http://lvh.me:3000/ - the default locale (:az)
15
17
  http://ru.lvh.me:3000/ - I18n.locale is set to :ru
18
+ http://en-us.lvh.me:3000/ - I18n.locale is set to :en-US
16
19
  http://www.lvh.me:3000/ - again, default
17
20
 
18
21
  You can also put links to all locales in your view:
19
22
 
20
- <% I18n.available_locales.each do |locale| %>
21
- <%= link_to locale, params.merge(locale: locale) %>
22
- <% end %>
23
+ ```erb
24
+ <% I18n.available_locales.each do |locale| %>
25
+ <%= link_to locale, params.merge(locale: locale) %>
26
+ <% end %>
27
+ ```
28
+
29
+ ## Configuring own subdomain-to-locale mapping
30
+
31
+ ```ruby
32
+ # config/application.rb
33
+
34
+ config.subdomain_locales["us"] = "en-US"
35
+ # or even
36
+ # config.subdomain_locales = {"us" => "en-US", "ca" => "en-CA"}
37
+ ```
38
+
39
+ Having that configured `http://us.lvh.me:3000` will be rendered with :en-US locale.
40
+
41
+ ## Development setup
42
+
43
+ ```
44
+ gem install isolate
45
+ rake test:all
46
+ ```
47
+
23
48
 
24
49
  ## Changelog
25
50
 
51
+ master
52
+
53
+ * Support IETF language tag subdomains, e.g. pt-br.
54
+ * Allow to create custom mappings, e.g. "us" instead of "en-us".
55
+
26
56
  0.1.1
27
57
 
28
58
  * Adding changelog to the readme.
@@ -1,5 +1,3 @@
1
- require "subdomain_locale/locale"
2
-
3
1
  module SubdomainLocale
4
2
  module Controller
5
3
  def self.included(base)
@@ -9,7 +7,7 @@ module SubdomainLocale
9
7
  private
10
8
 
11
9
  def set_locale
12
- Locale.set(request.subdomain)
10
+ I18n.locale = SubdomainLocale.mapping.locale_for(request.subdomain)
13
11
  end
14
12
  end
15
13
  end
@@ -0,0 +1,22 @@
1
+ module SubdomainLocale
2
+ class << self
3
+ attr_accessor :mapping
4
+ end
5
+
6
+ class Mapping
7
+ def initialize(mapping)
8
+ @repository = {}
9
+ mapping.each do |subdomain, locale|
10
+ @repository[subdomain] = locale.to_s
11
+ end
12
+ end
13
+
14
+ def locale_for(subdomain)
15
+ @repository[subdomain] || subdomain
16
+ end
17
+
18
+ def subdomain_for(locale)
19
+ locale and @repository.invert[locale.to_s] || locale.to_s
20
+ end
21
+ end
22
+ end
@@ -2,6 +2,15 @@ require "rails/railtie"
2
2
 
3
3
  module SubdomainLocale
4
4
  class Railtie < ::Rails::Railtie
5
+ config.subdomain_locale = {}
6
+
7
+ # Execute after all application initializers, I18n is often configured there.
8
+ config.after_initialize do |app|
9
+ require "subdomain_locale/mapping"
10
+ default = { "" => I18n.default_locale, "www" => I18n.default_locale }
11
+ mapping = default.merge(app.config.subdomain_locale)
12
+ SubdomainLocale.mapping = Mapping.new(mapping)
13
+ end
5
14
 
6
15
  initializer "subdomain_locale.url_helpers" do
7
16
  require "subdomain_locale/url_for"
@@ -1,5 +1,3 @@
1
- require "subdomain_locale/locale"
2
-
3
1
  module SubdomainLocale
4
2
  module UrlFor
5
3
  # Makes url_for(locale: 'ru') the same as url_for(subdomain: 'ru', only_path: false)
@@ -10,14 +8,28 @@ module SubdomainLocale
10
8
  # After including this module:
11
9
  # url_for params.merge(locale: 'ru') # => http://ru.example.com/current_path
12
10
  def url_for(options=nil)
13
- if options.is_a?(Hash) and options.has_key?(:locale)
11
+ if options.is_a?(Hash)
14
12
  options = options.dup
15
- locale = options.delete(:locale)
16
- options[:subdomain] = Locale.new(locale).subdomain
17
- options[:only_path] = false
13
+ if options.has_key?(:locale)
14
+ # Locale specified, force full URL
15
+ locale = options.delete(:locale)
16
+ options[:subdomain] = subdomain_locales.subdomain_for(locale)
17
+ options[:only_path] = false
18
+ elsif options.has_key?(:only_path) && !options[:only_path]
19
+ # Requested full URL, use current locale
20
+ options[:subdomain] = subdomain_locales.subdomain_for(current_locale)
21
+ end
18
22
  end
19
23
 
20
24
  super
21
25
  end
26
+
27
+ def current_locale
28
+ I18n.locale
29
+ end
30
+
31
+ def subdomain_locales
32
+ SubdomainLocale.mapping
33
+ end
22
34
  end
23
35
  end
@@ -1,2 +1 @@
1
- require "subdomain_locale/version"
2
1
  require "subdomain_locale/railtie" if defined?(Rails)
@@ -0,0 +1,16 @@
1
+ # coding: utf-8
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "subdomain_locale"
5
+ spec.version = "1.0.beta"
6
+ spec.authors = ["Semyon Perepelitsa"]
7
+ spec.email = ["sema@sema.in"]
8
+ spec.summary = "Set I18n locale based on subdomain"
9
+ spec.homepage = "https://github.com/semaperepelitsa/subdomain_locale"
10
+ spec.license = "MIT"
11
+
12
+ spec.files = File.read("Manifest.txt").split("\n")
13
+ spec.test_files = spec.files.grep(%r{^test/})
14
+
15
+ spec.add_dependency "i18n", "~> 0.2"
16
+ end
@@ -0,0 +1,3 @@
1
+ class ApplicationController < ActionController::Base
2
+ protect_from_forgery
3
+ end
@@ -0,0 +1,2 @@
1
+ class HelloController < ApplicationController
2
+ end
@@ -0,0 +1,7 @@
1
+ class HelloMailer < ActionMailer::Base
2
+ def world
3
+ mail from: "app@example.com", to: "world@example.com", subject: "Hello, world!" do |f|
4
+ f.text
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,8 @@
1
+ <menu>
2
+ <%= link_to "Home", root_path %>
3
+ <%= link_to "English", root_path(locale: "en") %>
4
+ <%= link_to "Russian", root_path(locale: "ru") %>
5
+ <%= link_to "Ukrainian", root_path(locale: "uk") %>
6
+ </menu>
7
+
8
+ <p><%= t "hello" %></p>
@@ -0,0 +1,4 @@
1
+ Hello,
2
+
3
+ Here is our website:
4
+ <%= root_url %>
@@ -0,0 +1,12 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Dummy</title>
5
+ <%= csrf_meta_tags %>
6
+ </head>
7
+ <body>
8
+
9
+ <%= yield %>
10
+
11
+ </body>
12
+ </html>
@@ -0,0 +1,14 @@
1
+ require "action_controller/railtie"
2
+ require "action_mailer/railtie"
3
+ require "subdomain_locale/railtie"
4
+
5
+ module Dummy
6
+ class Application < Rails::Application
7
+ config.i18n.enforce_available_locales = true
8
+ config.i18n.default_locale = :en
9
+ config.i18n.available_locales = :en, :ru, :uk
10
+ config.subdomain_locale["ua"] = :uk
11
+
12
+ config.secret_key_base = '-'
13
+ end
14
+ end
@@ -0,0 +1,5 @@
1
+ # Load the rails application
2
+ require File.expand_path('../application', __FILE__)
3
+
4
+ # Initialize the rails application
5
+ Dummy::Application.initialize!
@@ -0,0 +1,12 @@
1
+ Dummy::Application.configure do
2
+ config.eager_load = false
3
+ config.cache_classes = true
4
+ config.serve_static_assets = true
5
+ config.consider_all_requests_local = true
6
+ config.action_controller.perform_caching = false
7
+ config.action_dispatch.show_exceptions = false
8
+ config.action_controller.allow_forgery_protection = false
9
+ config.active_support.deprecation = :stderr
10
+ config.action_mailer.delivery_method = :test
11
+ config.action_mailer.default_url_options = { host: "example.com" }
12
+ end
@@ -0,0 +1,2 @@
1
+ ru:
2
+ hello: Привет
@@ -0,0 +1,2 @@
1
+ uk:
2
+ hello: Привіт
@@ -0,0 +1,3 @@
1
+ Dummy::Application.routes.draw do
2
+ root to: "hello#world"
3
+ end
@@ -0,0 +1,25 @@
1
+ require "isolate/now"
2
+ require "minitest/autorun"
3
+ require "subdomain_locale/mapping"
4
+
5
+ class MappingTest < MiniTest::Unit::TestCase
6
+ include SubdomainLocale
7
+
8
+ def setup
9
+ @mapping = Mapping.new("ua" => :uk)
10
+ end
11
+
12
+ def test_custom
13
+ assert_equal "uk", @mapping.locale_for("ua")
14
+ assert_equal "ua", @mapping.subdomain_for(:uk)
15
+ end
16
+
17
+ def test_default
18
+ assert_equal "ru", @mapping.locale_for("ru")
19
+ assert_equal "ru", @mapping.subdomain_for(:ru)
20
+ end
21
+
22
+ def test_nil
23
+ assert_nil @mapping.subdomain_for(nil)
24
+ end
25
+ end
@@ -0,0 +1,55 @@
1
+ # encoding: UTF-8
2
+ ENV["RAILS_ENV"] = "test"
3
+
4
+ require "isolate/now"
5
+ require File.expand_path("../dummy/config/environment.rb", __FILE__)
6
+ require "minitest/autorun"
7
+ require "rails/test_help"
8
+ Rails.backtrace_cleaner.remove_silencers!
9
+
10
+ class HelloControllerTest < ActionController::TestCase
11
+ def test_links
12
+ @request.host = "example.com"
13
+ get :world
14
+ menu = css_select("menu a")
15
+ assert_equal "/", menu[0]["href"]
16
+ assert_equal "http://www.example.com/", menu[1]["href"]
17
+ assert_equal "http://ru.example.com/", menu[2]["href"]
18
+ assert_equal "http://ua.example.com/", menu[3]["href"]
19
+ end
20
+
21
+ def test_default
22
+ @request.host = "www.example.com"
23
+ get :world
24
+ assert_response :ok
25
+ assert_select "p", "Hello"
26
+ end
27
+
28
+ def test_direct
29
+ @request.host = "ru.example.com"
30
+ get :world
31
+ assert_response :ok
32
+ assert_select "p", "Привет"
33
+ end
34
+
35
+ def test_custom
36
+ @request.host = "ua.example.com"
37
+ get :world
38
+ assert_response :ok
39
+ assert_select "p", "Привіт"
40
+ end
41
+
42
+ def test_other
43
+ @request.host = "wtf.example.com"
44
+ assert_raise(I18n::InvalidLocale) do
45
+ get :world
46
+ end
47
+ end
48
+ end
49
+
50
+ class HelloMailerTest < ActionController::TestCase
51
+ def test
52
+ mail = I18n.with_locale(:ru) { HelloMailer.world }
53
+ assert_equal "http://ru.example.com/", mail.body.to_s.lines[3].chomp
54
+ end
55
+ end
data/test/url_for_test.rb CHANGED
@@ -1,5 +1,6 @@
1
+ require "isolate/now"
1
2
  require "minitest/autorun"
2
- $LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
3
+ require "subdomain_locale"
3
4
  require "subdomain_locale/url_for"
4
5
 
5
6
  class UrlForTest < MiniTest::Unit::TestCase
@@ -12,34 +13,46 @@ class UrlForTest < MiniTest::Unit::TestCase
12
13
  include UrlFor
13
14
  include SubdomainLocale::UrlFor
14
15
 
15
- def test_does_not_affect_if_there_is_no_locale
16
+ def subdomain_locales
17
+ @mapping ||= Object.new.tap do |mapping|
18
+ def mapping.subdomain_for(locale)
19
+ "ru" if locale == :ru
20
+ end
21
+ end
22
+ end
23
+
24
+ def current_locale
25
+ :ru
26
+ end
27
+
28
+ def test_no_locale
16
29
  @actual = url_for(foo: 'bar')
17
30
  assert_equal [{foo: 'bar'}], @actual
18
31
  end
19
32
 
20
- def test_does_not_affect_string_argument
33
+ def test_string_argument
21
34
  @actual = url_for('/bar')
22
35
  assert_equal ['/bar'], @actual
23
36
  end
24
37
 
25
- def test_replaces_locale_with_subdomain_and_forces_not_only_path
38
+ def test_locale
26
39
  @actual = url_for(foo: 'bar', locale: :ru)
27
40
  assert_equal [{foo: 'bar', subdomain: 'ru', only_path: false}], @actual
28
41
  end
29
42
 
30
- def test_replaces_locale_with_subdomain_and_overrides_only_path
43
+ def test_only_path
31
44
  @actual = url_for(foo: 'bar', locale: :ru, only_path: true)
32
45
  assert_equal [{foo: 'bar', subdomain: 'ru', only_path: false}], @actual
33
46
  end
34
47
 
35
- def test_sets_subdomain_to_www_for_default_locale
36
- @actual = url_for(foo: 'bar', locale: :az)
37
- assert_equal [{foo: 'bar', subdomain: 'www', only_path: false}], @actual
48
+ def test_implicit_locale
49
+ @actual = url_for(foo: 'bar', only_path: false)
50
+ assert_equal [{foo: 'bar', subdomain: 'ru', only_path: false}], @actual
38
51
  end
39
52
 
40
- def test_original_options_not_modified
53
+ def test_hash_immutable
41
54
  orig_params = { foo: 'bar', locale: :ru }
42
- params = orig_params.dup
55
+ params = orig_params.dup.freeze
43
56
  url_for(params)
44
57
  assert_equal orig_params, params
45
58
  end
metadata CHANGED
@@ -1,108 +1,98 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: subdomain_locale
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
5
- prerelease:
4
+ version: 1.0.beta
6
5
  platform: ruby
7
6
  authors:
8
7
  - Semyon Perepelitsa
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-05-24 00:00:00.000000000 Z
11
+ date: 2014-02-17 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: i18n
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ~>
17
+ - - "~>"
20
18
  - !ruby/object:Gem::Version
21
19
  version: '0.2'
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ~>
24
+ - - "~>"
28
25
  - !ruby/object:Gem::Version
29
26
  version: '0.2'
30
- - !ruby/object:Gem::Dependency
31
- name: rdoc
32
- requirement: !ruby/object:Gem::Requirement
33
- none: false
34
- requirements:
35
- - - ~>
36
- - !ruby/object:Gem::Version
37
- version: '3.10'
38
- type: :development
39
- prerelease: false
40
- version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
- requirements:
43
- - - ~>
44
- - !ruby/object:Gem::Version
45
- version: '3.10'
46
- - !ruby/object:Gem::Dependency
47
- name: hoe
48
- requirement: !ruby/object:Gem::Requirement
49
- none: false
50
- requirements:
51
- - - ~>
52
- - !ruby/object:Gem::Version
53
- version: '3.0'
54
- type: :development
55
- prerelease: false
56
- version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
- requirements:
59
- - - ~>
60
- - !ruby/object:Gem::Version
61
- version: '3.0'
62
- description: ''
27
+ description:
63
28
  email:
64
29
  - sema@sema.in
65
30
  executables: []
66
31
  extensions: []
67
32
  extra_rdoc_files: []
68
33
  files:
34
+ - Isolate
35
+ - LICENSE.txt
69
36
  - Rakefile
70
37
  - Readme.md
71
38
  - lib/subdomain_locale.rb
72
39
  - lib/subdomain_locale/controller.rb
73
- - lib/subdomain_locale/locale.rb
40
+ - lib/subdomain_locale/mapping.rb
74
41
  - lib/subdomain_locale/railtie.rb
75
42
  - lib/subdomain_locale/url_for.rb
76
- - lib/subdomain_locale/version.rb
77
- - test/controller_test.rb
78
- - test/lib/i18n.rb
79
- - test/locale_test.rb
43
+ - subdomain_locale.gemspec
44
+ - test/dummy/app/controllers/application_controller.rb
45
+ - test/dummy/app/controllers/hello_controller.rb
46
+ - test/dummy/app/mailers/hello_mailer.rb
47
+ - test/dummy/app/views/hello/world.html.erb
48
+ - test/dummy/app/views/hello_mailer/world.text.erb
49
+ - test/dummy/app/views/layouts/application.html.erb
50
+ - test/dummy/config/application.rb
51
+ - test/dummy/config/environment.rb
52
+ - test/dummy/config/environments/test.rb
53
+ - test/dummy/config/locales/ru.yml
54
+ - test/dummy/config/locales/uk.yml
55
+ - test/dummy/config/routes.rb
56
+ - test/mapping_test.rb
57
+ - test/rails_test.rb
80
58
  - test/url_for_test.rb
81
- - .gemtest
82
59
  homepage: https://github.com/semaperepelitsa/subdomain_locale
83
- licenses: []
60
+ licenses:
61
+ - MIT
62
+ metadata: {}
84
63
  post_install_message:
85
- rdoc_options:
86
- - --main
87
- - README.txt
64
+ rdoc_options: []
88
65
  require_paths:
89
66
  - lib
90
67
  required_ruby_version: !ruby/object:Gem::Requirement
91
- none: false
92
68
  requirements:
93
- - - ! '>='
69
+ - - ">="
94
70
  - !ruby/object:Gem::Version
95
71
  version: '0'
96
72
  required_rubygems_version: !ruby/object:Gem::Requirement
97
- none: false
98
73
  requirements:
99
- - - ! '>='
74
+ - - ">"
100
75
  - !ruby/object:Gem::Version
101
- version: '0'
76
+ version: 1.3.1
102
77
  requirements: []
103
- rubyforge_project: subdomain_locale
104
- rubygems_version: 1.8.23
78
+ rubyforge_project:
79
+ rubygems_version: 2.2.0
105
80
  signing_key:
106
- specification_version: 3
107
- summary: Moves current locale into subdomain in your Rails app
108
- test_files: []
81
+ specification_version: 4
82
+ summary: Set I18n locale based on subdomain
83
+ test_files:
84
+ - test/dummy/app/controllers/application_controller.rb
85
+ - test/dummy/app/controllers/hello_controller.rb
86
+ - test/dummy/app/mailers/hello_mailer.rb
87
+ - test/dummy/app/views/hello/world.html.erb
88
+ - test/dummy/app/views/hello_mailer/world.text.erb
89
+ - test/dummy/app/views/layouts/application.html.erb
90
+ - test/dummy/config/application.rb
91
+ - test/dummy/config/environment.rb
92
+ - test/dummy/config/environments/test.rb
93
+ - test/dummy/config/locales/ru.yml
94
+ - test/dummy/config/locales/uk.yml
95
+ - test/dummy/config/routes.rb
96
+ - test/mapping_test.rb
97
+ - test/rails_test.rb
98
+ - test/url_for_test.rb
data/.gemtest DELETED
File without changes
@@ -1,63 +0,0 @@
1
- require "i18n"
2
-
3
- module SubdomainLocale
4
- class Locale
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
12
- def set(subdomain)
13
- I18n.locale = find(subdomain).to_sym
14
- end
15
-
16
- private
17
-
18
- def find(subdomain)
19
- new(subdomain).valid || default
20
- end
21
-
22
- def default
23
- new(I18n.default_locale)
24
- end
25
- end
26
-
27
- def initialize(str)
28
- @str = str.to_s
29
- end
30
-
31
- def to_s
32
- @str
33
- end
34
-
35
- def to_sym
36
- @sym ||= @str.to_sym
37
- end
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'
43
- def subdomain
44
- if default?
45
- 'www'
46
- else
47
- to_s
48
- end
49
- end
50
-
51
- def default?
52
- I18n.default_locale.to_sym == self.to_sym
53
- end
54
-
55
- def valid
56
- self if valid?
57
- end
58
-
59
- def valid?
60
- I18n.available_locales.include?(self.to_sym)
61
- end
62
- end
63
- end
@@ -1,3 +0,0 @@
1
- module SubdomainLocale
2
- VERSION = "0.1.1"
3
- end
@@ -1,38 +0,0 @@
1
- require "minitest/autorun"
2
- $LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
3
- require "subdomain_locale/controller"
4
-
5
- class ControllerTest < MiniTest::Unit::TestCase
6
- class Controller
7
- class Request
8
- def subdomain
9
- "ru"
10
- end
11
- end
12
-
13
- def self.before_filter(sym)
14
- @@before_filters ||= []
15
- @@before_filters << sym
16
- end
17
-
18
- def self.before_filters
19
- @@before_filters
20
- end
21
-
22
- def run
23
- @@before_filters.each{ |sym| send(sym) }
24
- end
25
-
26
- def request
27
- Request.new
28
- end
29
-
30
- include SubdomainLocale::Controller
31
- end
32
-
33
- def test_sets_locale
34
- assert_nil I18n.locale
35
- Controller.new.run
36
- assert_equal :ru, I18n.locale
37
- end
38
- end
data/test/lib/i18n.rb DELETED
@@ -1,13 +0,0 @@
1
- module I18n
2
- class << self
3
- attr_accessor :locale
4
-
5
- def available_locales
6
- [:az, :ru, :en]
7
- end
8
-
9
- def default_locale
10
- :az
11
- end
12
- end
13
- end
data/test/locale_test.rb DELETED
@@ -1,33 +0,0 @@
1
- require "minitest/autorun"
2
- $LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
3
- require "subdomain_locale/locale"
4
-
5
- class LocaleTest < MiniTest::Unit::TestCase
6
- include SubdomainLocale
7
-
8
- def test_sets_locale
9
- Locale.set("ru")
10
- assert_equal :ru, I18n.locale
11
- end
12
-
13
- def test_sets_default_locale_for_nil
14
- Locale.set(nil)
15
- assert_equal :az, I18n.locale
16
- end
17
-
18
- def test_subdomain
19
- assert_equal "ru", Locale.new(:ru).subdomain
20
- end
21
-
22
- def test_default_subdomain
23
- assert_equal "www", Locale.new(:az).subdomain
24
- end
25
-
26
- def test_default?
27
- assert Locale.new(:az).default?
28
- end
29
-
30
- def test_not_default?
31
- refute Locale.new(:ru).default?
32
- end
33
- end