route_translator 3.2.4 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5d1c42a43f7afa3aa88301f402d64e0fbe60ebdf
4
- data.tar.gz: c873187b33ca3327f482276c780b30e6999e246b
3
+ metadata.gz: 077e37f57ea89aa141aa2e271ba08e832cd85ef5
4
+ data.tar.gz: d6d9c89e7c57dcf22975bdc9e2194107ddf0b290
5
5
  SHA512:
6
- metadata.gz: 93d3bb5f6ff06529a3d986a90574925d353e4882c472ca3254c405cd4c3736ac9737ae7fb933066b1e7cecd720d6eca211908d311ee2d6e471b774a39a2296e5
7
- data.tar.gz: aa225c315b5a2afe93ec29e3a80253d840a5191f9031575933ca67231985522b162d0709601a76c68c31959c7e411ea53571bae65b44acbe53f6eb6dc760cb2f
6
+ metadata.gz: 554ab0400cbdffbfc854c3de668d892513bf1195598d6611df9057bfaa26f764682fc25a370531366ba8e4b1ac06620ee49138cd2584e695516a228303297168
7
+ data.tar.gz: d5e661b4a03807afe05d7ba8c066fde6979b656b949464cccf667f75855ec083aab15301fdb92c5ee5d3b03f36bf1d4f2bf73a59beeceba18bf13f722f93129f
@@ -1,7 +1,4 @@
1
1
  require 'active_support'
2
- require 'action_controller'
3
- require 'action_mailer'
4
- require 'action_dispatch'
5
2
 
6
3
  require File.expand_path('../route_translator/extensions', __FILE__)
7
4
  require File.expand_path('../route_translator/translator', __FILE__)
@@ -24,7 +21,7 @@ module RouteTranslator
24
21
  @config.generate_unlocalized_routes ||= false
25
22
  @config.locale_param_key ||= :locale
26
23
  @config.generate_unnamed_unlocalized_routes ||= false
27
- @config.host_locales ||= {}.with_indifferent_access
24
+ @config.host_locales ||= ActiveSupport::OrderedHash.new
28
25
  yield @config if block
29
26
  resolve_config_conflicts
30
27
  @config
@@ -36,7 +33,10 @@ module RouteTranslator
36
33
  @config.generate_unnamed_unlocalized_routes = false
37
34
  @config.force_locale = false
38
35
  @config.hide_locale = false
39
- @config.host_locales = @config.host_locales.with_indifferent_access
40
36
  end
41
37
  end
38
+
39
+ def self.locale_param_key
40
+ self.config.locale_param_key
41
+ end
42
42
  end
@@ -4,30 +4,24 @@ module ActionController
4
4
  class Base
5
5
  around_filter :set_locale_from_url
6
6
 
7
- def set_locale_from_url(&block)
8
- with_host_locale { I18n.with_locale(params[RouteTranslator.locale_param_key], &block) }
9
- end
10
-
11
- private
12
-
13
- def with_host_locale(&block)
14
- host_locale = RouteTranslator::Host.locale_from_host(request.host)
15
- begin
16
- if host_locale
17
- original_default = I18n.default_locale
18
- original_locale = I18n.locale
7
+ def set_locale_from_url
8
+ tmp_default_locale = RouteTranslator::Host.locale_from_host(request.host)
9
+ if tmp_default_locale
10
+ current_default_locale = I18n.default_locale
11
+ I18n.default_locale = tmp_default_locale
12
+ end
19
13
 
20
- I18n.default_locale = host_locale
21
- I18n.locale = host_locale
22
- end
14
+ tmp_locale = params[RouteTranslator.locale_param_key] || tmp_default_locale
15
+ if tmp_locale
16
+ current_locale = I18n.locale
17
+ I18n.locale = tmp_locale
18
+ end
23
19
 
24
- yield
20
+ yield
25
21
 
26
- ensure
27
- I18n.default_locale = original_default if host_locale
28
- I18n.locale = original_locale if host_locale
29
- end
22
+ ensure
23
+ I18n.default_locale = current_default_locale if tmp_default_locale
24
+ I18n.locale = current_locale if tmp_locale
30
25
  end
31
-
32
26
  end
33
27
  end
@@ -25,7 +25,12 @@ module ActionDispatch
25
25
  options[:as] = name_for_action(options[:as], action)
26
26
  end
27
27
 
