0

I want to make my Rails3.2 app i18n according to the following site and to include a locale parameter in the URL path. (http://www.wordchuck.com/en/website/blogs/5)

I implemented as

scope ':locale' do
  resources :nodes
end

and other methods

def set_locale
  I18n.locale = params[:locale] if params.include?('locale')
end


def default_url_options(options = {})
  options.merge!({ :locale => I18n.locale })
end

in my application_controller.rb.

And now I can confirm that

http://myhost/ja/nodes/explore or http://myhost/en/nodes/explore

pass, but

http://myhost/nodes/explore

got "No route matches [GET] "/nodes/explore"" error.

I wonder that could be :locale is nil.
To make nil :locale enable and defaults to "en" when :locale is nil, what should I do?

linzhixing
  • 146
  • 2
  • 12

1 Answers1

0

With the way you're routes are set up, the nodes resources require that there be a locale present.

To get around this, you can simply create more routes outside of the :locale scope :

scope ':locale' do
  resource :nodes
end

resource :nodes

In principle, the default locale will be used when the current locale isn't explicitly set. If you haven't changed anything in your application, this will be :en by default.

Otherwise, you can set this explicitly in your set_locale method :

def set_locale
  I18n.locale = params.include?('locale') ? params[:locale] : :en
end
tigrish
  • 2,488
  • 18
  • 21
  • Oh, to duplicate "resource nodes" with/without scope is as you say a possible way, but in fact I have much more "resources" and "match" routing statements, so isn't there any other scalable method? I suppose that setting default locale trick works only after the routing passes... – linzhixing Jun 05 '13 at 13:54
  • I don't know that you can define routes to have an optional scope. – tigrish Jun 06 '13 at 13:39