Table of Content
Switching locale
How about switching the locale?
Getting locale option from params.
In the application_controller.rb
file:
Or
And now, we can access different with the params query: http://0.0.0.0:3000/?locale=en
.
Automatically appending the locale params to URL.
Rails comes with a default_url_options
that is the default query string and values that will be attached in the URL.
We can override the default_url_options
. Add the following method in the application_controller.rb
file.
This only applies to URL which is generated by rails, such as root_path. Manually typing the URL will not get the default_url_options
attached.
And than the UI to switch. Since we want the language available everywhere, put it in the applicotian.html.erb
file.
1<a href='?locale=zh-TW'>中</a> | <a href='?locale=en'>EN</a>
For more detail, check http://guides.rubyonrails.org/i18n.html. For example, adding locale as part of the URL
What’s next? We’re going to take a look at “Pow local server”.