localization-middleware 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use --create 1.9.2@localization-middleware
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.8.7
4
+ - 1.9.2
5
+ - 1.9.3
data/Gemfile ADDED
@@ -0,0 +1,19 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "i18n"
4
+
5
+ group :test do
6
+ gem "padrino"
7
+ gem "test-spec"
8
+ gem "yard", "~> 0.6.0"
9
+ gem "bundler", "~> 1.0.0"
10
+ gem "jeweler", "~> 1.6.4"
11
+
12
+ gem "debugger", "~> 1.1.3", :platform => :ruby_19
13
+ gem "simplecov", "~> 0.6.4", :platform => :ruby_19
14
+
15
+ gem 'ruby-debug', :platform => :ruby_18
16
+ gem "rcov", "~> 1.0.0", :platform => :ruby_18
17
+
18
+ gem "capybara"
19
+ end
@@ -0,0 +1,127 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activesupport (3.2.8)
5
+ i18n (~> 0.6)
6
+ multi_json (~> 1.0)
7
+ addressable (2.3.2)
8
+ capybara (1.1.2)
9
+ mime-types (>= 1.16)
10
+ nokogiri (>= 1.3.3)
11
+ rack (>= 1.0.0)
12
+ rack-test (>= 0.5.4)
13
+ selenium-webdriver (~> 2.0)
14
+ xpath (~> 0.1.4)
15
+ childprocess (0.3.5)
16
+ ffi (~> 1.0, >= 1.0.6)
17
+ columnize (0.3.6)
18
+ debugger (1.1.4)
19
+ columnize (>= 0.3.1)
20
+ debugger-linecache (~> 1.1.1)
21
+ debugger-ruby_core_source (~> 1.1.3)
22
+ debugger-linecache (1.1.2)
23
+ debugger-ruby_core_source (>= 1.1.1)
24
+ debugger-ruby_core_source (1.1.3)
25
+ ffi (1.1.5)
26
+ git (1.2.5)
27
+ http_router (0.10.2)
28
+ rack (>= 1.0.0)
29
+ url_mount (~> 0.2.1)
30
+ i18n (0.6.0)
31
+ jeweler (1.6.4)
32
+ bundler (~> 1.0)
33
+ git (>= 1.2.5)
34
+ rake
35
+ libwebsocket (0.1.5)
36
+ addressable
37
+ linecache (0.46)
38
+ rbx-require-relative (> 0.0.4)
39
+ mail (2.3.3)
40
+ i18n (>= 0.4.0)
41
+ mime-types (~> 1.16)
42
+ treetop (~> 1.4.8)
43
+ mime-types (1.19)
44
+ multi_json (1.3.6)
45
+ nokogiri (1.5.5)
46
+ padrino (0.10.7)
47
+ padrino-admin (= 0.10.7)
48
+ padrino-cache (= 0.10.7)
49
+ padrino-core (= 0.10.7)
50
+ padrino-gen (= 0.10.7)
51
+ padrino-helpers (= 0.10.7)
52
+ padrino-mailer (= 0.10.7)
53
+ padrino-admin (0.10.7)
54
+ padrino-core (= 0.10.7)
55
+ padrino-helpers (= 0.10.7)
56
+ padrino-cache (0.10.7)
57
+ padrino-core (= 0.10.7)
58
+ padrino-core (0.10.7)
59
+ activesupport (~> 3.2.0)
60
+ http_router (~> 0.10.2)
61
+ sinatra (~> 1.3.1)
62
+ thor (~> 0.15.2)
63
+ tilt (~> 1.3.0)
64
+ padrino-gen (0.10.7)
65
+ bundler (~> 1.0)
66
+ padrino-core (= 0.10.7)
67
+ padrino-helpers (0.10.7)
68
+ i18n (~> 0.6)
69
+ padrino-core (= 0.10.7)
70
+ padrino-mailer (0.10.7)
71
+ mail (~> 2.3.0)
72
+ padrino-core (= 0.10.7)
73
+ polyglot (0.3.3)
74
+ rack (1.4.1)
75
+ rack-protection (1.2.0)
76
+ rack
77
+ rack-test (0.6.1)
78
+ rack (>= 1.0)
79
+ rake (0.9.2.2)
80
+ rbx-require-relative (0.0.9)
81
+ rcov (1.0.0)
82
+ ruby-debug (0.10.4)
83
+ columnize (>= 0.1)
84
+ ruby-debug-base (~> 0.10.4.0)
85
+ ruby-debug-base (0.10.4)
86
+ linecache (>= 0.3)
87
+ rubyzip (0.9.9)
88
+ selenium-webdriver (2.25.0)
89
+ childprocess (>= 0.2.5)
90
+ libwebsocket (~> 0.1.3)
91
+ multi_json (~> 1.0)
92
+ rubyzip
93
+ simplecov (0.6.4)
94
+ multi_json (~> 1.0)
95
+ simplecov-html (~> 0.5.3)
96
+ simplecov-html (0.5.3)
97
+ sinatra (1.3.3)
98
+ rack (~> 1.3, >= 1.3.6)
99
+ rack-protection (~> 1.2)
100
+ tilt (~> 1.3, >= 1.3.3)
101
+ test-spec (0.10.0)
102
+ thor (0.15.4)
103
+ tilt (1.3.3)
104
+ treetop (1.4.10)
105
+ polyglot
106
+ polyglot (>= 0.3.1)
107
+ url_mount (0.2.1)
108
+ rack
109
+ xpath (0.1.4)
110
+ nokogiri (~> 1.3)
111
+ yard (0.6.8)
112
+
113
+ PLATFORMS
114
+ ruby
115
+
116
+ DEPENDENCIES
117
+ bundler (~> 1.0.0)
118
+ capybara
119
+ debugger (~> 1.1.3)
120
+ i18n
121
+ jeweler (~> 1.6.4)
122
+ padrino
123
+ rcov (~> 1.0.0)
124
+ ruby-debug
125
+ simplecov (~> 0.6.4)
126
+ test-spec
127
+ yard (~> 0.6.0)
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 E-Max
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,36 @@
1
+ = localization-middleware
2
+
3
+ The gem that allows to manipulate I18n.locale via url.
4
+
5
+ == Usage
6
+
7
+ * First install the gem
8
+
9
+ With Bundle
10
+
11
+ #Gemfile
12
+ gem "padrino-localization"
13
+
14
+ #irb
15
+ bundle install
16
+
17
+ or with gem
18
+
19
+ gem install padrino-localization
20
+
21
+ * Now it should be added to middleware(Padrino example)
22
+
23
+ Padrino.use(Localization::Middleware, __DEFINE_HERE_LOCALES__)
24
+
25
+ # config/apps.rb
26
+ Padrino.use(Localization::Middleware, %w(en ru))
27
+
28
+ * And that's it. You now can access your application via /page_path, /en/page_path or /ru/page_path
29
+ (in case you specified only tow locales: russian ang english). If no locale was defined, then the
30
+ I18n.default_locale will be used.
31
+
32
+ == Copyright
33
+
34
+ Copyright (c) 2012 kot-begemot. See LICENSE.txt for
35
+ further details.
36
+
@@ -0,0 +1,38 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "localization-middleware"
18
+ gem.homepage = "http://github.com/kot-begemot/localization-middleware"
19
+ gem.license = "MIT"
20
+ gem.summary = "Localize your application via url"
21
+ gem.description = "Middleware that plays the connection between the url and I18n."
22
+ gem.email = "max@studentify.nl"
23
+ gem.authors = ["E-Max"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'test'
31
+ test.pattern = 'test/**/*_test.rb'
32
+ test.verbose = true
33
+ end
34
+
35
+ task :default => :test
36
+
37
+ require 'yard'
38
+ YARD::Rake::YardocTask.new
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,94 @@
1
+ module Localization
2
+ class Middleware
3
+ attr_reader :languages
4
+ alias_method :langs, :languages
5
+
6
+ class << self
7
+ def languages
8
+ @languages
9
+ end
10
+ end
11
+
12
+ def initialize(app, langs)
13
+ @app = app
14
+ self.languages = langs
15
+ end
16
+
17
+ def call(env)
18
+ @env = env
19
+ set_locale
20
+ response_array = @app.call(@env)
21
+ unset_locale
22
+ response_array
23
+ end
24
+
25
+ private
26
+
27
+ def languages= langs
28
+ @languages = langs.collect do |l|
29
+ raise ArgumentError.new("An arguments should be 2 latters long") unless l.to_s =~ /\A[a-z]{2}\Z/i
30
+ l.to_s
31
+ end
32
+ self.class.instance_variable_set :@languages, @languages
33
+ @languages
34
+ end
35
+ alias_method :langs=, :languages=
36
+
37
+ def locale_pattern
38
+ @locale_pattern ||= /\A(?:\/(#{langs.join('|')}))?(\/?.*|)/i
39
+ end
40
+
41
+ def set_locale
42
+ ::I18n.locale = locale_from_url.to_sym if locale_defined_in_url?
43
+ end
44
+
45
+ def unset_locale
46
+ ::I18n.locale = ::I18n.default_locale if locale_defined_in_url?
47
+ @locale_set = @locale = nil
48
+ end
49
+
50
+ ###
51
+ # Determine locale from the request URL and return it as a string, it it
52
+ # is matching any of provided locales
53
+ # If locale matching failed an empty String will be returned.
54
+ #
55
+ # Examples:
56
+ #
57
+ # #http://www.example.com/se
58
+ # locale_from_url # => 'se'
59
+ #
60
+ # #http://www.example.com/de/posts
61
+ # locale_from_url # => 'se'
62
+ #
63
+ # #http://www.example.com/ursers
64
+ # locale_from_url # => ''
65
+ #
66
+ def locale_from_url path_info = nil
67
+ @locale_set ||= begin
68
+ @locale ||= (match = (path_info || @env['PATH_INFO']).match(locale_pattern)) ? match[1] : nil
69
+ @env['PATH_INFO'] = match[2] if match && @env.try(:fetch, 'PATH_INFO')
70
+ true
71
+ end
72
+ @locale
73
+ end
74
+
75
+ ###
76
+ # Try to determine, weather the locale is defined, from request URL, if it is matching any of
77
+ # provided locales, and returns boolean as a result.
78
+ #
79
+ # Examples:
80
+ #
81
+ # #http://www.example.com/se
82
+ # locale_from_url # => true
83
+ #
84
+ # #http://www.example.com/de/posts
85
+ # locale_from_url # => true
86
+ #
87
+ # #http://www.example.com/ursers
88
+ # locale_from_url # => false
89
+ #
90
+ def locale_defined_in_url?
91
+ !locale_from_url.blank?
92
+ end
93
+ end
94
+ end
@@ -0,0 +1 @@
1
+ require 'localization-middleware'
@@ -0,0 +1,62 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "localization-middleware"
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["E-Max"]
12
+ s.date = "2012-08-26"
13
+ s.description = "Middleware that plays the connection between the url and I18n."
14
+ s.email = "max@studentify.nl"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rvmrc",
22
+ ".travis.yml",
23
+ "Gemfile",
24
+ "Gemfile.lock",
25
+ "LICENSE.txt",
26
+ "README.rdoc",
27
+ "Rakefile",
28
+ "VERSION",
29
+ "lib/localization-middleware.rb",
30
+ "lib/localization_middleware.rb",
31
+ "localization-middleware.gemspec",
32
+ "test/middleware_test.rb",
33
+ "test/test_app/.components",
34
+ "test/test_app/app/app.rb",
35
+ "test/test_app/app/controllers/main.rb",
36
+ "test/test_app/app/controllers/user.rb",
37
+ "test/test_app/app/helpers/application_helper.rb",
38
+ "test/test_app/config.ru",
39
+ "test/test_app/config/apps.rb",
40
+ "test/test_app/config/boot.rb",
41
+ "test/test_app/public/favicon.ico",
42
+ "test/test_helper.rb"
43
+ ]
44
+ s.homepage = "http://github.com/kot-begemot/localization-middleware"
45
+ s.licenses = ["MIT"]
46
+ s.require_paths = ["lib"]
47
+ s.rubygems_version = "1.8.10"
48
+ s.summary = "Localize your application via url"
49
+
50
+ if s.respond_to? :specification_version then
51
+ s.specification_version = 3
52
+
53
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
54
+ s.add_runtime_dependency(%q<i18n>, [">= 0"])
55
+ else
56
+ s.add_dependency(%q<i18n>, [">= 0"])
57
+ end
58
+ else
59
+ s.add_dependency(%q<i18n>, [">= 0"])
60
+ end
61
+ end
62
+
@@ -0,0 +1,35 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/test_helper')
2
+
3
+ class MiddlewareTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @middleware = Localization::Middleware.new nil, [:en,:ru]
7
+ end
8
+
9
+ def test_root_matching_without_locale
10
+ assert_nil @middleware.send :locale_from_url, '/'
11
+ assert_equal ::I18n.default_locale, ::I18n.locale
12
+ end
13
+
14
+ def test_set_correct_locale
15
+ assert_equal 'en', @middleware.send(:locale_from_url, '/en')
16
+ assert_equal :en, @middleware.send(:set_locale)
17
+ assert_equal :en, ::I18n.locale
18
+
19
+ @middleware.send(:unset_locale)
20
+ assert_equal ::I18n.default_locale, ::I18n.locale
21
+
22
+ assert_equal 'ru', @middleware.send(:locale_from_url, '/ru')
23
+ assert_equal :ru, @middleware.send(:set_locale)
24
+ assert_equal :ru, ::I18n.locale
25
+
26
+ @middleware.send(:unset_locale)
27
+ assert_equal ::I18n.default_locale, ::I18n.locale
28
+ end
29
+
30
+ def test_set_incorrect_locale
31
+ assert_equal nil, @middleware.send(:locale_from_url, '/test')
32
+ assert_equal nil, @middleware.send(:set_locale)
33
+ assert_equal ::I18n.default_locale, ::I18n.locale
34
+ end
35
+ end
@@ -0,0 +1,7 @@
1
+ ---
2
+ :orm: none
3
+ :test: none
4
+ :mock: none
5
+ :script: none
6
+ :renderer: haml
7
+ :stylesheet: none
@@ -0,0 +1,7 @@
1
+ class TestApp < Padrino::Application
2
+ register Padrino::Helpers
3
+
4
+ error 404 do
5
+ '404 Error'
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ TestApp.controller do
2
+ get :index do
3
+ t 'main.index'
4
+ end
5
+
6
+ get :welcome do
7
+ t 'main.welcome'
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ TestApp.controller :user do
2
+ get :index do
3
+ t 'user.index'
4
+ end
5
+
6
+ get :show, :with => [:id] do
7
+ "ID is %{id}" % { :id => params[:id]}
8
+ end
9
+ end
@@ -0,0 +1,2 @@
1
+ TestApp.helpers do
2
+ end
@@ -0,0 +1,2 @@
1
+ require ::File.dirname(__FILE__) + '/config/boot.rb'
2
+ run Padrino.application
@@ -0,0 +1,27 @@
1
+ ##
2
+ # This file mounts each app in the Padrino project to a specified sub-uri.
3
+ # You can mount additional applications using any of these commands below:
4
+ #
5
+ # Padrino.mount("blog").to('/blog')
6
+ # Padrino.mount("blog", :app_class => "BlogApp").to('/blog')
7
+ # Padrino.mount("blog", :app_file => "path/to/blog/app.rb").to('/blog')
8
+ #
9
+ # You can also map apps to a specified host:
10
+ #
11
+ # Padrino.mount("Admin").host("admin.example.org")
12
+ # Padrino.mount("WebSite").host(/.*\.?example.org/)
13
+ # Padrino.mount("Foo").to("/foo").host("bar.example.org")
14
+ #
15
+ # Note 1: Mounted apps (by default) should be placed into the project root at '/app_name'.
16
+ # Note 2: If you use the host matching remember to respect the order of the rules.
17
+ #
18
+ # By default, this file mounts the parimary app which was generated with this project.
19
+ # However, the mounted app can be modified as needed:
20
+ #
21
+ # Padrino.mount(:app_file => "path/to/file", :app_class => "Blog").to('/')
22
+ #
23
+
24
+ Padrino.use(Localization::Middleware, ['en' , 'ru'])
25
+
26
+ # Mounts the core application for this project
27
+ Padrino.mount("TestApp").to('/')
@@ -0,0 +1,23 @@
1
+ require 'bundler'
2
+ Bundler.setup(:default, :test)
3
+ require 'padrino'
4
+
5
+ # Defines our constants
6
+ PADRINO_ENV = ENV["PADRINO_ENV"] ||= ENV["RACK_ENV"] ||= "development" unless defined?(PADRINO_ENV)
7
+ PADRINO_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
8
+
9
+
10
+ ##
11
+ # Add here your before load hooks
12
+ #
13
+ Padrino.before_load do
14
+ ::I18n.default_locale = :en
15
+ end
16
+
17
+ ##
18
+ # Add here your after load hooks
19
+ #
20
+ Padrino.after_load do
21
+ end
22
+
23
+ Padrino.load!
File without changes
@@ -0,0 +1,25 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'capybara'
4
+ require 'capybara/dsl'
5
+ if RUBY_VERSION >= '1.9.0'
6
+ require "debugger"
7
+ else
8
+ require 'ruby-debug'
9
+ end
10
+
11
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
12
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
13
+ require 'localization-middleware'
14
+
15
+ require File.join(File.dirname(__FILE__), 'test_app', 'config', 'boot')
16
+
17
+ I18n.locale = :en
18
+
19
+ class Test::Unit::TestCase
20
+ include Capybara::DSL
21
+
22
+ def app
23
+ @app || Padrino.application
24
+ end
25
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: localization-middleware
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - E-Max
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-26 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: i18n
16
+ requirement: &84176770 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *84176770
25
+ description: Middleware that plays the connection between the url and I18n.
26
+ email: max@studentify.nl
27
+ executables: []
28
+ extensions: []
29
+ extra_rdoc_files:
30
+ - LICENSE.txt
31
+ - README.rdoc
32
+ files:
33
+ - .document
34
+ - .rvmrc
35
+ - .travis.yml
36
+ - Gemfile
37
+ - Gemfile.lock
38
+ - LICENSE.txt
39
+ - README.rdoc
40
+ - Rakefile
41
+ - VERSION
42
+ - lib/localization-middleware.rb
43
+ - lib/localization_middleware.rb
44
+ - localization-middleware.gemspec
45
+ - test/middleware_test.rb
46
+ - test/test_app/.components
47
+ - test/test_app/app/app.rb
48
+ - test/test_app/app/controllers/main.rb
49
+ - test/test_app/app/controllers/user.rb
50
+ - test/test_app/app/helpers/application_helper.rb
51
+ - test/test_app/config.ru
52
+ - test/test_app/config/apps.rb
53
+ - test/test_app/config/boot.rb
54
+ - test/test_app/public/favicon.ico
55
+ - test/test_helper.rb
56
+ homepage: http://github.com/kot-begemot/localization-middleware
57
+ licenses:
58
+ - MIT
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ segments:
70
+ - 0
71
+ hash: -871748497
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 1.8.10
81
+ signing_key:
82
+ specification_version: 3
83
+ summary: Localize your application via url
84
+ test_files: []