28
- mapping = Mapping.new(@set, @scope, path, options)
28
+ begin
29
+ mapping = Mapping.new(@set, @scope, path, options)
30
+ rescue ArgumentError => e
31
+ mapping = Mapping.build(@scope, @set, URI.parser.escape(path), options.delete(:as), options)
32
+ end
33
+
29
34
  if @localized
30
35
  @set.add_localized_route(*mapping.to_route)
31
36
  else
@@ -1,12 +1,12 @@
1
1
  module RouteTranslator
2
2
  module Host
3
3
  def self.locale_from_host(host)
4
- matches = RouteTranslator.config.host_locales.detect {|key, locale| host =~ regex_for(key) }
5
- matched_locale = if matches && host_match = matches.first
6
- locale = RouteTranslator.config.host_locales[host_match].to_sym
7
- locale if I18n.available_locales.include?(locale)
8
- end
9
- matched_locale || I18n.default_locale
4
+ locales = RouteTranslator.config.host_locales.reduce([]) do |locales, (pattern, locale)|
5
+ locales << locale.to_sym if host =~ regex_for(pattern)
6
+ locales
7
+ end
8
+ locales &= I18n.available_locales
9
+ (locales.first || I18n.default_locale).to_sym
10
10
  end
11
11
 
12
12
  def self.regex_for(host_string)
@@ -21,9 +21,5 @@ module RouteTranslator
21
21
  def native_locales
22
22
  config.host_locales.values.map {|locale| :"native_#{locale}" }
23
23
  end
24
-
25
- def locale_param_key
26
- self.config.locale_param_key
27
- end
28
24
  end
29
25
  end
@@ -5,38 +5,40 @@ module RouteTranslator
5
5
  # people_path -> people_de_path
6
6
  # I18n.locale = :fr
7
7
  # people_path -> people_fr_path
8
- def self.add_untranslated_helpers_to_controllers_and_views(old_name, helper_container, helper_list)
9
- ['path', 'url'].each do |suffix|
8
+ def self.add_untranslated_helpers_to_controllers_and_views(old_name, named_route_collection)
9
+ if (named_route_collection.respond_to?(:url_helpers_module))
10
+ url_helpers_module = named_route_collection.url_helpers_module
11
+ path_helpers_module = named_route_collection.path_helpers_module
12
+ url_helpers_list = named_route_collection.helper_names
13
+ path_helpers_list = named_route_collection.helper_names
14
+ else
15
+ url_helpers_module = named_route_collection.module
16
+ path_helpers_module = named_route_collection.module
17
+ url_helpers_list = named_route_collection.helpers
18
+ path_helpers_list = named_route_collection.helpers
19
+ end
20
+
21
+ [
22
+ ['path', path_helpers_module, path_helpers_list],
23
+ ['url', url_helpers_module, url_helpers_list]
24
+ ].each do |suffix, helper_container, helper_list|
10
25
  new_helper_name = "#{old_name}_#{suffix}"
11
26
 
12
27
  helper_list.push(new_helper_name.to_sym) unless helper_list.include?(new_helper_name.to_sym)
13
28
 
14
29
  helper_container.__send__(:define_method, new_helper_name) do |*args|
15
- locale_suffix = I18n.locale.to_s.underscore
16
- default_locale_suffix = I18n.default_locale.to_s.underscore
17
- args_hash = args.select {|arg| arg.is_a?(Hash) }.first
18
- args_locale_suffix = args_hash[:locale].to_s.underscore if args_hash.present?
19
- if RouteTranslator.config.host_locales.present?
20
- if args.blank? || args_locale_suffix == default_locale_suffix
21
- __send__("#{old_name}_native_#{default_locale_suffix}_#{suffix}", *args)
22
- elsif args_locale_suffix
23
- __send__("#{old_name}_#{args_locale_suffix}_#{suffix}", *args)
24
- end
25
- elsif respond_to?("#{old_name}_#{locale_suffix}_#{suffix}")
26
- __send__("#{old_name}_#{locale_suffix}_#{suffix}", *args)
27
- else
28
- __send__("#{old_name}_#{I18n.default_locale.to_s.underscore}_#{suffix}", *args)
29
- end
30
+ __send__(Translator.route_name_for(args, old_name, suffix, self), *args)
30
31
  end
31
32
  end
32
33
  end
33
34
 
