为什么我翻译不了这个词?
配置/区域设置/pt-BR.yml
pt-BR:
testing:
off: 'Desligado'
offff: 'Test'
Run Code Online (Sandbox Code Playgroud)
导轨控制台
> I18n.t 'testing.offff'
=> "Test"
> I18n.t 'testing.off'
=> "translation missing: pt-BR.testing.off"
Run Code Online (Sandbox Code Playgroud) 我的语言环境文件因一堆嵌套键而变得笨拙。有没有办法从单个区域设置文件中获取所有可用区域设置键或所有区域设置键的列表?
例如。
en:
car:
honda:
civic:
name: 'Civic'
description: 'Entry Level Sedan'
ferrari:
la_ferrari:
name: 'La Ferrari'
description: 'Supercar'
Run Code Online (Sandbox Code Playgroud)
此语言环境应返回键列表,在本例中为
['en.car.honda.civic.name', 'en.car.honda.civic.description',
'en.ferrari.la_ferrari.name', 'en.car.ferrari.la_ferrari.name.description']
Run Code Online (Sandbox Code Playgroud)
有 Rails (I18n) 助手可以做到这一点吗?另一种方法是迭代解析的 YAML。
我在我的es.yml语言环境中有以下代码:
es:\n activerecord:\n models:\n courier:\n one: Repartidor\n other: Repartidores\n admin_user:\n one: Administrador\n other: Administradores\n attributes:\n courier:\n ci: C\xc3\xa9dula\n first_name: Nombre\n last_name: Apellido\n password: Contrase\xc3\xb1a\n password_confirmation: Confirmaci\xc3\xb3n de contrase\xc3\xb1a\n sign_in_count: Veces que inici\xc3\xb3 sesi\xc3\xb3n\n created_at: Fecha de creaci\xc3\xb3n\n updated_at: \xc3\x9altima actualizaci\xc3\xb3n\n admin_user:\n email: Email\n last_sign_in_ip: \xc3\x9atima IP utilizada\n last_sign_in_at: \xc3\x9atimo inicio de sesi\xc3\xb3n\n current_sign_in_ip: Actual IP\n current_sign_in_at: Actual inicio de sesi\xc3\xb3n\n sign_in_count: Veces que inici\xc3\xb3 sesi\xc3\xb3n\n created_at: Fecha de creaci\xc3\xb3n\n updated_at: \xc3\x9altima actualizaci\xc3\xb3n\n password: Contrase\xc3\xb1a\n password_confirmation: Confirmaci\xc3\xb3n de …Run Code Online (Sandbox Code Playgroud) 我有rails 6.1.6.1以下弃用警告
DEPRECATION WARNING: action_view.raise_on_missing_translations is deprecated and will be removed in Rails 7.0. Set i18n.raise_on_missing_translations instead. Note that this new setting also affects how missing translations are handled in controllers.
Run Code Online (Sandbox Code Playgroud)
如何设置该raise_on_missing_translations选项,以及在哪里设置它(例如在初始化程序中)?
我想改变现有项目中的I18n.translate方法.
require 'I18n'
module I18n
alias_method :old_translate, :translate
def translate(*args)
old_translate(*args) + 'blabla'
end
alias_method :t, :translate
end
Run Code Online (Sandbox Code Playgroud)
这会产生:
未捕获的异常:缺少帮助文件助手/ I18n.rb
我做错了什么以及我应该把这段代码放在哪里?
我从来没有真正处理或理解en.yml文件,因为我以前没有使用它,今天我正在尝试更改它并自定义表单提交失败时显示的验证错误消息
我有一个食谱模型和一个提交食谱的表格.目前我的en.yml文件看起来像这样
en:
activerecord:
models:
recipe:
attributes:
user:
email:
errors:
models:
recipe:
attributes:
dish_name:
blank: "Dont forget to give your Recipe a Dish Name"
Run Code Online (Sandbox Code Playgroud)
现在显然这是错误的,我正在寻找一些关于如何布局文件的帮助,也许是对正在发生的事情的解释,我已经阅读了文档,但是当涉及到rails时,它的技能水平有点过高.
此外,如果dish_name的验证失败,即它的空白,我收到此错误消息
Dish name Dont forget to give your Recipe a Dish Name
Run Code Online (Sandbox Code Playgroud)
任何帮助赞赏
谢谢
我正在尝试使用spree自己的spree_i18ngem 来国际化Rails/Spree应用程序,但我无法让它工作.
简而言之,我在ApplicationController中有以下代码:
before_action :set_locale
def set_locale
I18n.locale = params[:locale] || I18n.default_locale
puts I18n.locale
end
Run Code Online (Sandbox Code Playgroud)
我的视图中的代码应该被翻译(<%= t("whatever") %>).但无论我做什么,文本总是以英文输出.
使用一些额外的调试代码,我可以看到一旦set_locale调用,但执行仍然在控制器内,语言环境是正确的(例如,如果我访问url /?locale=es,那么puts上面的控制器代码输出中的语句es).
但是当执行到达视图时,语言环境已经以某种方式被重置为en.(例如<% raise I18n.locale.to_s %>,在视图中添加会将"en"作为错误消息引发.)
我已经在Spree的Github上打开了一个问题,因为据我所知,我已完全按照他们的指示操作,但仍然没有工作,但我可能仍然遗漏了一些东西.为什么区域设置没有正确设置?
(注意:我应该补充说,Spree.t它不起作用,不仅仅是t.)
编辑:如果你看看我的Github问题的评论,你会看到我的工作.但是,我99%肯定我的解决方案是一个黑客,并且我应该使用更好的方法.赏金可以告诉我什么是我错了.
我怎么能纠正这个"i18n需要Ruby版本> = 1.9.3"我在运行"bundler install"时得到了什么?
背景:需要在dreamhost上使用ruby 1.8.7,因此针对此确定了Rails v3.2.
命令行
Gregs-MacBook-Pro:weekends Greg$ ruby -v
ruby 1.8.7 (2013-12-22 patchlevel 375) [i686-darwin14.1.0]
Gregs-MacBook-Pro:weekends Greg$ bundler -v
Bundler version 1.9.2
Gregs-MacBook-Pro:weekends Greg$ bundler install
Fetching gem metadata from https://rubygems.org/..........
Fetching version metadata from https://rubygems.org/...
Fetching dependency metadata from https://rubygems.org/..
Resolving dependencies............
Using rake 10.4.2
Gem::InstallError: i18n requires Ruby version >= 1.9.3.
An error occurred while installing i18n (0.7.0), and Bundler cannot continue.
Make sure that `gem install i18n -v '0.7.0'` succeeds before bundling.
Gregs-MacBook-Pro:weekends Greg$ …Run Code Online (Sandbox Code Playgroud) 我将Rails 4与Rails-i18n Gem一起使用,我想用我的语言翻译文件中的占位符替换我的硬编码字符串"300px",如config/locales/de.yml中的%{minimum_resolution}
activerecord:
errors:
models:
organisation:
attributes:
image:
resolution_too_small:"Image Resolution should be at least %{minimum_resolution}"
Run Code Online (Sandbox Code Playgroud)
%{minimum_resolution}中的值应来自app/models/organisation.rb中的自定义验证
def validate_minimum_image_dimensions
if image.present?
logo = MiniMagick::Image.open(image.path)
minimum_resolution = 300
unless logo[:width] > minimum_resolution || logo[:height] > minimum_resolution
errors.add :image, :minimum_image_size
end
else
return false
end
end
Run Code Online (Sandbox Code Playgroud)
如何从minimum_resolution获取值到我的yaml文件中?
我在我的应用程序中使用了服务,这不是"标准"应用程序组件之一.
假设我的规格测试如下
需要"rails_helper"
# spec/services/create_user.rb
RSpec.describe CreateUser, type: :service do
it "returns the error message" do
error_message = Foo.new.errors
expect(error_message).to eq(t("foo.errors.message"))
end
Run Code Online (Sandbox Code Playgroud)
结束
只测试返回的字符串是否与特定的翻译字符串匹配.
但是,这会引发错误,因为帮助t()程序不可用.
我可以将它明确地称为I18n.t(),但出于我自己的好奇心,我如何包含正确的模块以获得调用速记形式的奢侈品?
谢谢!