Reputation: 1585
I've created a virtual (non-persistent) model in Rails 3 (see below)
I now need to apply translations to the model but the standard translations locations don't seem to work. e.g.
en:
activerecord:
attributes:
media_upload:
title: "My Title"
I know I can apply this directly to the label with an optional string parameter eg. f.label :title, t('activerecord.attributes.media_upload')
but that doesn't work for error messages resulting from validations. Similarly, I could add a key to the translations file for the label helper as suggested in Localise nested virtual attribute in Rails but this also fails to work for the validations.
helpers:
label:
media_upload:
title: "My Title"
Apart from redefining all of the relevant validation messages, is there any other way I can do localisation of attributes in non-persistent models??
A sample model is shown below,
class MediaUpload
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming
attr_accessor :media_file, :title
validates_presence_of :media_file
validates_presence_of :title
def initialize(attributes = {})
unless attributes.nil?
attributes.each do |name, value|
send("#{name}=", value)
end
end
end
def persisted?
false
end
end
Upvotes: 4
Views: 949
Reputation: 613
It seems like you are using simple_form gem for generation of forms.
So following i18n chapter from github your internationalization file should look like this
en:
simple_form:
labels:
media_upload:
media_file: My File
title: My Title
If you are using Rails 4 than there is an easier way to make ActiveModel Form Objects. You can just include ActiveModel::Model
like so
class MediaUpload
include ActiveModel::Model
attr_accessor :media_file, :title
validates_presence_of :media_file
validates_presence_of :title
end
Upvotes: 0
Reputation: 570
You need write like this:
en:
activemodel:
attributes:
media_upload:
title: "My Title"
not activerecord
replace it with activemodel
Upvotes: 5