34
35
  def self.translations_for(app, conditions, requirements, defaults, route_name, anchor, route_set, &block)
35
- add_untranslated_helpers_to_controllers_and_views(route_name, route_set.named_routes.module, route_set.named_routes.helpers)
36
+ add_untranslated_helpers_to_controllers_and_views(route_name, route_set.named_routes)
36
37
 
37
38
  available_locales.each do |locale|
38
39
  new_conditions = conditions.dup
39
40
  new_conditions[:path_info] = translate_path(conditions[:path_info], locale)
41
+ new_conditions[:parsed_path_info] = ActionDispatch::Journey::Parser.new.parse(new_conditions[:path_info]) if conditions[:parsed_path_info]
40
42
  if new_conditions[:required_defaults] && !new_conditions[:required_defaults].include?(RouteTranslator.locale_param_key)
41
43
  new_conditions[:required_defaults] << RouteTranslator.locale_param_key if new_conditions[:required_defaults]
42
44
  end
@@ -56,7 +58,7 @@ module RouteTranslator
56
58
  private
57
59
  def self.available_locales
58
60
  available_locales = I18n.available_locales.dup
59
- available_locales.push *RouteTranslator.native_locales if RouteTranslator.native_locales.present?
61
+ available_locales.push(*RouteTranslator.native_locales) if RouteTranslator.native_locales.present?
60
62
  # Make sure the default locale is translated in last place to avoid
61
63
  # problems with wildcards when default locale is omitted in paths. The
62
64
  # default routes will catch all paths like wildcard if it is translated first.
@@ -120,5 +122,25 @@ module RouteTranslator
120
122
  def self.locale_param_present?(path)
121
123
  !(path.split('/').detect { |segment| segment.to_s == ":#{RouteTranslator.locale_param_key.to_s}" }.nil?)
122
124
  end
125
+
126
+ def self.host_locales_option?
127
+ RouteTranslator.config.host_locales.present?
128
+ end
129
+
130
+ def self.route_name_for(args, old_name, suffix, kaller)
131
+ args_hash = args.detect{|arg| arg.is_a?(Hash)}
132
+ args_locale = host_locales_option? && args_hash && args_hash[:locale]
133
+ current_locale_name = I18n.locale.to_s.underscore
134
+
135
+ locale = if args_locale
136
+ args_locale.to_s.underscore
137
+ elsif kaller.respond_to?("#{old_name}_#{current_locale_name}_#{suffix}")
138
+ current_locale_name
139
+ else
140
+ I18n.default_locale.to_s.underscore
141
+ end
142
+
143
+ "#{old_name}_#{locale}_#{suffix}"
144
+ end
123
145
  end
124
146
  end
@@ -1,3 +1,3 @@
1
1
  module RouteTranslator
2
- VERSION = '3.2.4'
2
+ VERSION = '4.0.0'
3
3
  end
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env rake
2
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
3
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
4
+
5
+ require File.expand_path('../config/application', __FILE__)
6
+
7
+ Dummy::Application.load_tasks
@@ -1,5 +1,11 @@
1
1
  class DummyController < ActionController::Base
2
+
2
3
  def dummy
3
4
  render :text => I18n.locale
4
5
  end
6
+
7
+ def show
8
+ # Pass
9
+ end
10
+
5
11
  end
@@ -0,0 +1 @@
1
+ <%= link_to "Show [#{I18n.locale}]", show_path %>
@@ -1,3 +1,10 @@
1
+
2
+ require 'i18n'
3
+ begin
4
+ I18n.enforce_available_locales = true
5
+ rescue NoMethodError
6
+ end
7
+
1
8
  require "action_controller/railtie"
2
9
  begin
3
10
  require "active_resource/railtie"
@@ -4,9 +4,12 @@
4
4
  en:
5
5
  routes:
6
6
  dummy: dummy
7
+ show: show
7
8
  es:
8
9
  routes:
9
10
  dummy: dummy
11
+ show: mostrar
10
12
  ru:
11
13
  routes:
12
14
  dummy: манекен
15
+ show: показывать
@@ -1,6 +1,9 @@
1
+ require File.join(Dummy::Application.root, 'dummy_mounted_app.rb')
2
+
1
3
  Dummy::Application.routes.draw do
2
4
  localized do
