localized 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.swp
2
+ log/*.log
3
+ .DS_Store
4
+
data/.rvmrc ADDED
@@ -0,0 +1,3 @@
1
+ rvm use 1.9.2
2
+ rvm gemset create gem-localized
3
+ rvm gemset use gem-localized
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,109 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ localized (0.0.1)
5
+ rails (>= 3.0)
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ abstract (1.0.0)
11
+ actionmailer (3.0.0)
12
+ actionpack (= 3.0.0)
13
+ mail (~> 2.2.5)
14
+ actionpack (3.0.0)
15
+ activemodel (= 3.0.0)
16
+ activesupport (= 3.0.0)
17
+ builder (~> 2.1.2)
18
+ erubis (~> 2.6.6)
19
+ i18n (~> 0.4.1)
20
+ rack (~> 1.2.1)
21
+ rack-mount (~> 0.6.12)
22
+ rack-test (~> 0.5.4)
23
+ tzinfo (~> 0.3.23)
24
+ activemodel (3.0.0)
25
+ activesupport (= 3.0.0)
26
+ builder (~> 2.1.2)
27
+ i18n (~> 0.4.1)
28
+ activerecord (3.0.0)
29
+ activemodel (= 3.0.0)
30
+ activesupport (= 3.0.0)
31
+ arel (~> 1.0.0)
32
+ tzinfo (~> 0.3.23)
33
+ activeresource (3.0.0)
34
+ activemodel (= 3.0.0)
35
+ activesupport (= 3.0.0)
36
+ activesupport (3.0.0)
37
+ archive-tar-minitar (0.5.2)
38
+ arel (1.0.1)
39
+ activesupport (~> 3.0.0)
40
+ builder (2.1.2)
41
+ columnize (0.3.4)
42
+ diff-lcs (1.1.2)
43
+ erubis (2.6.6)
44
+ abstract (>= 1.0.0)
45
+ i18n (0.4.2)
46
+ linecache19 (0.5.12)
47
+ ruby_core_source (>= 0.1.4)
48
+ mail (2.2.19)
49
+ activesupport (>= 2.3.6)
50
+ i18n (>= 0.4.0)
51
+ mime-types (~> 1.16)
52
+ treetop (~> 1.4.8)
53
+ mime-types (1.16)
54
+ polyglot (0.3.1)
55
+ rack (1.2.3)
56
+ rack-mount (0.6.14)
57
+ rack (>= 1.0.0)
58
+ rack-test (0.5.7)
59
+ rack (>= 1.0)
60
+ rails (3.0.0)
61
+ actionmailer (= 3.0.0)
62
+ actionpack (= 3.0.0)
63
+ activerecord (= 3.0.0)
64
+ activeresource (= 3.0.0)
65
+ activesupport (= 3.0.0)
66
+ bundler (~> 1.0.0)
67
+ railties (= 3.0.0)
68
+ railties (3.0.0)
69
+ actionpack (= 3.0.0)
70
+ activesupport (= 3.0.0)
71
+ rake (>= 0.8.4)
72
+ thor (~> 0.14.0)
73
+ rake (0.9.2)
74
+ rspec (2.6.0)
75
+ rspec-core (~> 2.6.0)
76
+ rspec-expectations (~> 2.6.0)
77
+ rspec-mocks (~> 2.6.0)
78
+ rspec-core (2.6.4)
79
+ rspec-expectations (2.6.0)
80
+ diff-lcs (~> 1.1.2)
81
+ rspec-mocks (2.6.0)
82
+ rspec-rails (2.6.0)
83
+ actionpack (~> 3.0)
84
+ activesupport (~> 3.0)
85
+ railties (~> 3.0)
86
+ rspec (~> 2.6.0)
87
+ ruby-debug-base19 (0.11.25)
88
+ columnize (>= 0.3.1)
89
+ linecache19 (>= 0.5.11)
90
+ ruby_core_source (>= 0.1.4)
91
+ ruby-debug19 (0.11.6)
92
+ columnize (>= 0.3.1)
93
+ linecache19 (>= 0.5.11)
94
+ ruby-debug-base19 (>= 0.11.19)
95
+ ruby_core_source (0.1.5)
96
+ archive-tar-minitar (>= 0.5.2)
97
+ thor (0.14.6)
98
+ treetop (1.4.9)
99
+ polyglot (>= 0.3.1)
100
+ tzinfo (0.3.29)
101
+
102
+ PLATFORMS
103
+ ruby
104
+
105
+ DEPENDENCIES
106
+ localized!
107
+ rspec (= 2.6.0)
108
+ rspec-rails (= 2.6.0)
109
+ ruby-debug19
data/README.md ADDED
@@ -0,0 +1,45 @@
1
+ ### Localized
2
+
3
+ A simple gem for Rails 3 that helps set the locale for your application from a
4
+ subdomain. It also contains a way for overriding the "site" in url helpers.
5
+
6
+ ## Setup
7
+
8
+ 1. Add a config/localized.yml file with your supported subdomain/site name and locale mappings.
9
+
10
+ 2. Add your domains to your /etc/hosts file for development:
11
+
12
+ 127.0.0.1 www.mysite.localhost # default locale
13
+ 127.0.0.1 www.it.mysite.localhost
14
+ 127.0.0.1 www.ca.mysite.localhost
15
+
16
+ 3. Create your "change locale widget" using simple url helpers:
17
+
18
+ root_url(:site => :us) # => http://www.mysite.com/
19
+ root_url(:site => :it) # => http://www.it.mysite.com/
20
+ root_url(:site => :ca) # => http://www.ca.mysite.com/
21
+
22
+ OR to keep the current locale:
23
+
24
+ root_url # => http://www.<whatever the current site is>.mysite.com
25
+
26
+ ## Contributing
27
+
28
+ This is a very simple gem and I am sure you could think of something you could
29
+ add to it, please create a pull request.
30
+
31
+ ## Author
32
+
33
+ Paul Hepworth (http://peppyheppy.com)
34
+
35
+ ## License
36
+
37
+ The MIT License (MIT)
38
+ Copyright (c) 2011 Paul Hepworth
39
+
40
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
41
+
42
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
43
+
44
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
45
+
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env rake
2
+
3
+ require 'bundler'
4
+ Bundler::GemHelper.install_tasks
5
+
6
+ require "rspec/core/rake_task"
7
+ RSpec::Core::RakeTask.new do |t|
8
+ t.rspec_opts = ["-c", "-f progress", "-r ./spec/spec_helper.rb"]
9
+ t.pattern = 'spec/**/*_spec.rb'
10
+ end
11
+
12
+ task :default => [:spec]
13
+
@@ -0,0 +1,5 @@
1
+ default_host_prefix: 'www.'
2
+ default_site: 'us'
3
+ site_locale_map:
4
+ us: 'en-US'
5
+
data/lib/localized.rb ADDED
@@ -0,0 +1,14 @@
1
+ require 'rails'
2
+ require 'action_view'
3
+ require 'action_controller'
4
+ require 'active_model'
5
+ module Localized; end
6
+ require 'localized/config'
7
+ require 'localized/helper'
8
+
9
+ # load the modules into the rails world
10
+ [
11
+ ActionView::Helpers::UrlHelper,
12
+ ActionController::Base
13
+ ].each { |mod| mod.send :include, Localized::Helper }
14
+
@@ -0,0 +1,54 @@
1
+ require 'yaml'
2
+
3
+ module Localized::Config
4
+ def self.configuration
5
+ @configuration ||= begin
6
+ config_file = File.expand_path(File.join(Rails.root.to_s,'/config/localized.yml'))
7
+ defaults = YAML.load_file('config/defaults.yml')
8
+ custom = YAML.load_file(config_file) if File.exists?(config_file)
9
+ symobolize_keys_and_values(defaults.merge(custom || {}))
10
+ end.symbolize_keys
11
+ end
12
+
13
+ def self.default_host_prefix
14
+ @default_host_prefix ||= self.configuration[:default_host_prefix].to_s
15
+ end
16
+
17
+ def self.site_to_locale_map
18
+ @site_to_locale_map ||= self.configuration[:site_locale_map].symbolize_keys
19
+ end
20
+
21
+ def self.locale_to_site_map
22
+ @locale_to_site_map ||= site_to_locale_map.invert
23
+ end
24
+
25
+ def self.default_site
26
+ @default_site ||= self.configuration[:default_site]
27
+ end
28
+
29
+ def self.supported_sites
30
+ @supported_sites ||= site_to_locale_map.symbolize_keys.keys.sort
31
+ end
32
+
33
+ def self.supported_locales
34
+ @supported_locales ||= site_to_locale_map.values.sort
35
+ end
36
+
37
+ def self.default_locale
38
+ @default_locale ||= site_to_locale_map[default_site]
39
+ end
40
+
41
+ private
42
+
43
+ def self.symobolize_keys_and_values(hash_name)
44
+ new_hash = HashWithIndifferentAccess.new({})
45
+ hash_name.each do |k,v|
46
+ if v.is_a? Hash
47
+ new_hash[k.to_sym] = symobolize_keys_and_values(v)
48
+ else
49
+ new_hash[k.to_sym] = v.to_s.to_sym
50
+ end
51
+ end
52
+ new_hash
53
+ end
54
+ end
@@ -0,0 +1,43 @@
1
+ module Localized::Helper
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ if self.respond_to? :before_filter
6
+ before_filter :set_locale
7
+ end
8
+ end
9
+
10
+ def set_locale
11
+ key = request.subdomains.last.to_s.to_sym
12
+ unless Localized::Config.site_to_locale_map[key]
13
+ key = Localized::Config.default_site
14
+ end
15
+ I18n.locale = Localized::Config.site_to_locale_map[key]
16
+ end
17
+
18
+ def url_for(options = nil)
19
+ if options.kind_of?(Hash)
20
+ options[:host] = with_locale(options.delete(:site))
21
+ end
22
+ super options
23
+ end
24
+
25
+ protected
26
+
27
+ def with_locale(site = nil)
28
+ current_site = Localized::Config.locale_to_site_map[I18n.locale]
29
+ site = if site.blank? and current_site != Localized::Config.default_site
30
+ current_site
31
+ else
32
+ site || Localized::Config.default_site
33
+ end.to_s.to_sym
34
+ Localized::Config.site_to_locale_map.fetch(site) {
35
+ raise ArgumentError,
36
+ "the site locale alias #{site} does not exist in list: #{Localized::Config.site_to_locale_map.stringify_keys.keys.sort.join(', ')}",
37
+ caller(5)
38
+ }
39
+
40
+ subdomain = (Localized::Config.default_site != site ? "#{site}." : nil)
41
+ [Localized::Config.default_host_prefix, subdomain, request.domain, request.port_string].join
42
+ end
43
+ end
data/localized.gemspec ADDED
@@ -0,0 +1,15 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'localized'
3
+ s.author = 'Paul Hepworth'
4
+ s.email = 'paul<dot>hepworth<at>peppyheppy<dot>com'
5
+ s.version = '0.0.1'
6
+ s.date = '2011-07-09'
7
+ s.summary = "A ruby on rails gem that provides locale setting support through the subdomain (in a box)"
8
+ s.description = "This gem allows you to set locale using a subdomain and url helper support for switching sites."
9
+ s.files = s.files = `git ls-files`.split("\n")
10
+ s.add_runtime_dependency "rails", ">= 3.0"
11
+ s.add_development_dependency "ruby-debug19"
12
+ s.add_development_dependency "rspec", "2.6.0"
13
+ s.add_development_dependency "rspec-rails", "2.6.0"
14
+ end
15
+
@@ -0,0 +1,18 @@
1
+ default_host_prefix: 'www.'
2
+ default_site: 'us'
3
+ site_locale_map:
4
+ au: 'en-AU'
5
+ nl: 'nl-NL'
6
+ uk: 'en-GB'
7
+ us: 'en-US'
8
+ fr: 'fr-FR'
9
+ de: 'de-DE'
10
+ it: 'it-IT'
11
+ se: 'sv-SE'
12
+ es: 'es-ES'
13
+ jp: 'ja-JP'
14
+ ca: 'en-CA'
15
+ es: 'es-ES'
16
+ jp: 'ja-JP'
17
+ ca: 'en-CA'
18
+
@@ -0,0 +1,2 @@
1
+ LocalizedApp::Application.routes.draw do
2
+ end
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+
3
+ describe Localized::Config do
4
+
5
+ before :each do
6
+ Rails.stub(:root).and_return(Pathname.new(File.join(File.dirname(__FILE__),'..', '..')))
7
+ end
8
+
9
+ it "should load defaults" do
10
+ Localized::Config.configuration.should_not be_nil
11
+ Localized::Config.configuration.keys.should include :site_locale_map
12
+ end
13
+
14
+ it "should have site to local mappings" do
15
+ Localized::Config.should respond_to :site_to_locale_map
16
+ Localized::Config.site_to_locale_map.should be_a Hash
17
+ Localized::Config.site_to_locale_map.keys.should include(:us)
18
+ end
19
+
20
+ it "should have local to site mappings" do
21
+ Localized::Config.should respond_to :locale_to_site_map
22
+ Localized::Config.locale_to_site_map.should be_a Hash
23
+ Localized::Config.locale_to_site_map.keys.should include(:'en-US')
24
+ end
25
+
26
+ it "should have default site" do
27
+ Localized::Config.should respond_to :default_site
28
+ Localized::Config.site_to_locale_map.keys.should include(Localized::Config.default_site)
29
+ end
30
+
31
+ it "should have default host prefix" do
32
+ Localized::Config.should respond_to :default_host_prefix
33
+ Localized::Config.default_host_prefix.should == "www."
34
+ end
35
+
36
+ it "should have supported sites" do
37
+ Localized::Config.should respond_to :supported_sites
38
+ Localized::Config.supported_sites.should be_a Array
39
+ Localized::Config.supported_sites.should include(:us)
40
+ end
41
+
42
+ it "should have supported locales" do
43
+ Localized::Config.should respond_to :supported_locales
44
+ Localized::Config.supported_locales.should be_a Array
45
+ Localized::Config.supported_locales.should include(:'en-US')
46
+ end
47
+
48
+ it "should have default locale" do
49
+ Localized::Config.should respond_to :default_locale
50
+ Localized::Config.default_locale.should == :'en-US'
51
+ end
52
+
53
+ end
@@ -0,0 +1,71 @@
1
+ require 'spec_helper'
2
+
3
+ class FoobarController < ::ActionController::Base
4
+ def test
5
+ render :text => 'success'
6
+ end
7
+ end
8
+
9
+ describe FoobarController , "locale specific routes", :type => :controller do
10
+ before :all do
11
+ Rails.stub(:root).and_return(Pathname.new(File.join(File.dirname(__FILE__),'..', '..')))
12
+ Rails.application.routes.draw do
13
+ get '/test' => 'foobar#test', :as => :test
14
+ root :to => "foobar#test"
15
+ end
16
+ end
17
+
18
+ after :all do
19
+ silence_warnings do
20
+ Rails.application.reload_routes!
21
+ end
22
+ end
23
+
24
+ describe "sites and subdomain" do
25
+ before :each do
26
+ @request.host = "www.roadrunnerrecords.test"
27
+ end
28
+
29
+ it "should have the regular tests host" do
30
+ root_url.should == 'http://www.roadrunnerrecords.test/'
31
+ end
32
+
33
+ it "should have the default host prefix and default locale" do
34
+ root_url(:site => 'us').should == 'http://www.roadrunnerrecords.test/'
35
+ end
36
+
37
+ it "should error if site is not supported" do
38
+ expect {
39
+ root_url(:site => 'cz')
40
+ }.to raise_error(ArgumentError)
41
+ end
42
+
43
+ it "should allow for locale override" do
44
+ root_url(:site => 'it').should == 'http://www.it.roadrunnerrecords.test/'
45
+ end
46
+
47
+ it "should set the default site when Locale is set" do
48
+ original_locale = I18n.locale
49
+ I18n.locale = Localized::Config.site_to_locale_map[:es]
50
+ root_url.should == 'http://www.es.roadrunnerrecords.test/'
51
+ I18n.locale = original_locale
52
+ end
53
+
54
+ end
55
+
56
+ describe "subdomain locale" do
57
+
58
+ it "should set the current locale to that of the subdomain" do
59
+ @request.host = "www.us.example.com"
60
+ get :test
61
+ I18n.locale.should == :"en-US"
62
+ end
63
+
64
+ it "should set the current locale to that of the subdomain" do
65
+ @request.host = "www.it.example.com"
66
+ get :test
67
+ I18n.locale.should == :"it-IT"
68
+ end
69
+
70
+ end
71
+ end
@@ -0,0 +1,13 @@
1
+ require "rubygems"
2
+ require "bundler/setup"
3
+ require "ruby-debug"
4
+ require "localized"
5
+ require "action_controller/railtie"
6
+ require 'rspec/rails'
7
+
8
+ class Application < Rails::Application
9
+ # abusing locale for site/locale
10
+ config.i18n.default_locale = :'en-US'
11
+ end
12
+ Application.initialize!
13
+
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: localized
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Paul Hepworth
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-07-09 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: rails
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "3.0"
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: ruby-debug19
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: "0"
36
+ type: :development
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: rspec
40
+ prerelease: false
41
+ requirement: &id003 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - "="
45
+ - !ruby/object:Gem::Version
46
+ version: 2.6.0
47
+ type: :development
48
+ version_requirements: *id003
49
+ - !ruby/object:Gem::Dependency
50
+ name: rspec-rails
51
+ prerelease: false
52
+ requirement: &id004 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - "="
56
+ - !ruby/object:Gem::Version
57
+ version: 2.6.0
58
+ type: :development
59
+ version_requirements: *id004
60
+ description: This gem allows you to set locale using a subdomain and url helper support for switching sites.
61
+ email: paul<dot>hepworth<at>peppyheppy<dot>com
62
+ executables: []
63
+
64
+ extensions: []
65
+
66
+ extra_rdoc_files: []
67
+
68
+ files:
69
+ - .gitignore
70
+ - .rvmrc
71
+ - Gemfile
72
+ - Gemfile.lock
73
+ - README.md
74
+ - Rakefile
75
+ - config/.DS_Store
76
+ - config/defaults.yml
77
+ - lib/localized.rb
78
+ - lib/localized/config.rb
79
+ - lib/localized/helper.rb
80
+ - localized.gemspec
81
+ - spec/config/localized.yml
82
+ - spec/config/routes.rb
83
+ - spec/lib/localized/config_spec.rb
84
+ - spec/lib/localized/helper_spec.rb
85
+ - spec/spec_helper.rb
86
+ has_rdoc: true
87
+ homepage:
88
+ licenses: []
89
+
90
+ post_install_message:
91
+ rdoc_options: []
92
+
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: "0"
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ none: false
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: "0"
107
+ requirements: []
108
+
109
+ rubyforge_project:
110
+ rubygems_version: 1.6.2
111
+ signing_key:
112
+ specification_version: 3
113
+ summary: A ruby on rails gem that provides locale setting support through the subdomain (in a box)
114
+ test_files: []
115
+