我有一个text_field :birthday_line在我的用户表单中,我需要解析用户的birthday属性.所以我在User课堂上做这样的事.
attr_accessor :birthday_line
before_save :set_birthday
def set_birthday
self.birthday = Date.strptime(birthday_line, I18n.translate("date.formats.default")
end
Run Code Online (Sandbox Code Playgroud)
但问题是,由于某种原因,它给了我一个错误,说Invalid date我何时尝试传入一个27 ?????? 1987 ?.应该被解析的字符串1987-01-27.我的格式和月份名称config/locales/ru.yml
ru:
date:
formats:
default: "%d %B %Y ?."
month_names: [~, ??????, ???????, ?????, ??????, ???, ????, ????, ???????, ????????, ???????, ??????, ???????]
Run Code Online (Sandbox Code Playgroud)
似乎是正确的.
Date.parse 也没有帮助,它只是解析日期编号(27)并将月份和年份放到今天的日期(因此它将是2013年9月27日而不是1987年1月27日).
例如,如果我有YAML文件
en:
questions:
new: 'New Question'
other:
recent: 'Recent'
old: 'Old'
Run Code Online (Sandbox Code Playgroud)
这最终会像json对象一样
{
'questions.new': 'New Question',
'questions.other.recent': 'Recent',
'questions.other.old': 'Old'
}
Run Code Online (Sandbox Code Playgroud) 我正在使用子域来确定Rails 4网站中的区域设置.我有一个完全按照我想要的方式使用区域设置切换器,但现在我需要翻译路线,我不确定最好的方法.
我查看了https://github.com/kwi/i18n_routing i18n路由gem,但这不适用于子域,它似乎通过添加/ locale来改变路径,这不是我需要的.
其他宝石似乎与Rails 4过时了.
编辑
基本上我希望能够使用相同的视图助手,但是更改URL以使用所选子域反映的任何语言.这只是路线翻译.
我有语言特定的模板,我可以生成语言的导航模板,但我真的不想担心改变erb路径和url erb标签
结束编辑
来自routes.rb的示例
scope module: 'countries', shallow: true do
get 'south_american', to: 'south_america#index', as: :south_america
scope module: 'south_america' do
get 'south-america-weather', to: 'weather#index', as: :south_america_weather
get 'south-america-gps-maps', to: 'gps#index', as: :south_america_gps
get 'south-america-accommodation', to: 'hotels#index', as: :south_america_hotels
get 'south-america-vacations', to: 'vacations#index', as: :south_america_vacations
get 'south-america-facts', to: 'facts#index', as: :south_america_facts
end
Run Code Online (Sandbox Code Playgroud)
以south_america_hotels_path为例,将生成一个url
南美 - 住宿
这很好,但我怎么翻译这个,以便当我在西班牙子域名south_america_hotels_path时将生成以下网址
酒店预订烯sudamerica
UPDATE
此外,这对于一个url而不仅仅是一个路径是如何工作的,以便生成south_america_hotels_url
en.some_site /南美,住宿
当我在西班牙的子域上时,我会得到
es.some_site /酒店预订烯sudamerica
对于涉及的不同语言环境等等.
我很乐意使用yml文件进行url的翻译或在routes.rb文件中定义其他路由,或者是一个选项,但我更喜欢在routes.rb中定义url,但我无法找到提供相同语言特定URL的方法:作为基于子域的路径/ url选项.
针对先前的答复进行更新和进一步澄清.
更改网址不是一个选项,他们需要匹配现有的网址.我只需要知道如何从视图助手的角度转换它们,而不必更改视图助手.
我正在使用Enumerize gem https://github.com/brainspec/enumerize/ 它允许我使用siple形式的美丽选择.此选择中的所有选项都已翻译.
en:
enumerize:
user:
sex:
male: 'Man'
female: 'Woman'
Run Code Online (Sandbox Code Playgroud)
所以,在我的形式中,我选择了变体'Man'和'Woman'.当我用'Man'值保存记录时,我会得到具有'male'值的性别属性.现在我想在显示页面上将此值显示为"Man",但是
= @user.sex
Run Code Online (Sandbox Code Playgroud)
输出为"男性"而不是"男性"
我正在尝试开发双语应用程序.
但是,我发现默认的i18n gem不会持久化语言环境.
我有这个索引页面并提供了一个更改默认语言的链接.
<% if I18n.locale == I18n.default_locale %>
<%= link_to "English", :locale=>'en'%>
<% else %>
<%= link_to "Français", :locale=>'fr'%>
<%end%>
Run Code Online (Sandbox Code Playgroud)
问题是当我登录到另一个页面时,语言环境会回退到默认语言.
所以,我只是想知道如何保持用户语言选择并持续使用该语言环境直到用户注销.
提前致谢.
如何列出我的元素yml并在视图中循环它们并访问它们的属性?我当前的代码只获取列表中的最后一项.我想在视图中循环遍历项目列表并显示它们title和description元素.
例如
阳明:
en:
hello: "Hello world"
front_page:
index:
description_section:
title: "MyTitle"
items:
item:
title: "first item"
description: "a random description"
item:
title: "second item"
description: "another item description"
Run Code Online (Sandbox Code Playgroud)
视图:
<%= t('front_page.index.description_section.items')do |item| %>
<%= item.title %>
<%= item.description %>
<%end %>
Run Code Online (Sandbox Code Playgroud)
结果:
{:item=>{:title=>"second item", :description=>"another item description"}}
Run Code Online (Sandbox Code Playgroud)
期望的结果:
first item
a random description
second item
another item description
Run Code Online (Sandbox Code Playgroud) 假设我有一个方便的验证类,如:
User < ActiveRecord::Base
validates :username, :format => {/regex/}, :message => :name_format
end
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我可以i18n通过在我的包含中包含以下内容来使错误消息可翻译/config/locals/en.yml:
en:
activerecord:
errors:
models:
user:
attributes:
username:
name_format: 'has the way-wrong format, bro!'
Run Code Online (Sandbox Code Playgroud)
这很好,通常非常方便.
我的问题是:当我有从User继承的子类时会发生什么:
UserSubclassOne < User
# extra stuff
end
UserSubclassTwo < User
# extra stuff
end
...
UserSubclassEnn < User
# extra stuff
end
Run Code Online (Sandbox Code Playgroud)
现在的问题是Rails无法找到翻译user_subclass_one.attributes.username.name_format.
它抱怨说:
translation missing:
en.activerecord.errors.models.user_subclass_one.attributes.username.name_format
Run Code Online (Sandbox Code Playgroud)
我希望Rails会在搜索字符串时查找UserSubclassOne到的层次结构,然后注意它何时被"命中",但是(除非我做了一些可怕的错误)显然不会发生.Useren.yml
一个显而易见的解决办法是复制的数据en.yml.en.errors.models为user,user_subclass_one,user_subclass_two等,但我的Rails的常识告诉我,这是极其错误的.
任何想法,伙计们?
我升级到 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) 在 Rails 4 多域应用程序中,我需要为每个域(总共 3 个域)的 4 种语言设置一组区域设置文件。
一些翻译在域之间重叠,但其中一些非常具体,所以我正在考虑一个有点像这样的结构:
config/locales/en.yml ..fr.yml ..de.yml ..it.yml #is picked up by all domains
config/locales/domain1/en.yml ..fr.yml ..de.yml ..it.yml #is picked up by domain 1
config/locales/domain2/en.yml ..fr.yml ..de.yml ..it.yml #is picked up by domain 2
config/locales/domain3/en.yml ..fr.yml ..de.yml ..it.yml #is picked up by domain 3
Run Code Online (Sandbox Code Playgroud)
这在 Rails 4 中可能吗?如果是这样,进行此设置的最佳方法是什么?
我有一个日期,当它用作英语时效果很好
> Date.strptime("Aug 02, 2015", "%b %d, %Y")
Sun, 02 Aug 2015
Run Code Online (Sandbox Code Playgroud)
但是,当我使用另一种语言时es,这不起作用。例如
> Date.strptime("ago 02, 2015", "%b %d, %Y")
ArgumentError: invalid date
Run Code Online (Sandbox Code Playgroud)
文本字符串ago 02, 2015本身来自另一个服务,我需要将其标准化为特定格式,例如
> I18n.localize(Date.strptime("Aug 02, 2015", "%b %d, %Y"), format: "%m/%d/%Y")
"08/02/2015"
Run Code Online (Sandbox Code Playgroud)
有没有办法在 Ruby 中做到这一点,以便
> I18n.localize(Date.strptime("ago 02, 2015", "%b %d, %Y"), format: "%m/%d/%Y")
"08/02/2015"
Run Code Online (Sandbox Code Playgroud) rails-i18n ×10
ruby ×3
i18n-gem ×2
globalize ×1
inheritance ×1
locale ×1
routing ×1
simple-form ×1
validation ×1