23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
# File 'lib/generators/localizer_rails/localizer_rails_generator.rb', line 23
def manifest
if options[:views] || !@has_options
copy_file File.join( *%w( app views localizer_rails _item.html.erb ) ), ( Rails.root.join( *%w( app views localizer_rails _item.html.erb ) ) )
copy_file File.join( *%w( app views localizer_rails _item.bootstrap.html.erb ) ), ( Rails.root.join( *%w( app views localizer_rails _item.bootstrap.html.erb ) ) )
copy_file File.join( *%w( app views localizer_rails _elements.html.erb ) ), ( Rails.root.join( *%w( app views localizer_rails _elements.html.erb ) ) )
end
if options[:config] || !@has_options
copy_file File.join(*%w( lib generators localizer_rails templates localizer_rails_prefs.rb )), (Rails.root.join(*%w( config initializers localizer_rails localizer_rails_prefs.rb )))
end
if options[:map] || @all
copy_file File.join(*%w( config localizer_rails lang_countries.yml )), (Rails.root.join(*%w( config localizer_rails lang_countries.yml )))
end
if options[:kam] || !@has_options
copy_file File.join(*%w( lib generators localizer_rails templates kaminari_action_view_extension.rb )), (Rails.root.join(*%w( config initializers kaminari_action_view_extension.rb )))
end
if options[:nav] || !@has_options
copy_file File.join(*%w( lib generators localizer_rails templates _header.html.erb )), (Rails.root.join(*%w( app views layouts _header.html.erb )))
end
if options[:routes] || !@has_options
append_to_file Rails.root.join(*%w( config routes.rb )) do |a|
%q{
__END__
## --- LocalizerRails routes and match-alls
## (1) move inside your 'locale' scope to create the routes to your custom error pages:
## NOTES:
# forget about the system error 500, your app might be too broken to handle it
# Rails.application.routes.draw do
scope ":locale", :locale => /#{LocalizerRails.active_locales.join("|")}/ do
# ...
%w( 404 ).each do |code|
get code, :to => "errors#show", :code => code, :as => "error_#{code}"
end
## (2) move after your 'locale' scope if you need match-all routes prepending missing locale
## NOTES:
# - for TESTING POURPOSES in development, REMEMBER to CHANGE this value in config/environments/development
# config.consider_all_requests_local = true
# to:
# config.consider_all_requests_local = false
# => full error reports are disabled, and Rails treats all requests as in production
match '*path', :to => redirect { |p, req|
LocalizerRails.set_locale(req)
case
when LocalizerRails.active_locales.none? { |word| req.path.starts_with?("/#{word}/") }
## TRY prepending the locale
"/#{I18n.locale}#{req.path}"
when Rails.env.production? || !Rails.application.config.consider_all_requests_local
## SHOW custom 404
Rails.application.routes.url_helpers.error_404_path(:locale => I18n.locale)
else
## let Rails handle this with default 404
raise ActionController::RoutingError.new('Not Found')
end
},
:via => [ :get, :post, :patch, :delete ],
:status => '301'
match '', :to => redirect { |p, req| LocalizerRails.set_locale(req) },
:via => [:get, :post, :patch, :delete],
:status => '301'
}
end
end
end
|