0

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 /.

Community
  • 1
  • 1
Barklem
  • 91
  • 1
  • 11

1 Answers1

0

I found that the problems was the rails build itself. I seem to have this problem with rails from time to time. It gets to a point where it stops working correctly. Unfortunately I was not far enough along to have a stable branch.

So I rebuilt the rails fresh.

Added these gems:

gem 'spree', '~> 3.1.0.rc1'
gem 'spree_auth_devise', '~> 3.1.0.rc1'
gem 'spree_gateway', '~> 3.1.0.rc1'
gem 'spree_i18n', git: 'git://github.com/spree/spree_i18n.git', branch: '3-1-stable'
gem 'spree_reviews', github: 'spree-contrib/spree_reviews', branch: '3-1-stable'
gem 'spree_globalize', github: 'spree-contrib/spree_globalize', branch: 'master'

After some experimenting the above is the stable gem revisions for that combination.

One failing I got was the spree globalise gem would not install. So I manually copied the include statements for the vendor js and css and then ran:

rake spree_globalize:install:migrations

Then migrated. With the default locales set as per the documentation. The application default correctly and /en and /ca altered the locale and link pathing as intended.

Lesson learned.

Barklem
  • 91
  • 1
  • 11