I am trying to configure a heroku/rails/spree
site to use locale in the url. For example: www.sample.com/en/products
and www.sample.com/ca/products
and default locale at: www.sample.com/
The spree i18n
gem is working fine, allowing translations in spree backend. The i18n gem for rails is allowing for local change correctly via menu selector.
The site is in three languages. The default is Spanish, with options for English and Catalan.
initializers/locale.rb
# tell the I18n library where to find your translations
I18n.load_path += Dir[Rails.root.join('lib', 'locale', '*.{rb,yml}')]
# set default locale to something other than :en
I18n.default_locale = :es
Which works perfectly. However I would like to fix the admin backend language to english(en).
The i18n instructions for point to the routes configuration:
config/routes.rb
scope "(:locale)", locale: /en|nl/ do
resources :books
end
I have tried all kinds of combinations to get:
config/routes.rb
mount Spree::Core::Engine, at: '/'
To work with /en
and /ca
but without success.
I have tried to use the routing-filter gem to wrap the spree application in a locale, but with little success.
I thought that locales in url for multilingual sites was the preferred method. Believing that there would be support or a tutorial covering the subject. But my research has not found any solutions.
I can get /ca/ /es/ and /en/ to work. But I still need to get es to work at / instead of /es/.
Here is the current configuration:
application_controller:
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
before_filter :set_locale
def url_options
{ locale: I18n.locale }
end
def set_locale
I18n.locale = params[:locale] || I18n.default_locale
Rails.application.routes.default_url_options[:locale]= I18n.locale
end
end
With routes as:
Rails.application.routes.draw do
scope "(:locale)", :locale => /en|es|ca/ do
mount Spree::Core::Engine, at: '/'
end
end
I was thinking it would need:
In the controller, look to the local or url to set locale correctly. If no locale or locale in url, set to default.
Then once the locale is set always, mount the spree engine on / is es, on /ca if ca, or on /en is en locale.
I tried the routing-filter gem. With filer locale in the routes. All it did was default the application to ca locale always. Even on /es and /en. Also the page rendered a fullpage into the template payload, which gave me two page headers. Something very wrong there.
I tried this approach: i18n Routing To Mounted Engine - Ignoring locale but no configuration worked for me.
Looks like the spree-globalization gem was not installed correctly. Now I have all the /locale/ paths working with not configuration in routes or the application controller. The only thing to do now is push the default locale to root /.