route_translator 2.0.1 → 3.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/lib/route_translator.rb +11 -50
- data/lib/route_translator/extensions.rb +3 -0
- data/lib/route_translator/extensions/action_controller.rb +11 -0
- data/lib/route_translator/extensions/mapper.rb +52 -0
- data/lib/route_translator/extensions/route_set.rb +13 -0
- data/lib/route_translator/translator.rb +84 -0
- data/lib/route_translator/version.rb +1 -1
- data/test/dummy/app/controllers/dummy_controller.rb +5 -0
- data/test/dummy/config.ru +4 -0
- data/test/dummy/config/application.rb +13 -0
- data/test/dummy/config/boot.rb +0 -0
- data/test/dummy/config/environment.rb +5 -0
- data/test/dummy/config/initializers/secret_token.rb +7 -0
- data/test/dummy/config/initializers/session_store.rb +8 -0
- data/test/dummy/config/locales/all.yml +9 -0
- data/test/dummy/config/routes.rb +6 -0
- data/test/dummy/dummy_mounted_app.rb +6 -0
- data/test/dummy/script/rails +6 -0
- data/test/integration_test.rb +23 -0
- data/test/locales/routes.yml +2 -6
- data/test/{route_translator_test.rb → routing_test.rb} +110 -193
- data/test/test_helper.rb +65 -49
- metadata +39 -18
- data/lib/route_translator/railtie.rb +0 -23
- data/lib/route_translator/route_set.rb +0 -50
- data/lib/route_translator/route_set/dictionary_management.rb +0 -76
- data/lib/route_translator/route_set/helpers.rb +0 -29
- data/lib/route_translator/route_set/translator.rb +0 -161
- data/lib/route_translator/routes_reloader.rb +0 -32
- data/lib/route_translator/test_request.rb +0 -11
- data/lib/tasks/route_translator.rake +0 -35
data/test/test_helper.rb
CHANGED
@@ -1,91 +1,107 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'minitest/mock'
|
3
|
+
require 'minitest/unit'
|
1
4
|
|
2
|
-
|
3
|
-
|
4
|
-
'es' => { 'people' => 'gente'},
|
5
|
-
'fr' => {} # empty on purpose to test behaviour on incompleteness scenarios
|
6
|
-
}
|
5
|
+
require "rails"
|
6
|
+
require "action_controller/railtie"
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
8
|
+
require 'route_translator'
|
9
|
+
|
10
|
+
module ActionDispatch
|
11
|
+
class TestRequest < Request
|
12
|
+
def initialize(env = {})
|
13
|
+
super(DEFAULT_ENV.merge(env))
|
13
14
|
|
14
|
-
|
15
|
-
|
15
|
+
self.host = 'test.host'
|
16
|
+
self.remote_addr = '0.0.0.0'
|
17
|
+
self.user_agent = 'Rails Testing'
|
18
|
+
end
|
16
19
|
end
|
17
20
|
end
|
18
21
|
|
19
22
|
module RouteTranslator
|
20
23
|
module TestHelper
|
21
|
-
def
|
22
|
-
|
23
|
-
|
24
|
+
def draw_routes(&block)
|
25
|
+
@routes.draw(&block)
|
26
|
+
if @routes.respond_to?(:install_helpers)
|
27
|
+
@routes.install_helpers
|
28
|
+
else
|
29
|
+
ActionView::Base.send(:include, @routes.url_helpers)
|
30
|
+
ActionController::Base.send(:include, @routes.url_helpers)
|
31
|
+
end
|
24
32
|
end
|
25
33
|
|
26
|
-
def
|
27
|
-
|
34
|
+
def config_default_locale_settings(locale)
|
35
|
+
I18n.default_locale = locale
|
28
36
|
end
|
29
37
|
|
30
|
-
def
|
31
|
-
|
38
|
+
def config_force_locale(boolean)
|
39
|
+
RouteTranslator.config.force_locale = boolean
|
32
40
|
end
|
33
41
|
|
34
|
-
def
|
35
|
-
|
42
|
+
def config_generate_unlocalized_routes(boolean)
|
43
|
+
RouteTranslator.config.generate_unlocalized_routes = boolean
|
44
|
+
end
|
36
45
|
|
37
|
-
|
38
|
-
|
39
|
-
app.paths["log"] = "#{tmp_path}/log/test.log"
|
40
|
-
app.paths["config/routes"] = File.join(app_path, routes_config)
|
41
|
-
app.initialize!
|
42
|
-
Rails.application = app
|
46
|
+
def config_translation_file (file)
|
47
|
+
RouteTranslator.config.translation_file = file
|
43
48
|
end
|
44
49
|
|
45
|
-
def
|
46
|
-
|
50
|
+
def path_string(route)
|
51
|
+
path = route.respond_to?(:path) ? route.path : route.to_s.split(' ')[1]
|
52
|
+
path.respond_to?(:spec) ? path.spec.to_s : path.to_s
|
47
53
|
end
|
48
54
|
|
49
|
-
def
|
50
|
-
@
|
51
|
-
File.join(@tmp_path, *args)
|
55
|
+
def named_route(name)
|
56
|
+
@routes.routes.detect{ |r| r.name == name }
|
52
57
|
end
|
53
58
|
|
54
|
-
def
|
55
|
-
|
59
|
+
def formatted_root_route?
|
60
|
+
b = ActionPack::VERSION::MAJOR == 3 && ActionPack::VERSION::MINOR > 0
|
61
|
+
b ||= ActionPack::VERSION::MAJOR == 4
|
62
|
+
!b
|
56
63
|
end
|
57
64
|
|
58
|
-
def
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
65
|
+
def print_routes(route_set)
|
66
|
+
all_routes = route_set.routes
|
67
|
+
|
68
|
+
routes = all_routes.collect do |route|
|
69
|
+
|
70
|
+
reqs = route.requirements.dup
|
71
|
+
reqs[:to] = route.app unless route.app.class.name.to_s =~ /^ActionDispatch::Routing/
|
72
|
+
reqs = reqs.empty? ? "" : reqs.inspect
|
73
|
+
|
74
|
+
{:name => route.name.to_s, :verb => route.verb.to_s, :path => route.path.try(:spec).to_s, :reqs => reqs}
|
63
75
|
end
|
64
|
-
app.routes_reloader.paths.unshift(fn)
|
65
|
-
end
|
66
76
|
|
67
|
-
|
68
|
-
|
69
|
-
|
77
|
+
name_width = routes.map{ |r| r[:name].length }.max
|
78
|
+
verb_width = routes.map{ |r| r[:verb].length }.max
|
79
|
+
path_width = routes.map{ |r| r[:path].to_s.length }.max
|
70
80
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
puts inspector.format(route_set.routes, ENV['CONTROLLER']).join "\n"
|
81
|
+
routes.each do |r|
|
82
|
+
puts "#{r[:name].rjust(name_width)} #{r[:verb].ljust(verb_width)} #{r[:path].to_s.ljust(path_width)} #{r[:reqs]}"
|
83
|
+
end
|
75
84
|
rescue LoadError
|
76
85
|
end
|
77
86
|
|
78
87
|
def assert_helpers_include(*helpers)
|
88
|
+
controller = ActionController::Base.new
|
89
|
+
view = ActionView::Base.new
|
79
90
|
helpers.each do |helper|
|
80
91
|
['url', 'path'].each do |suffix|
|
81
|
-
[
|
92
|
+
[controller, view].each { |obj| assert_respond_to obj, "#{helper}_#{suffix}".to_sym }
|
82
93
|
end
|
83
94
|
end
|
84
95
|
end
|
85
96
|
|
97
|
+
# Hack for compatibility between Rails 4 and Rails 3
|
86
98
|
def assert_unrecognized_route(route_path, options)
|
87
99
|
assert_raise ActionController::RoutingError do
|
100
|
+
begin
|
88
101
|
assert_routing route_path, options
|
102
|
+
rescue Minitest::Assertion => m
|
103
|
+
raise ActionController::RoutingError.new("")
|
104
|
+
end
|
89
105
|
end
|
90
106
|
end
|
91
107
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: route_translator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 3.0.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,26 +10,26 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2013-02
|
13
|
+
date: 2013-05-02 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
|
-
name:
|
16
|
+
name: minitest
|
17
17
|
requirement: !ruby/object:Gem::Requirement
|
18
18
|
none: false
|
19
19
|
requirements:
|
20
|
-
- -
|
20
|
+
- - ~>
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version:
|
22
|
+
version: 4.7.0
|
23
23
|
type: :development
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
26
26
|
none: false
|
27
27
|
requirements:
|
28
|
-
- -
|
28
|
+
- - ~>
|
29
29
|
- !ruby/object:Gem::Version
|
30
|
-
version:
|
30
|
+
version: 4.7.0
|
31
31
|
- !ruby/object:Gem::Dependency
|
32
|
-
name:
|
32
|
+
name: sinatra
|
33
33
|
requirement: !ruby/object:Gem::Requirement
|
34
34
|
none: false
|
35
35
|
requirements:
|
@@ -52,17 +52,26 @@ extensions: []
|
|
52
52
|
extra_rdoc_files: []
|
53
53
|
files:
|
54
54
|
- lib/route_translator.rb
|
55
|
-
- lib/route_translator/
|
56
|
-
- lib/route_translator/
|
57
|
-
- lib/route_translator/
|
58
|
-
- lib/route_translator/route_set
|
59
|
-
- lib/route_translator/
|
60
|
-
- lib/route_translator/routes_reloader.rb
|
61
|
-
- lib/route_translator/test_request.rb
|
55
|
+
- lib/route_translator/extensions.rb
|
56
|
+
- lib/route_translator/extensions/action_controller.rb
|
57
|
+
- lib/route_translator/extensions/mapper.rb
|
58
|
+
- lib/route_translator/extensions/route_set.rb
|
59
|
+
- lib/route_translator/translator.rb
|
62
60
|
- lib/route_translator/version.rb
|
63
|
-
-
|
61
|
+
- test/dummy/app/controllers/dummy_controller.rb
|
62
|
+
- test/dummy/config.ru
|
63
|
+
- test/dummy/config/application.rb
|
64
|
+
- test/dummy/config/boot.rb
|
65
|
+
- test/dummy/config/environment.rb
|
66
|
+
- test/dummy/config/initializers/secret_token.rb
|
67
|
+
- test/dummy/config/initializers/session_store.rb
|
68
|
+
- test/dummy/config/locales/all.yml
|
69
|
+
- test/dummy/config/routes.rb
|
70
|
+
- test/dummy/dummy_mounted_app.rb
|
71
|
+
- test/dummy/script/rails
|
72
|
+
- test/integration_test.rb
|
64
73
|
- test/locales/routes.yml
|
65
|
-
- test/
|
74
|
+
- test/routing_test.rb
|
66
75
|
- test/test_helper.rb
|
67
76
|
homepage: http://github.com/enriclluelles/route_translator
|
68
77
|
licenses: []
|
@@ -89,6 +98,18 @@ signing_key:
|
|
89
98
|
specification_version: 3
|
90
99
|
summary: Translate your Rails routes in a simple manner
|
91
100
|
test_files:
|
101
|
+
- test/dummy/app/controllers/dummy_controller.rb
|
102
|
+
- test/dummy/config.ru
|
103
|
+
- test/dummy/config/application.rb
|
104
|
+
- test/dummy/config/boot.rb
|
105
|
+
- test/dummy/config/environment.rb
|
106
|
+
- test/dummy/config/initializers/secret_token.rb
|
107
|
+
- test/dummy/config/initializers/session_store.rb
|
108
|
+
- test/dummy/config/locales/all.yml
|
109
|
+
- test/dummy/config/routes.rb
|
110
|
+
- test/dummy/dummy_mounted_app.rb
|
111
|
+
- test/dummy/script/rails
|
112
|
+
- test/integration_test.rb
|
92
113
|
- test/locales/routes.yml
|
93
|
-
- test/
|
114
|
+
- test/routing_test.rb
|
94
115
|
- test/test_helper.rb
|
@@ -1,23 +0,0 @@
|
|
1
|
-
module RouteTranslator
|
2
|
-
class Railtie < ::Rails::Railtie
|
3
|
-
config.route_translator = ActiveSupport::OrderedOptions.new
|
4
|
-
|
5
|
-
initializer "route_translator.set_configs" do |app|
|
6
|
-
options = app.config.route_translator
|
7
|
-
options.force_locale ||= false
|
8
|
-
options.generate_unlocalized_routes ||= false
|
9
|
-
options.translation_file ||= File.join(%w[config i18n-routes.yml])
|
10
|
-
|
11
|
-
ActiveSupport.on_load :route_translator do
|
12
|
-
options.each do |k, v|
|
13
|
-
k = "#{k}="
|
14
|
-
if config.respond_to?(k)
|
15
|
-
config.send k, v
|
16
|
-
else
|
17
|
-
raise "Invalid option key: #{k}"
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
@@ -1,50 +0,0 @@
|
|
1
|
-
%w(helpers translator dictionary_management).each do |f|
|
2
|
-
require File.expand_path(File.join(File.dirname(__FILE__), 'route_set', f))
|
3
|
-
end
|
4
|
-
|
5
|
-
|
6
|
-
module RouteTranslator
|
7
|
-
module RouteSet
|
8
|
-
include DictionaryManagement
|
9
|
-
include Translator
|
10
|
-
|
11
|
-
attr_accessor :dictionary, :localized_routes
|
12
|
-
|
13
|
-
#Use the i18n setting from the app
|
14
|
-
def translate_with_i18n(*wanted_locales)
|
15
|
-
init_i18n_dictionary(*wanted_locales)
|
16
|
-
translate
|
17
|
-
end
|
18
|
-
|
19
|
-
#Use yield the block passing and empty dictionary as a paramenter
|
20
|
-
def translate_with_dictionary(&block)
|
21
|
-
yield_dictionary &block
|
22
|
-
translate
|
23
|
-
end
|
24
|
-
|
25
|
-
#Use the translations from the specified file
|
26
|
-
def translate_from_file(file_path = nil)
|
27
|
-
file_path = absolute_path(file_path)
|
28
|
-
|
29
|
-
reset_dictionary
|
30
|
-
Dir[file_path].each do |file|
|
31
|
-
add_dictionary_from_file(file)
|
32
|
-
end
|
33
|
-
translate
|
34
|
-
end
|
35
|
-
|
36
|
-
private
|
37
|
-
|
38
|
-
def absolute_path (file_path)
|
39
|
-
file_path ||= RouteTranslator.config.translation_file
|
40
|
-
file_path = Rails.root.join(file_path) if defined?(Rails)
|
41
|
-
file_path
|
42
|
-
end
|
43
|
-
|
44
|
-
public
|
45
|
-
|
46
|
-
include Helpers
|
47
|
-
include Translator
|
48
|
-
include DictionaryManagement
|
49
|
-
end
|
50
|
-
end
|
@@ -1,76 +0,0 @@
|
|
1
|
-
module RouteTranslator
|
2
|
-
module RouteSet
|
3
|
-
module DictionaryManagement
|
4
|
-
# Resets dictionary and yields the block wich can be used to manually fill the dictionary
|
5
|
-
# with translations e.g.
|
6
|
-
# route_translator = RouteTranslator.new
|
7
|
-
# route_translator.yield_dictionary do |dict|
|
8
|
-
# dict['en'] = { 'people' => 'people' }
|
9
|
-
# dict['de'] = { 'people' => 'personen' }
|
10
|
-
# end
|
11
|
-
def yield_dictionary &block
|
12
|
-
reset_dictionary
|
13
|
-
yield @dictionary
|
14
|
-
set_available_locales_from_dictionary
|
15
|
-
end
|
16
|
-
|
17
|
-
# Resets dictionary and loads translations from specified file
|
18
|
-
# config/locales/routes.yml:
|
19
|
-
# en:
|
20
|
-
# people: people
|
21
|
-
# de:
|
22
|
-
# people: personen
|
23
|
-
# routes.rb:
|
24
|
-
# ... your routes ...
|
25
|
-
# ActionDispatch::Routing::Translator.translate_from_file
|
26
|
-
# or, to specify a custom file
|
27
|
-
# ActionDispatch::Routing::Translator.translate_from_file 'config', 'locales', 'routes.yml'
|
28
|
-
def load_dictionary_from_file file_path
|
29
|
-
reset_dictionary
|
30
|
-
add_dictionary_from_file file_path
|
31
|
-
end
|
32
|
-
|
33
|
-
# Add translations from another file to the dictionary.
|
34
|
-
def add_dictionary_from_file file_path
|
35
|
-
yaml = YAML.load_file(file_path)
|
36
|
-
yaml.each_pair do |locale, translations|
|
37
|
-
merge_translations locale, translations
|
38
|
-
end
|
39
|
-
set_available_locales_from_dictionary
|
40
|
-
end
|
41
|
-
|
42
|
-
# Merge translations for a specified locale into the dictionary
|
43
|
-
def merge_translations locale, translations
|
44
|
-
locale = locale.to_s
|
45
|
-
if translations.blank?
|
46
|
-
@dictionary[locale] ||= {}
|
47
|
-
return
|
48
|
-
end
|
49
|
-
@dictionary[locale] = (@dictionary[locale] || {}).merge(translations)
|
50
|
-
end
|
51
|
-
|
52
|
-
# Init dictionary to use I18n to translate route parts. Creates
|
53
|
-
# a hash with a block for each locale to lookup keys in I18n dynamically.
|
54
|
-
def init_i18n_dictionary *wanted_locales
|
55
|
-
wanted_locales = available_locales if wanted_locales.blank?
|
56
|
-
reset_dictionary
|
57
|
-
wanted_locales.each do |locale|
|
58
|
-
@dictionary[locale] = Hash.new do |hsh, key|
|
59
|
-
hsh[key] = I18n.translate key, :locale => locale #DISCUSS: caching or no caching (store key and translation in dictionary?)
|
60
|
-
end
|
61
|
-
end
|
62
|
-
@available_locales = @dictionary.keys.map &:to_s
|
63
|
-
end
|
64
|
-
|
65
|
-
private
|
66
|
-
def set_available_locales_from_dictionary
|
67
|
-
@available_locales = @dictionary.keys.map &:to_s
|
68
|
-
end
|
69
|
-
|
70
|
-
# Resets dictionary
|
71
|
-
def reset_dictionary
|
72
|
-
@dictionary = { default_locale => {}}
|
73
|
-
end
|
74
|
-
end
|
75
|
-
end
|
76
|
-
end
|
@@ -1,29 +0,0 @@
|
|
1
|
-
module RouteTranslator
|
2
|
-
module RouteSet
|
3
|
-
module Helpers
|
4
|
-
def available_locales
|
5
|
-
@available_locales ||= I18n.available_locales.map(&:to_s)
|
6
|
-
end
|
7
|
-
|
8
|
-
def available_locales= locales
|
9
|
-
@available_locales = locales.map(&:to_s)
|
10
|
-
end
|
11
|
-
|
12
|
-
def default_locale
|
13
|
-
@default_locale ||= I18n.default_locale.to_s
|
14
|
-
end
|
15
|
-
|
16
|
-
def default_locale= locale
|
17
|
-
@default_locale = locale.to_s
|
18
|
-
end
|
19
|
-
|
20
|
-
def default_locale? locale
|
21
|
-
default_locale == locale.to_s
|
22
|
-
end
|
23
|
-
|
24
|
-
def locale_suffix locale
|
25
|
-
RouteTranslator.locale_suffix locale
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
@@ -1,161 +0,0 @@
|
|
1
|
-
module RouteTranslator
|
2
|
-
module RouteSet
|
3
|
-
module Translator
|
4
|
-
# Translate a specific RouteSet, usually Rails.application.routes, but can
|
5
|
-
# be a RouteSet of a gem, plugin/engine etc.
|
6
|
-
def translate
|
7
|
-
Rails.logger.info "Translating routes (default locale: #{default_locale})" if defined?(Rails) && defined?(Rails.logger)
|
8
|
-
|
9
|
-
# save original routes and clear route set
|
10
|
-
original_routes = routes.dup
|
11
|
-
original_named_routes = named_routes.routes.dup # Hash {:name => :route}
|
12
|
-
|
13
|
-
# The filter method exists if the "routing-filter" gem is used
|
14
|
-
original_filters = set.respond_to?(:filters) ? set.filters.dup : []
|
15
|
-
|
16
|
-
routes_to_create = []
|
17
|
-
original_routes.each do |original_route|
|
18
|
-
if localized_routes && localized_routes.include?(original_route.to_s)
|
19
|
-
translations_for(original_route).each do |translated_route_args|
|
20
|
-
routes_to_create << translated_route_args
|
21
|
-
end
|
22
|
-
else
|
23
|
-
route = untranslated_route original_route
|
24
|
-
routes_to_create << route
|
25
|
-
end
|
26
|
-
# Always generate unlocalized routes?
|
27
|
-
if RouteTranslator.config.generate_unlocalized_routes
|
28
|
-
route = untranslated_route original_route
|
29
|
-
routes_to_create << route
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
reset!
|
34
|
-
|
35
|
-
routes_to_create.each do |r|
|
36
|
-
add_route(*r)
|
37
|
-
end
|
38
|
-
|
39
|
-
if original_filters.any?
|
40
|
-
set.filters << original_filters
|
41
|
-
set.filters.flatten!
|
42
|
-
end
|
43
|
-
|
44
|
-
Hash[original_named_routes.select{|k,v| localized_routes && localized_routes.include?(v.to_s)}].each_key do |route_name|
|
45
|
-
named_routes.helpers.concat add_untranslated_helpers_to_controllers_and_views(route_name)
|
46
|
-
end
|
47
|
-
|
48
|
-
finalize!
|
49
|
-
named_routes.install
|
50
|
-
end
|
51
|
-
|
52
|
-
# Add standard route helpers for default locale e.g.
|
53
|
-
# I18n.locale = :de
|
54
|
-
# people_path -> people_de_path
|
55
|
-
# I18n.locale = :fr
|
56
|
-
# people_path -> people_fr_path
|
57
|
-
def add_untranslated_helpers_to_controllers_and_views old_name
|
58
|
-
['path', 'url'].map do |suffix|
|
59
|
-
new_helper_name = "#{old_name}_#{suffix}"
|
60
|
-
|
61
|
-
ROUTE_HELPER_CONTAINER.each do |helper_container|
|
62
|
-
helper_container.send :define_method, new_helper_name do |*args|
|
63
|
-
if respond_to? "#{old_name}_#{locale_suffix(I18n.locale)}_#{suffix}"
|
64
|
-
send "#{old_name}_#{locale_suffix(I18n.locale)}_#{suffix}", *args
|
65
|
-
else
|
66
|
-
send "#{old_name}_#{locale_suffix(I18n.default_locale)}_#{suffix}", *args
|
67
|
-
end
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
new_helper_name.to_sym
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|
75
|
-
# Generate translations for a single route for all available locales
|
76
|
-
def translations_for route
|
77
|
-
available_locales.map do |locale|
|
78
|
-
translate_route(route, locale.dup) #we duplicate the locale string to ensure it's not frozen
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
# Generate translation for a single route for one locale
|
83
|
-
def translate_route route, locale
|
84
|
-
path_regex = route.path.respond_to?(:spec) ? route.path.spec : route.path
|
85
|
-
|
86
|
-
conditions = route.conditions.dup.merge({
|
87
|
-
:path_info => translate_path(path_regex.dup.to_s, locale)
|
88
|
-
})
|
89
|
-
|
90
|
-
conditions[:request_method] = request_method_array(conditions[:request_method]) if conditions[:request_method]
|
91
|
-
|
92
|
-
requirements = route.requirements.dup.merge!(LOCALE_PARAM_KEY => locale)
|
93
|
-
defaults = route.defaults.dup.merge LOCALE_PARAM_KEY => locale
|
94
|
-
|
95
|
-
new_name = "#{route.name}_#{locale_suffix(locale)}" if route.name
|
96
|
-
|
97
|
-
[route.app, conditions, requirements, defaults, new_name]
|
98
|
-
end
|
99
|
-
|
100
|
-
def untranslated_route route
|
101
|
-
path_regex = route.path.respond_to?(:spec) ? route.path.spec : route.path
|
102
|
-
|
103
|
-
conditions = route.conditions.dup.merge({
|
104
|
-
:path_info => path_regex.to_s
|
105
|
-
})
|
106
|
-
|
107
|
-
conditions[:request_method] = request_method_array(conditions[:request_method]) if conditions[:request_method]
|
108
|
-
|
109
|
-
[route.app, conditions, route.requirements.dup, route.defaults.dup, route.name]
|
110
|
-
end
|
111
|
-
|
112
|
-
def request_method_array(reg)
|
113
|
-
reg.source.gsub(%r{\^|\$}, "").split("|")
|
114
|
-
end
|
115
|
-
|
116
|
-
# Translates a path and adds the locale prefix.
|
117
|
-
def translate_path(path, locale)
|
118
|
-
final_optional_segments = path.slice!(/(\(.+\))$/)
|
119
|
-
new_path = path.split("/").map{|seg| translate_path_segment(seg, locale)}.join('/')
|
120
|
-
|
121
|
-
# Add locale prefix if it's not the default locale,
|
122
|
-
# or forcing locale to all routes,
|
123
|
-
# or already generating actual unlocalized routes
|
124
|
-
if !default_locale?(locale) ||
|
125
|
-
RouteTranslator.config.force_locale ||
|
126
|
-
RouteTranslator.config.generate_unlocalized_routes
|
127
|
-
new_path = "/#{locale.downcase}#{new_path}"
|
128
|
-
end
|
129
|
-
|
130
|
-
new_path = "/" if new_path.blank?
|
131
|
-
"#{new_path}#{final_optional_segments}"
|
132
|
-
end
|
133
|
-
|
134
|
-
# Tries to translate a single path segment. If the path segment
|
135
|
-
# contains sth. like a optional format "people(.:format)", only
|
136
|
-
# "people" will be translated, if there is no translation, the path
|
137
|
-
# segment is blank or begins with a ":" (param key), the segment
|
138
|
-
# is returned untouched
|
139
|
-
def translate_path_segment segment, locale
|
140
|
-
return segment if segment.blank? or segment.starts_with?(":")
|
141
|
-
|
142
|
-
match = TRANSLATABLE_SEGMENT.match(segment)[1] rescue nil
|
143
|
-
|
144
|
-
(translate_string(match, locale) || segment)
|
145
|
-
end
|
146
|
-
|
147
|
-
def translate_string(str, locale)
|
148
|
-
@dictionary[locale.to_s][str.to_s]
|
149
|
-
end
|
150
|
-
|
151
|
-
private
|
152
|
-
|
153
|
-
def reset!
|
154
|
-
clear!
|
155
|
-
named_routes.module.instance_methods.each do |method|
|
156
|
-
named_routes.module.send(:remove_method, method)
|
157
|
-
end
|
158
|
-
end
|
159
|
-
end
|
160
|
-
end
|
161
|
-
end
|