i18n_url 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2011 YOURNAME
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.
data/README.rdoc ADDED
@@ -0,0 +1,48 @@
1
+ = I18n Url
2
+
3
+ I18n_url is a plugin for Ruby on Rails that lets you easily translate your routes trough the I18n api
4
+
5
+ Works with Rails 3.0 and 3.1
6
+
7
+ = How to use
8
+
9
+ Just add gem "i18n_url" in your gemfile
10
+
11
+ This provide you a new method your route file : i18n_url
12
+
13
+ Exemple of use:
14
+
15
+ i18n_url do
16
+ match "home" => "users#edit", as: :my_home
17
+ end
18
+
19
+ All routes in the block will generate for each locale a new route :
20
+
21
+ For :en by exemple :
22
+
23
+ en_home => /en/home
24
+
25
+ This plugin also provides an helper for your controller : extract_locale_from_url
26
+ So you have juste to do :
27
+
28
+ class ApplicationController < ActionController::Base
29
+
30
+ before_filter :set_locale
31
+
32
+
33
+ def set_locale
34
+ I18n.locale = extract_locale_from_url
35
+ end
36
+
37
+ end
38
+
39
+ You're locale file have to be like this :
40
+ en:
41
+ routes:
42
+ home: home
43
+
44
+
45
+ This also provide an helper
46
+ = Contributors
47
+
48
+ *Anthony Laibe
data/Rakefile ADDED
@@ -0,0 +1,28 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'I18nUrl'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+ APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
24
+ load 'rails/tasks/engine.rake'
25
+
26
+
27
+ Bundler::GemHelper.install_tasks
28
+
@@ -0,0 +1,8 @@
1
+ module I18nUrl
2
+ require "i18n_url/routes/mapper"
3
+ require "i18n_url/routes/route_set"
4
+ require "i18n_url/extract_locale"
5
+ class Engine < Rails::Engine
6
+
7
+ end
8
+ end
@@ -0,0 +1,17 @@
1
+ require 'action_controller'
2
+
3
+ module I18nUrl::ExtractLocale
4
+ extend ActiveSupport::Concern
5
+
6
+ module InstanceMethods
7
+ def extract_locale_from_url
8
+ path = request.fullpath
9
+ path.gsub(/^\//, '').split("/").first
10
+ end
11
+ end
12
+
13
+ end
14
+
15
+ class ActionController::Base
16
+ include I18nUrl::ExtractLocale
17
+ end
@@ -0,0 +1,56 @@
1
+ require "action_dispatch"
2
+ module ActionDispatch::Routing
3
+ class Mapper
4
+
5
+ def i18n_url(&block)
6
+ @locales = I18n.available_locales
7
+ yield
8
+ @locales = nil
9
+ end
10
+
11
+ module Base
12
+ def match(path, options)
13
+ if @locales
14
+ @locales.each do |locale|
15
+ mapping = MappingLocalized.new(locale, @set, @scope, path, options || {})
16
+ app, conditions, requirements, defaults, as, anchor = mapping.to_route
17
+ @set.add_route(app, conditions, requirements, defaults, as, anchor)
18
+ end
19
+ @set.name_route_proxy options[:as]
20
+ else
21
+ mapping = Mapping.new(@set, @scope, path, options || {})
22
+ app, conditions, requirements, defaults, as, anchor = mapping.to_route
23
+ @set.add_route(app, conditions, requirements, defaults, as, anchor)
24
+ end
25
+ end
26
+ end
27
+
28
+ class MappingLocalized < Mapping
29
+
30
+ def initialize(locale, set, scope, path, options)
31
+ @locale = locale
32
+ I18n.locale = locale
33
+ path.gsub!(/^\//, '')
34
+ format = '(.:format)'
35
+ path.gsub!( format, '')
36
+ translate_route path
37
+ path = "/#{@locale}/" + @localized_path.join("/")
38
+ super(set, scope, path, options)
39
+ localized_name_route
40
+ end
41
+
42
+ def localized_name_route
43
+ original_name = @options[:as]
44
+ @options[:as] = "#{@locale}_#{original_name}" if original_name
45
+ end
46
+
47
+ def translate_route path
48
+ path.split("/").each do |word|
49
+ next if word[0] == ":"
50
+ (@localized_path ||= []) << I18n.t(word,:scope => :routes, :default => word)
51
+ end
52
+ end
53
+ end
54
+
55
+ end
56
+ end
@@ -0,0 +1,39 @@
1
+ module ActionDispatch
2
+ module Routing
3
+ class RouteSet
4
+
5
+ class NamedRouteCollection
6
+
7
+ def add(name)
8
+ routes[name.to_sym] = nil
9
+ define_proxy_named_route(name)
10
+ end
11
+
12
+ def define_proxy_named_route(name)
13
+ {:url => {:only_path => false}, :path => {:only_path => true}}.each do |kind, opts|
14
+ define_proxy_helper name, kind, hash
15
+ end
16
+ end
17
+
18
+
19
+ def define_proxy_helper(name, kind, hash)
20
+ selector = url_helper_name(name, kind)
21
+ hash_access_method = hash_access_name(name, kind)
22
+ @module.module_eval <<-END_EVAL, __FILE__, __LINE__ + 1
23
+ remove_possible_method :#{selector}
24
+ def #{selector}(*args)
25
+ send(I18n.locale.to_s+"_#{selector}", *args)
26
+ end
27
+ END_EVAL
28
+ helpers << selector
29
+ end
30
+
31
+ end
32
+
33
+ def name_route_proxy(name = nil)
34
+ named_routes.add(name) if name
35
+ end
36
+
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,3 @@
1
+ module I18nUrl
2
+ VERSION = "1.0.0"
3
+ end
data/lib/i18n_url.rb ADDED
@@ -0,0 +1,4 @@
1
+ require "i18n_url/engine"
2
+
3
+ module I18nUrl
4
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :i18n_url do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: i18n_url
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Anthony Laibe
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-09-25 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: &70219792521140 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 3.1.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70219792521140
25
+ description: I18n url module for Rails 3. Translate your routes easily
26
+ email:
27
+ - anthony.laibe@gmail.com
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - lib/i18n_url/engine.rb
33
+ - lib/i18n_url/extract_locale.rb
34
+ - lib/i18n_url/routes/mapper.rb
35
+ - lib/i18n_url/routes/route_set.rb
36
+ - lib/i18n_url/version.rb
37
+ - lib/i18n_url.rb
38
+ - lib/tasks/i18n_url_tasks.rake
39
+ - MIT-LICENSE
40
+ - Rakefile
41
+ - README.rdoc
42
+ homepage: https://github.com/alaibe/i18n_url
43
+ licenses: []
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubyforge_project:
62
+ rubygems_version: 1.8.10
63
+ signing_key:
64
+ specification_version: 3
65
+ summary: I18n url module for Rails 3. Translate your routes easily
66
+ test_files: []