3
- get 'dummy', :to => 'dummy#dummy'
5
+ get 'dummy', :to => 'dummy#dummy'
6
+ get 'show', :to => 'dummy#show'
4
7
  end
5
8
 
6
9
  root :to => 'dummy#dummy'
data/test/host_test.rb CHANGED
@@ -1,24 +1,28 @@
1
1
  #encoding: utf-8
2
- require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper'))
2
+ require File.expand_path('../test_helper', __FILE__)
3
3
 
4
4
  class TestHostsFromLocale < MiniTest::Unit::TestCase
5
- include RouteTranslator::TestHelper
5
+
6
+ include RouteTranslator::ConfigurationHelper
7
+ include RouteTranslator::I18nHelper
8
+ include RouteTranslator::RoutesHelper
9
+
6
10
  def setup
7
- I18n.backend = I18n::Backend::Simple.new
8
- I18n.load_path = [ File.expand_path('../locales/routes.yml', __FILE__) ]
9
- I18n.reload!
10
-
11
- config_host_locales({
12
- '*.something.es' => :es,
13
- '*.ru.subdomain.domain.*' => :ru,
14
- 'russia.something.net' => :ru,
15
- '*.com' => :en
16
- })
11
+ setup_config
12
+ setup_i18n
13
+
14
+ config = host_locales_config_hash
15
+ config['*.something.es'] = :es
16
+ config['*.ru.subdomain.domain.*'] = :ru
17
+ config['russia.something.net'] = :ru
18
+ config['*.com'] = :en
19
+
20
+ config_host_locales(config)
17
21
  end
18
22
 
19
23
  def teardown
20
- config_host_locales({})
21
- I18n.default_locale = :en
24
+ teardown_i18n
25
+ teardown_config
22
26
  end
23
27
 
24
28
  def test_wildcard_at_beginning_matches
@@ -54,10 +58,16 @@ class TestHostsFromLocale < MiniTest::Unit::TestCase
54
58
  end
55
59
 
56
60
  def test_precedence_if_more_than_one_match
57
- config_host_locales({ 'russia.*' => :ru, '*.com' => :en })
61
+ config = host_locales_config_hash
62
+ config['russia.*'] = :ru
63
+ config['*.com'] = :en
64
+ config_host_locales(config)
58
65
  assert_equal :ru, RouteTranslator::Host.locale_from_host('russia.com')
59
66
 
60
- config_host_locales({ '*.com' => :en, 'russia.*' => :ru })
67
+ config = host_locales_config_hash
68
+ config['*.com'] = :en
69
+ config['russia.*'] = :ru
70
+ config_host_locales(config)
61
71
  assert_equal :en, RouteTranslator::Host.locale_from_host('russia.com')
62
72
  end
63
73
 
@@ -66,14 +76,13 @@ class TestHostsFromLocale < MiniTest::Unit::TestCase
66
76
  end
67
77
 
68
78
  def test_readme_examples_work
69
- config_host_locales(
70
- {
71
- '*.es' => :es, # matches ['domain.es', 'subdomain.domain.es', 'www.long.string.of.subdomains.es'] etc.
72
- 'ru.wikipedia.*' => :ru, # matches ['ru.wikipedia.org', 'ru.wikipedia.net', 'ru.wikipedia.com'] etc.
73
- '*.subdomain.domain.*' => :ru, # matches ['subdomain.domain.org', 'www.subdomain.domain.net'] etc.
74
- 'news.bbc.co.uk' => :en, # matches ['news.bbc.co.uk'] only
75
- }
76
- )
79
+ config = host_locales_config_hash
80
+ config['*.es'] = :es # matches ['domain.es', 'subdomain.domain.es', 'www.long.string.of.subdomains.es'] etc.
81
+ config['ru.wikipedia.*'] = :ru # matches ['ru.wikipedia.org', 'ru.wikipedia.net', 'ru.wikipedia.com'] etc.
82
+ config['*.subdomain.domain.*'] = :ru # matches ['subdomain.domain.org', 'www.subdomain.domain.net'] etc.
83
+ config['news.bbc.co.uk'] = :en # matches ['news.bbc.co.uk'] only
84
+
85
+ config_host_locales(config)
77
86
 
78
87
  examples_1 = ['domain.es', 'subdomain.domain.es', 'www.long.string.of.subdomains.es']
79
88
  examples_2 = ['ru.wikipedia.org', 'ru.wikipedia.net', 'ru.wikipedia.com']
