【问题标题】:How to configure: Rails - Spree - i18n - /locale in url or default to / for default locale如何配置:Rails - Spree - i18n - /locale in url or default to / for default locale
【发布时间】:2016-05-08 15:43:30
【问题描述】:

我正在尝试配置 heroku/rails/spree 站点以在 url 中使用语言环境。例如:www.sample.com/en/productswww.sample.com/ca/products 和默认语言环境:www.sample.com/

spree i18n gem 运行良好,允许在 spree 后端进行翻译。 rails 的 i18n gem 允许通过菜单选择器正确地进行本地更改。

该网站支持三种语言。默认为西班牙语,可选择英语和加泰罗尼亚语。

初始化程序/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

效果很好。但是我想将管理后端语言修复为英语(en)。

指向路由配置的i18n指令:

config/routes.rb

scope "(:locale)", locale: /en|nl/ do
  resources :books
end

我尝试了各种组合来获得: 配置/路由.rb 安装 Spree::Core::Engine,在:'/' 与/en/ca 合作但没有成功。

我曾尝试使用 routing-filter gem 将 spree 应用程序包装在一个语言环境中,但收效甚微。

我认为多语言网站的 url 中的语言环境是首选方法。相信会有涵盖该主题的支持或教程。但是我的研究没有找到任何解决方案。

我可以让 /ca/ /es/ 和 /en/ 工作。但我仍然需要让 es 在 / 而不是 /es/ 上工作。

这是当前配置:

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

路线为:

Rails.application.routes.draw do

  scope "(:locale)", :locale => /en|es|ca/ do
   mount Spree::Core::Engine, at: '/'
  end

end

我认为它需要:

在控制器中,查看本地或 url 以正确设置语言环境。如果 url 中没有语言环境或语言环境,则设置为默认值。

然后,一旦始终设置语言环境,将 spree 引擎安装在 / 是 es 上,如果是 ca,则安装在 /ca 上,或者 /en 是 en 语言环境。

我尝试了路由过滤器 gem。在路由中使用文件管理器语言环境。它所做的只是将应用程序始终默认为 ca locale。即使在 /es 和 /en 上。该页面还将整页呈现到模板有效负载中,这给了我两个页眉。那里出了点问题。

我尝试了这种方法:i18n Routing To Mounted Engine - Ignoring locale,但没有适合我的配置。

看起来 spree-globalization gem 没有正确安装。现在我的所有 /locale/ 路径都没有在路由或应用程序控制器中配置。现在唯一要做的就是将默认语言环境推送到 root /。

【问题讨论】:

    标签: ruby-on-rails heroku spree rails-i18n


    【解决方案1】:

    我发现问题出在 Rails 构建本身。我似乎不时遇到这个问题。它达到了停止正常工作的程度。不幸的是,我还不够远,没有一个稳定的分支。

    所以我重新构建了导轨。

    添加了这些宝石:

    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'
    

    经过一些试验,以上是该组合的稳定宝石修订版。

    我遇到的一个失败是无法安装 spree globalize gem。所以我手动复制了供应商 js 和 css 的包含语句,然后运行:

    rake spree_globalize:install:migrations
    

    然后迁移。根据文档设置默认语言环境。应用程序默认正确,并且 /en 和 /ca 按预期更改了语言环境和链接路径。

    经验教训。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-27
      • 2022-12-28
      相关资源
      最近更新 更多