translatable_routes 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +46 -0
- data/Rakefile +32 -0
- data/lib/translatable_routes/action_controller/base.rb +38 -0
- data/lib/translatable_routes/action_dispatch/mapper.rb +72 -0
- data/lib/translatable_routes/action_dispatch/named_route_collection.rb +27 -0
- data/lib/translatable_routes/railtie.rb +14 -0
- data/lib/translatable_routes/version.rb +5 -0
- data/lib/translatable_routes.rb +8 -0
- data/test/dummy/README.rdoc +28 -0
- data/test/dummy/Rakefile +6 -0
- data/test/dummy/app/assets/javascripts/application.js +13 -0
- data/test/dummy/app/assets/stylesheets/application.css +13 -0
- data/test/dummy/app/controllers/application_controller.rb +5 -0
- data/test/dummy/app/controllers/namespace/nested_controller.rb +6 -0
- data/test/dummy/app/controllers/namespace/nested_resources_controller.rb +6 -0
- data/test/dummy/app/controllers/pages_controller.rb +6 -0
- data/test/dummy/app/controllers/params_controller.rb +6 -0
- data/test/dummy/app/controllers/resources_controller.rb +6 -0
- data/test/dummy/app/controllers/simple_controller.rb +6 -0
- data/test/dummy/app/helpers/application_helper.rb +2 -0
- data/test/dummy/app/views/layouts/application.html.erb +14 -0
- data/test/dummy/app/views/pages/show.html.erb +0 -0
- data/test/dummy/bin/bundle +3 -0
- data/test/dummy/bin/rails +4 -0
- data/test/dummy/bin/rake +4 -0
- data/test/dummy/config/application.rb +29 -0
- data/test/dummy/config/boot.rb +5 -0
- data/test/dummy/config/database.yml +25 -0
- data/test/dummy/config/environment.rb +5 -0
- data/test/dummy/config/environments/development.rb +29 -0
- data/test/dummy/config/environments/production.rb +80 -0
- data/test/dummy/config/environments/test.rb +36 -0
- data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/test/dummy/config/initializers/inflections.rb +16 -0
- data/test/dummy/config/initializers/mime_types.rb +5 -0
- data/test/dummy/config/initializers/secret_token.rb +12 -0
- data/test/dummy/config/initializers/session_store.rb +3 -0
- data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/test/dummy/config/locales/en-US.yml +9 -0
- data/test/dummy/config/locales/es-AR.yml +9 -0
- data/test/dummy/config/locales/es-UY.yml +9 -0
- data/test/dummy/config/routes.rb +59 -0
- data/test/dummy/config.ru +4 -0
- data/test/dummy/db/development.sqlite3 +0 -0
- data/test/dummy/db/schema.rb +16 -0
- data/test/dummy/log/development.log +7 -0
- data/test/dummy/log/test.log +827 -0
- data/test/dummy/public/404.html +58 -0
- data/test/dummy/public/422.html +58 -0
- data/test/dummy/public/500.html +57 -0
- data/test/dummy/public/favicon.ico +0 -0
- data/test/dummy/tmp/cache/assets/test/sprockets/13fe41fee1fe35b49d145bcc06610705 +0 -0
- data/test/dummy/tmp/cache/assets/test/sprockets/2f5173deea6c795b8fdde723bb4b63af +0 -0
- data/test/dummy/tmp/cache/assets/test/sprockets/357970feca3ac29060c1e3861e2c0953 +0 -0
- data/test/dummy/tmp/cache/assets/test/sprockets/cffd775d018f68ce5dba1ee0d951a994 +0 -0
- data/test/dummy/tmp/cache/assets/test/sprockets/d771ace226fc8215a3572e0aa35bb0d6 +0 -0
- data/test/dummy/tmp/cache/assets/test/sprockets/f7cbd26ba1d28d48de824f0e94586655 +0 -0
- data/test/locale_test.rb +34 -0
- data/test/routes_test.rb +96 -0
- data/test/test_helper.rb +21 -0
- metadata +188 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6d52f4c0975cb626cba3295c879d05aa6b794cbb
|
4
|
+
data.tar.gz: 06a26a45a03184dcf3910c79090c2211ab8d8cc9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 761514992cebab18345209e9d6c436b7e35e721e53da7289da9a8dc9b3e4e344d567d4add505af2f3720ec3c07ada2cad3712f38e39cf74526e53880d325d649
|
7
|
+
data.tar.gz: 2387b86d3771b0128702a2b5b084a440a9de24e22f092d523f031f3d040895a5021fb05a575befec4492c20dc51587b2012bb727326e8f9fff2e8ec28fa44ca6
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2013 Mattways
|
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,46 @@
|
|
1
|
+
{<img src="https://codeclimate.com/github/mattways/translatable_routes.png" />}[https://codeclimate.com/github/mattways/translatable_routes] {<img src="https://secure.travis-ci.org/mattways/translatable_routes.png?branch=master" alt="Build Status" />}[https://travis-ci.org/mattways/translatable_routes] {<img src="https://gemnasium.com/mattways/translatable_routes.png" alt="Dependency Status" />}[https://gemnasium.com/mattways/translatable_routes]
|
2
|
+
|
3
|
+
= Translatable Routes
|
4
|
+
|
5
|
+
Minimalistic toolkit to handle translatable routes and detect locale from subdomain or prefix.
|
6
|
+
|
7
|
+
= Install
|
8
|
+
|
9
|
+
Put this line in your Gemfile:
|
10
|
+
gem 'translatable_routes'
|
11
|
+
|
12
|
+
Then bundle:
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
= Usage
|
16
|
+
|
17
|
+
Choose how the language would be selected in your application.rb:
|
18
|
+
|
19
|
+
To use prefix (default):
|
20
|
+
config.translatable_routes.selection = :prefix
|
21
|
+
|
22
|
+
To use subdomain:
|
23
|
+
config.translatable_routes.selection = :subdomain
|
24
|
+
config.translatable_routes.mapping = {
|
25
|
+
es: [:ar, :uy]
|
26
|
+
pt: [:br]
|
27
|
+
}
|
28
|
+
|
29
|
+
In the case of subdomains you need to name the yml file in your locales dir using this format:
|
30
|
+
es-UY.yml
|
31
|
+
es-AR.yml
|
32
|
+
pt-BR.yml
|
33
|
+
|
34
|
+
Inside each yaml file:
|
35
|
+
es-UY:
|
36
|
+
routes:
|
37
|
+
users: "usuarios"
|
38
|
+
profile: "perfil"
|
39
|
+
login: "entrar"
|
40
|
+
|
41
|
+
(Please note each part of the path it's translated individually, you don't have to put translations for paths like "users/1/profile" just translate "users" and "profile", the plugin will do the rest)
|
42
|
+
|
43
|
+
Finally in your routes.rb pass the routes you want to be localized to localize method:
|
44
|
+
localized do
|
45
|
+
get 'contact', to: 'static#contact', as: 'contact'
|
46
|
+
end
|
data/Rakefile
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'TranslatableRoutes'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.rdoc')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
Bundler::GemHelper.install_tasks
|
21
|
+
|
22
|
+
require 'rake/testtask'
|
23
|
+
|
24
|
+
Rake::TestTask.new(:test) do |t|
|
25
|
+
t.libs << 'lib'
|
26
|
+
t.libs << 'test'
|
27
|
+
t.pattern = 'test/**/*_test.rb'
|
28
|
+
t.verbose = false
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
task default: :test
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module TranslatableRoutes
|
2
|
+
module ActionController
|
3
|
+
module Base
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
prepend_before_filter :select_locale
|
8
|
+
end
|
9
|
+
|
10
|
+
protected
|
11
|
+
|
12
|
+
def select_locale
|
13
|
+
if Rails.application.config.translatable_routes.selection == :subdomain
|
14
|
+
Rails.application.config.translatable_routes.mapping.each_pair do |key, value|
|
15
|
+
if (value.is_a? Array and value.include? request.subdomain.to_sym) or value == request.subdomain.to_sym
|
16
|
+
I18n.locale = "#{key}-#{request.subdomain.upcase}"
|
17
|
+
break
|
18
|
+
end
|
19
|
+
end
|
20
|
+
elsif not params[:locale].nil?
|
21
|
+
I18n.locale = params[:locale]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def subdomains
|
26
|
+
@subdomains ||= begin
|
27
|
+
values = []
|
28
|
+
Rails.application.config.translatable_routes.mapping.values.each do |value|
|
29
|
+
value = [value] unless value.is_a? Array
|
30
|
+
values.concat value
|
31
|
+
end
|
32
|
+
values
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
module TranslatableRoutes
|
2
|
+
module ActionDispatch
|
3
|
+
module Mapper
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
def localized
|
7
|
+
@locales = I18n.available_locales.dup
|
8
|
+
case Rails.application.config.translatable_routes.selection
|
9
|
+
when :prefix
|
10
|
+
scope ':locale' do
|
11
|
+
yield
|
12
|
+
end
|
13
|
+
when :subdomain
|
14
|
+
@locales.shift # For some reason :en is always there
|
15
|
+
yield
|
16
|
+
end
|
17
|
+
@locales = nil # Needed to routes below localize block to work
|
18
|
+
end
|
19
|
+
|
20
|
+
def add_route(action, options)
|
21
|
+
if @locales
|
22
|
+
@locales.each do |locale|
|
23
|
+
path = @scope[:path].dup if @scope[:path].present?
|
24
|
+
path_names = @scope[:path_names].dup
|
25
|
+
@scope[:path] = i18n_path(path, locale) if @scope[:path].present?
|
26
|
+
@scope[:path_names].each { |key, value| @scope[:path_names][key] = I18n.t("routes.#{key}", locale: locale, default: value) }
|
27
|
+
super(*[i18n_path(action, locale), i18n_options(options, locale)])
|
28
|
+
@scope[:path] = path if @scope[:path].present?
|
29
|
+
@scope[:path_names] = path_names
|
30
|
+
end
|
31
|
+
@set.named_routes.define_i18n_route_helper @scope[:as] ? "#{@scope[:as]}_#{options[:as]}" : options[:as] unless @scope[:scope_level_resource].present?
|
32
|
+
return
|
33
|
+
end
|
34
|
+
super
|
35
|
+
end
|
36
|
+
|
37
|
+
protected
|
38
|
+
|
39
|
+
def i18n_options(options, locale)
|
40
|
+
selection = Rails.application.config.translatable_routes.selection
|
41
|
+
subdomain = locale.to_s.split('-')[1].downcase if selection == :subdomain
|
42
|
+
changes = { constraints: selection == :prefix ? { locale: locale.to_s } : { subdomain: subdomain.to_s } }
|
43
|
+
suffix = selection == :prefix ? locale.to_s.gsub('-', '_').downcase : subdomain
|
44
|
+
if options[:as].present?
|
45
|
+
changes[:as] = "#{options[:as]}_#{suffix}"
|
46
|
+
elsif @scope[:scope_level_resource].present?
|
47
|
+
resource = @scope[:scope_level_resource]
|
48
|
+
['singular', 'plural'].each do |context|
|
49
|
+
resource.instance_variable_set "@original_#{context}".to_sym, resource.send(context.to_sym) unless resource.instance_variable_get "@original_#{context}".to_sym
|
50
|
+
resource.instance_variable_set "@#{context}".to_sym, "#{resource.instance_variable_get "@original_#{context}".to_sym}_#{suffix}"
|
51
|
+
end
|
52
|
+
end
|
53
|
+
options[:path] = i18n_path(options[:path], locale) if options[:path].present?
|
54
|
+
options.merge changes
|
55
|
+
end
|
56
|
+
|
57
|
+
def i18n_path(path, locale)
|
58
|
+
unless path.is_a? Symbol
|
59
|
+
i18n_path = []
|
60
|
+
path.split('/').each do |part|
|
61
|
+
next if part == ''
|
62
|
+
i18n_path << ((part[0] == ':' or part[0] == '*') ? part : I18n.t("routes.#{part}", locale: locale, default: part.gsub(/_/, '-')))
|
63
|
+
end
|
64
|
+
i18n_path.join('/')
|
65
|
+
else
|
66
|
+
path
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module TranslatableRoutes
|
2
|
+
module ActionDispatch
|
3
|
+
module NamedRouteCollection
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
def define_i18n_route_helper(name)
|
7
|
+
['url', 'path'].each do |kind|
|
8
|
+
helper = :"#{name}_#{kind}"
|
9
|
+
@module.remove_possible_method helper
|
10
|
+
@module.module_eval do
|
11
|
+
define_method helper do |*args|
|
12
|
+
options = args.extract_options!
|
13
|
+
if Rails.application.config.translatable_routes.selection == :subdomain
|
14
|
+
suffix = (options[:subdomain] ? options[:subdomain] : request.subdomain)
|
15
|
+
else
|
16
|
+
suffix = (options[:locale] ? options[:locale] : I18n.locale).to_s.gsub('-', '_').downcase
|
17
|
+
end
|
18
|
+
send ("#{name}_" + suffix.to_s + "_#{kind}"), *(args << options)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
helpers << helper
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module TranslatableRoutes
|
2
|
+
class Railtie < Rails::Railtie
|
3
|
+
|
4
|
+
config.translatable_routes = ActiveSupport::OrderedOptions.new
|
5
|
+
config.translatable_routes.selection = :prefix
|
6
|
+
|
7
|
+
initializer 'translatable_routes' do
|
8
|
+
::ActionDispatch::Routing::Mapper.send :include, TranslatableRoutes::ActionDispatch::Mapper
|
9
|
+
::ActionDispatch::Routing::RouteSet::NamedRouteCollection.send :include, TranslatableRoutes::ActionDispatch::NamedRouteCollection
|
10
|
+
::ActionController::Base.send :include, TranslatableRoutes::ActionController::Base
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,8 @@
|
|
1
|
+
require 'translatable_routes/action_controller/base'
|
2
|
+
require 'translatable_routes/action_dispatch/named_route_collection'
|
3
|
+
require 'translatable_routes/action_dispatch/mapper'
|
4
|
+
require 'translatable_routes/railtie'
|
5
|
+
require 'translatable_routes/version'
|
6
|
+
|
7
|
+
module TranslatableRoutes
|
8
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
== README
|
2
|
+
|
3
|
+
This README would normally document whatever steps are necessary to get the
|
4
|
+
application up and running.
|
5
|
+
|
6
|
+
Things you may want to cover:
|
7
|
+
|
8
|
+
* Ruby version
|
9
|
+
|
10
|
+
* System dependencies
|
11
|
+
|
12
|
+
* Configuration
|
13
|
+
|
14
|
+
* Database creation
|
15
|
+
|
16
|
+
* Database initialization
|
17
|
+
|
18
|
+
* How to run the test suite
|
19
|
+
|
20
|
+
* Services (job queues, cache servers, search engines, etc.)
|
21
|
+
|
22
|
+
* Deployment instructions
|
23
|
+
|
24
|
+
* ...
|
25
|
+
|
26
|
+
|
27
|
+
Please feel free to use a different markup language if you do not plan to run
|
28
|
+
<tt>rake doc:app</tt>.
|
data/test/dummy/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
// This is a manifest file that'll be compiled into application.js, which will include all the files
|
2
|
+
// listed below.
|
3
|
+
//
|
4
|
+
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
|
5
|
+
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
|
6
|
+
//
|
7
|
+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
8
|
+
// compiled file.
|
9
|
+
//
|
10
|
+
// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
|
11
|
+
// about supported directives.
|
12
|
+
//
|
13
|
+
//= require_tree .
|
@@ -0,0 +1,13 @@
|
|
1
|
+
/*
|
2
|
+
* This is a manifest file that'll be compiled into application.css, which will include all the files
|
3
|
+
* listed below.
|
4
|
+
*
|
5
|
+
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
|
6
|
+
* or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
|
7
|
+
*
|
8
|
+
* You're free to add application-wide styles to this file and they'll appear at the top of the
|
9
|
+
* compiled file, but it's generally better to create a new file per style scope.
|
10
|
+
*
|
11
|
+
*= require_self
|
12
|
+
*= require_tree .
|
13
|
+
*/
|
@@ -0,0 +1,14 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title>Dummy</title>
|
5
|
+
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
|
6
|
+
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
|
7
|
+
<%= csrf_meta_tags %>
|
8
|
+
</head>
|
9
|
+
<body>
|
10
|
+
|
11
|
+
<%= yield %>
|
12
|
+
|
13
|
+
</body>
|
14
|
+
</html>
|
File without changes
|