For test reasons i tried:
I18n.locale = "fehler_ausgabe"
puts I18n.translate "activerecord.attributes.diagnosis.sicherheit"
What worked correctly and returned E-mail addresse for this yaml-file:
fehler_ausgabe:
activerecord:
attributes:
diagnosis:
sicherheit: "E-mail addresse"
Next i tried to test a error that is in my model defined like this:
validates :sicherheit, inclusion: { in: ["U","V"]}, unless: :skip_fehler
I changed my yaml file:
fehler_ausgabe:
activerecord:
attributes:
diagnosis:
sicherheit: "E-mail addresse"
inclusion: "muss zugeorndet werden"
I tried again:
puts I18n.translate "activerecord.attributes.diagnosis.sicherheit"
But now somehow get this error:
I18n::InvalidLocaleData (can not load translations from C:/Sites/heroku
2/config/locales/fehler_ausgabe.yml: #<Psych::SyntaxError: (C:/Sites/heroku2/config/locales/fehler_ausgabe.yml):
did not find expected key while parsin
g a block mapping at line 5 column 9>):
What do i wrong? I dont understand why my code now throws a error! THANKS
Your change:
fehler_ausgabe:
activerecord:
attributes:
diagnosis:
sicherheit: "E-mail addresse"
inclusion: "muss zugeorndet werden"
makes your YAML file invalid. You can not have inclusion: "muss zugeorndet werden" if you tend to have sicherheit: "E-mail addresse". Either remove inclusion: "muss zugeorndet werden" or "E-mail addresse" to make your arrangement work.
However, you can checkout this answer to setup inclusion validation properly in your YAML.
Related
I have a ruby on rails application. I have spanish and english support in my application. However, I get a translation missing exception in case spanish mode.
I have the following model:
class Company < ApplicationRecord
validates :name, length: { in: 5..15, message: :bad_name }
end
en.yml
en:
activerecord:
errors:
models:
company:
attributes:
name:
bad_name: 'message in english'
sp.yml
sp:
activerecord:
errors:
models:
company:
attributes:
name:
bad_name: 'message in spanish'
Just in case the error when I open the application in english. I get the "message in english" message and that's ok.
On the other hand, when I open and test it in spanish, I get the following error.
ActiveRecord::RecordInvalid:
translation missing: sp.activerecord.errors.messages.record_invalid
I can't see what I'm missing,
Any suggestions,
Thanks.
Are other translations work in this file?
Try to rename sp.yml to es.yml, because this is the iso-code for "espanol".
Also try to include a message for activerecord.errors.messages.record_invalid in your spansih file. will this be displayed?
Actually, to use Spanish within your locale you would need to set I18n.locale = :es. I think you mixed up your locales. The correct language file should be named es.yml and look like:
es:
activerecord:
errors:
models:
company:
attributes:
name:
bad_name: 'message in spanish'
I saw a lot of topics on Stack but still can't find solution.
I had to translate my model attribute so I followed:
Translations for Active Record Models
I want get something like this: "Pozycja nie może być puste" <= "%{attribute} %{message}
attribute => position
message => nie może być puste"
So I built this but it isn't working. How setup this interpolation?
errors:
format: "%{attribute} %{message}"
messages:
blank: "nie może być puste"
activerecord:
models:
user:
attributes:
position: "Pozycja"
The keys hierarchy should be proper,
It should be like this
pl:
errors:
format: "%{attribute} %{message}"
messages:
blank: "nie może być puste"
activerecord:
attributes:
user:
position: "Pozycja"
For more you can always refer rails en.yml
So the issue I've encountered days ago still makes me unconfortable with my code. I can't get proper translation for my application: Yml looks like that:
pl:
errors: &errors
format: ! '%{attribute} %{message}'
messages:
confirmation: nie zgadza się z potwierdzeniem
activemodel:
errors:
<<: *errors
activerecord:
errors:
<<: *errors
And model looks like that:
module Account
class User < ActiveRecord::Base
attr_accessor: password_confirmation
end
end
And flash in controller declared:
flash[:errors] = #user.errors.full_messages
I tried and read activerecord documentation and stack previous questions (How to use Rails I18n.t to translate an ActiveRecord attribute?, Translated attributes in Rails error messages (Rails 2.3.2, I18N)). Yet it still doesn't work as I wanted.
password_confirmation remains "Password Confirmation", and not "Potwierdzenie hasła" as it shoul be. The screenshot might explain it better: http://i42.tinypic.com/1glz5.png
Your User model is in a namespace, thus you have to declare the namespace in your translation file also. Try the following to get the correct translation for password_confirmation:
pl:
activerecord:
attributes:
account/user:
password_confirmation: "Potwierdzenie hasła"
I wanted to set a custom error message for when an email was already taken so I edited the config/locales/en.yml file. It looked like this:
en:
activerecord:
models:
user:
email:
taken: "already being used"
When I submit the form with email in it, I get this error:
translation data {:email=>{:taken=>"already being used"}} can not be used with :count => 1
I've only just started looking into i18n so this may be a really simple mistake but I can't find an answer.
Try:
en:
activerecord:
errors:
models:
user:
attributes:
email:
taken: "already being used"
See section 5.1.1 in Rails Guide.
Hope this helps!
I'm a newbie in rails, today I made my first web application using the validate, I just put this lines into the model:
class ClientWorkout < ActiveRecord::Base
validates_numericality_of :paid_amount
validates_presence_of :client_name
end
This is the view part:
<% form_for(#client_workout) do |f| %>
<%= f.error_messages %>
etc etc
Everything works fine, and the value are stored in the db, in succesful case< if an error occour, instead, this error displays on the view in this strange following manner:
{{count}} errors prohibited this {{model}} from being saved
There were problems with the following fields:
{{attribute}} {{message}}
{{attribute}} {{message}}
(The example show what's happening when 2 parameters of the form are wrong, but this happen in every case)
It doesen't manage to replace "count, model, attribute and message" with the real value.
Anyone can figure out about what's happenned ?
I use Ror 2.3.8 and rails 1.8.7
Rails introduced built-in internationalization back in 2.3. Your issue is a known bug with some combinations of rails and the i18n gem. If you have i18n gem version 0.5.0, try downgrading to 0.4.2. If you're using system gems:
sudo gem uninstall i18n
sudo gem install i18n -v 0.4.2
If you're using RVM to manage your gems, you don't need the sudo command.
If you are not interested in changing the i18n version you can do the following
Add the below code in config/locales/en.ym
If en: is already available copy and paste from ActiveRecord
After that stop the server and start again that should display the error messages properly...
en:
activerecord:
errors:
full_messages:
format: "%{attribute} %{message}"
messages:
inclusion: "is not included in the list"
exclusion: "is reserved"
invalid: "is invalid"
confirmation: "doesn't match %{attribute}"
accepted: "must be accepted"
empty: "can't be empty"
blank: "can't be blank"
too_long: "is too long (maximum is %{count} characters)"
too_short: "is too short (minimum is %{count} characters)"
wrong_length: "is the wrong length (should be %{count} characters)"
not_a_number: "is not a number"
not_an_integer: "must be an integer"
greater_than: "must be greater than %{count}"
greater_than_or_equal_to: "must be greater than or equal to %{count}"
equal_to: "must be equal to %{count}"
less_than: "must be less than %{count}"
less_than_or_equal_to: "must be less than or equal to %{count}"
other_than: "must be other than %{count}"
odd: "must be odd"
even: "must be even"
template:
header:
one: "1 error prohibited this %{model} from being saved"
other: "%{count} errors prohibited this %{model} from being saved