上下文:对于用于自行车租赁的 Ruby on Rails 应用程序,我使用 gem globalize 来处理:description不同语言的输入。
当前状态: globalize 实现有效,这取决于我能够以description特定语言存储的语言环境。输入 for:description是根据整个网页的区域设置处理的。
这意味着此页面上的所有内容都必须更改语言才能以:description正确的语言存储。
或者,我也可以显示所有可用的语言环境并description为每个语言环境显示。(另请参阅下面注释掉的代码)。
问:我正在寻找一种方法,让用户只选择一种语言:description,然后:description以正确的语言保存,而无需更改整个网页的语言。
代码
形式
<div class="row">
<%# I18n.available_locales.each do |locale| %>
<!-- <h1><%#= locale %></h1> -->
<%= f.globalize_fields_for locale do |ff| %>
<div class="col-10">
<div class="form-group">
<label class="form-control-label text required" for="accommodation_category_description">Description</label>
<div><%= ff.text_area :description, :rows =>"5", :cols =>"30", class:"form-control is-valid text required" %></div>
</div>
</div>
<% end %>
<%# end %> …Run Code Online (Sandbox Code Playgroud) 过去几个小时我一直在摸不着头脑,寻找答案,但我找不到任何地方.
我的宝石文件:
# Use globalize for translating models
gem "globalize", github: "ncri/globalize" # for Rails 4.2
gem 'globalize-accessors', '~> 0.1.5'
# Use friendly_id for slugs
gem 'friendly_id', '~> 5.1.0'
gem 'friendly_id-globalize', '~> 1.0.0.alpha1'
Run Code Online (Sandbox Code Playgroud)
情况如下:
我有两种语言"en"和"fr"
2个模型:pages和pages_translations页面有一个slug列,pages_translations也有一个slug列.
如果我查看页面 - > en/pages/slug-en,它可以工作.
如果我查看页面 - > fr/pages-slug-fr,它可以工作.
所以我假设friendly_id和globalize已正确配置.
但是我的问题是我不能使用以下语言切换器:
<% if I18n.locale != :en %>
<li>
<%= link_to t('menu.languages.short_en'), url_for(locale: 'en') %>
</li>
<% end %>
Run Code Online (Sandbox Code Playgroud)
路线变为en/pages/slug-fr(即语言改变但不是slug).
我在初始化程序中激活了config.use:finders.
我的页面模型:
translates :title, :slug, :blurb, :content, :seo_title, :seo_description, :seo_keywords
globalize_accessors :locales => [:en, :fr], :attributes …Run Code Online (Sandbox Code Playgroud) ruby-on-rails friendly-id globalize3 globalize ruby-on-rails-4.2
我有小桌子:
create_table :cities do |t|
t.string :name
end
Run Code Online (Sandbox Code Playgroud)
我需要国际化"名称"列,我不想为此创建单独的表.是否可以将翻译列添加到"城市"表中?结果我希望这个表的迁移看起来像这样:
create_table :cities do |t|
t.string :en_name
t.string :de_name
t.string :fr_name
end
Run Code Online (Sandbox Code Playgroud)
目前我正在尝试使用"globalize"gem,也许我应该使用其他解决方案,请指教.
ruby-on-rails internationalization globalize3 rails-i18n globalize
我有一个Rails 5应用程序,我使用Globalize进行本地化.我目前遇到的问题是,如果没有任何翻译,我无法保存新对象.
我的模型看起来像这样:
# Product.rb
translates :description, :fallbacks_for_empty_translations => true
has_many :translations
accepts_nested_attributes_for :translations
# ProductTranslation.rb
belongs_to :product, optional: true
Run Code Online (Sandbox Code Playgroud)
我的数据库模式查找我的product_translations,如下所示:
t.integer "product_id", null: false
t.string "locale", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.text "description"
t.index ["locale"], name: "index_product_translations_on_locale"
t.index ["product_id"], name: "index_product_translations_on_product_id"
Run Code Online (Sandbox Code Playgroud)
当我尝试保存新产品时,我目前收到此错误:
Unable to save product with id = got these validation errors {:"translations.globalized_model"=>["translation missing: sv.activerecord.errors.models.product/translation.attributes.globalized_model.required"]}
Run Code Online (Sandbox Code Playgroud)
关于我能做些什么才能让它发挥作用的任何想法?
我如何为我的模型编写表单,我使用rails 4和https://github.com/globalize/globalize进行翻译.我想在一个表单中显示所有翻译,如下例所示.我在这里找到了一个解决方案https://github.com/rilla/batch_translations但我不知道如何实现这一点.这个"批量翻译"是一个宝石还是什么?以及如何安装它.
<h1>Editing post</h1>
<% form_for(@post) do |f| %>
<%= f.error_messages %>
<h2>English (default locale)</h2>
<p><%= f.text_field :title %></p>
<p><%= f.text_field :teaser %></p>
<p><%= f.text_field :body %></p>
<hr/>
<h2>Spanish translation</h2>
<% f.globalize_fields_for :es do |g| %>
<p><%= g.text_field :title %></p>
<p><%= g.text_field :teaser %></p>
<p><%= g.text_field :body %></p>
<% end %>
<hr/>
<h2>French translation</h2>
<% f.globalize_fields_for :fr do |g| %>
<p><%= g.text_field :title %></p>
<p><%= g.text_field :teaser %></p>
<p><%= g.text_field :body %></p>
<% end %>
<% …Run Code Online (Sandbox Code Playgroud) 我升级到 Rails 4 并进行了全球化(而不是 globalize3),此后翻译无法正常工作。
脚步:
创建英文记录
将区域设置更改为 :es
然后在刚刚创建的对象上调用 update_attributes
这用于在 es 中创建新的翻译。
但是,现在,它正在修改英文记录!!!请帮忙?
宝石档案:
gem "rails", "4.0.1"
gem "globalize", "~> 4.0.0.alpha.1"
Run Code Online (Sandbox Code Playgroud)
模型:
class GlossaryTerm < ActiveRecord::Base
translates :term, :definition
accepts_nested_attributes_for :translations
has_many :glossary_term_translations
belongs_to :section
validates_presence_of :term
validates_presence_of :definition
**after_create :create_spanish_placeholder**
def create_spanish_placeholder
term = self.term
definition = self.definition
I18n.locale = :es
self.update_attributes(:term => "SPANISH placeholder for #{term}", :definition => "SPANISH placeholder for #{definition}")
I18n.locale = :en
end
#...
end
Run Code Online (Sandbox Code Playgroud)
控制器:
class Admin::GlossaryTermsController < ApplicationController
before_filter :authorize_sysadmin!
def …Run Code Online (Sandbox Code Playgroud) 我gobalize在Rails 4.1.12中使用gem 4.0.3.
我有一个Post模型,我运行Post.create_translation_table!提供的迁移globalize来设置一个post_translations表.
现在我想自动加载夹具文件的翻译.Fixtures支持关联的标签引用,所以我有这个:
# spec/fixtures/posts.yml
my_first_post:
author: dave
# spec/fixtures/post_translations.yml
my_first_post_translation:
locale: en
title: My first post
content: What a time to be alive!
post: my_first_post
# spec/models/post_spec
require 'rails_helper'
RSpec.describe Post, type: :model do
fixtures('post/translations', :posts, :authors)
subject(:post) { posts(:my_first_post) }
it "has an author" do
expect(post.author).to eq(authors(:dave))
end
it "has a title" do
binding.pry
expect(post.title).to be_present
end
end
Run Code Online (Sandbox Code Playgroud)
但运行RSpec会引发以下错误:
Failure/Error: Unable to find matching …Run Code Online (Sandbox Code Playgroud) activerecord ruby-on-rails fixtures ruby-on-rails-4 globalize
我使用宝石globalize和globalize_accessors翻译我的模型.这是一个例子:
# model.rb
class Model < ActiveRecord::Base
translates :title, :description
globalize_accessors
end
Run Code Online (Sandbox Code Playgroud)
我希望能够检索字段列表,即[:title, :description]在表单中循环它们.
我已经四处寻找,我唯一能找到的就是#globalize_attribute_names方法.但是,它返回一个翻译字段列表,其中的语言环境按原始标题排序:
[:title_en, :title_es, :title_xx, :description_en, ... ]
Run Code Online (Sandbox Code Playgroud)
所以,问题是 - 有没有办法获得我指定的字段列表translates?
我有点像这样修理它,但它不是很好:
def translates
globalize_attribute_names.map do |name|
name[/(\w+)_\w{2}\z/]
Regexp.last_match[1]
end.uniq
end
Run Code Online (Sandbox Code Playgroud) 我不知道如何找到一个可以理解的标题,所以我会尽力解释我的问题.
我有2个模型: - 可全球化的国家,名称和许多地区 - 地区所属国家
我想做的是从一系列国家/地区制作一系列所有地区.
例如
Country.all.regions
Country.with_translations(I18n.locale).order("country_translations.name asc").regions
Run Code Online (Sandbox Code Playgroud)
有一个简单的方法来获得这个数组?
我想订购一些与另一个模型有关系的模型的记录(带有翻译的属性).这是一个例子:
现在,我想通过其任务名称来订购所有项目.我怎么写这个范围?如何在Rails中加入转换表,如gem globalize中的方法with_translation(https://github.com/globalize/globalize/blob/eccb924ac9641b52399f22525b0e3ec004739f4c/lib/globalize/active_record/class_methods.rb),但是来自相关对象Project?
> Project.all.joins(:tasks) ... (how to include task translation table) ...
Run Code Online (Sandbox Code Playgroud) ruby-on-rails relationship has-many-through ruby-on-rails-4 globalize
globalize ×10
globalize3 ×3
rails-i18n ×3
ruby ×3
activerecord ×1
fixtures ×1
friendly-id ×1
has-many ×1
relationship ×1