@@ -0,0 +1,37 @@
1
+ #encoding: utf-8
2
+ require File.expand_path('../../test_helper', __FILE__)
3
+
4
+ class GeneratedPathTest < integration_test_suite_parent_class
5
+
6
+ include RouteTranslator::ConfigurationHelper
7
+
8
+ def test_path_generated
9
+ get '/show'
10
+ assert_response :success
11
+ assert_tag :tag => "a", :attributes => { :href => "/show" }
12
+ end
13
+
14
+ def test_path_translated
15
+ get '/es/mostrar'
16
+ assert_response :success
17
+ assert_tag :tag => "a", :attributes => { :href => "/es/mostrar" }
18
+ end
19
+
20
+ def test_path_translated_after_force
21
+ config_force_locale true
22
+
23
+ get '/es/mostrar'
24
+ assert_response :success
25
+ assert_tag :tag => "a", :attributes => { :href => "/es/mostrar" }
26
+ end
27
+
28
+ def test_path_translated_while_generate_unlocalized_routes
29
+ config_default_locale_settings 'en'
30
+ config_generate_unlocalized_routes true
31
+
32
+ get '/es/mostrar'
33
+ assert_response :success
34
+ assert_tag :tag => "a", :attributes => { :href => "/es/mostrar" }
35
+ end
36
+
37
+ end
@@ -1,42 +1,36 @@
1
1
  #encoding: utf-8
2
- require File.expand_path('../test_helper', __FILE__)
2
+ require File.expand_path('../../test_helper', __FILE__)
3
3
 
4
- require File.expand_path('../dummy/dummy_mounted_app', __FILE__)
5
- require File.expand_path('../dummy/config/environment', __FILE__)
4
+ class HostLocalesTest < integration_test_suite_parent_class
6
5
 
7
- class_to_inherit = defined?(ActionDispatch::IntegrationTest) ? ActionDispatch::IntegrationTest : ActionController::IntegrationTest
8
- class IntegrationTest < class_to_inherit
9
- include RouteTranslator::TestHelper
6
+ include RouteTranslator::ConfigurationHelper
10
7
 
11
- def test_set_locale_from_params
12
- config_default_locale_settings 'en'
13
-
14
- get '/es/dummy'
15
- assert_equal 'es', @response.body
16
- assert_response :success
8
+ def setup
9
+ config_host_locales('*.es' => 'es', 'ru.*.com' => 'ru')
10
+ Dummy::Application.reload_routes!
17
11
  end
18
12
 
19
- def test_mounted_apps_work_with_correct_path
20
- get 'dummy_mounted_app'
21
- assert_equal "Good", @response.body
22
- assert_response :success
13
+ def teardown
14
+ config_host_locales({})
15
+ Dummy::Application.reload_routes!
23
16
  end
24
17
 
25
- def test_i18n_locale_thread_safe
26
- config_default_locale_settings 'en'
27
- get '/es/dummy'
28
- assert_equal 'es', @response.body
29
-
30
- assert_equal :en, I18n.locale
31
- end
32
18
 
33
- def test_host_locales
19
+ def test_root_path
34
20
  ## root of es com
35
21
  host! 'www.testapp.es'
36
22
  get '/'
37
23
  assert_equal 'es', @response.body
38
24
  assert_response :success
39
25
 
26
+ ## root of ru com
27
+ host! 'ru.testapp.com'
28
+ get '/'
29
+ assert_equal 'ru', @response.body
30
+ assert_response :success
31
+ end
32
+
33
+ def test_explicit_path
40
34
  ## native es route on es com
41
35
  host! 'www.testapp.es'
42
36
  get '/dummy'
@@ -49,12 +43,6 @@ class IntegrationTest < class_to_inherit
49
43
  assert_equal 'ru', @response.body
50
44
  assert_response :success
51
45
 
52
- ## root of ru com
53
- host! 'ru.testapp.com'
54
- get '/'
55
- assert_equal 'ru', @response.body
56
- assert_response :success
57
-
58
46
  ## native ru route on ru com
59
47
  host! 'ru.testapp.com'
60
48
  get URI.escape('/манекен')
@@ -67,4 +55,5 @@ class IntegrationTest < class_to_inherit
67
55
  assert_equal 'es', @response.body
68
56
  assert_response :success
69
57
  end
58
+
70
59
  end