howto translate non ActiveRecords Attributes?

29 views
Skip to first unread message

fwalter

unread,
Sep 30, 2008, 5:31:24 AM9/30/08
to rails-i18n
Hi,

I am using a model which is not an ActiveRecord.
>>>>>>>>>>>>>>

class ContactFormPresenter
include Validatable

ATTRIBUTES = %w(name email subject comment)
ATTRIBUTES.each { |name| attr_accessor name }

ATTRIBUTES.each { |name| validates_presence_of name }
validates_format_of :email, :with => RegexCollection::EMAIL_REGEX

def initialize(attributes = nil)
ATTRIBUTES.each { |name| self.send("#{name}=",
attributes[name.to_sym]) } if attributes.is_a?(Hash)
end
end

<<<<<<<<<<<<<<

if i don't put the :message =>
I18n.t(:'activerecord.errors.messages.blank') , I only get the
base_locale translation.

btw. I would like to translate these attributes like ActiveRecords
Attributes. how are the keys for that? active records are:

de:
activerecord:
attributes:
<modelname>:
<attributename>:

do you have any hints for me, solving that?

thanks for help.

florian

iain hecker

unread,
Sep 30, 2008, 5:44:46 AM9/30/08
to rails-i18n
Hi again florian,

I have made a plugin to do what you wanted:
http://iain.nl/2008/09/acts_as_translatable_model-plugin/

I don't know if that "include Validatable" uses the official Rails
validation, but you can include the official Rails validation too (it
requires some more work though)

I already mentioned how to use translations for activerecord in my
other reply ;)

--
Iain Hecker
http://iain.nl/

fwalter

unread,
Sep 30, 2008, 5:52:29 AM9/30/08
to rails-i18n
thx,

ill try it

thanx

On Sep 30, 11:44 am, iain hecker <i...@iain.nl> wrote:
> Hi again florian,
>
> I have made a plugin to do what you wanted:http://iain.nl/2008/09/acts_as_translatable_model-plugin/
>
> I don't know if that "include Validatable" uses the official Rails
> validation, but you can include the official Rails validation too (it
> requires some more work though)
>
> I already mentioned how to use translations for activerecord in my
> other reply ;)
>
> --
> Iain Heckerhttp://iain.nl/

fwalter

unread,
Sep 30, 2008, 6:21:36 AM9/30/08
to rails-i18n
hey,

i tried that plugin, you mentioned before.

when i run script/console and Model.human_attribute_name i get the
translated attribute name, i configured in one of my yml files.

but in my form
>>>>>>>>>>>>>>>>

<% form_for(:mail_contact, :url => contact_path) do |f| %>
<div class="question_form_info">
<p>
<%= label_tag 'mail_contact_name', t(:"info.name") + ":" %>
<%= f.text_field(:name, { :size => 25}) %>
</p>
<p>
<%= f.label :email %>
<%= f.text_field(:email, { :size => 15}) %>
</p>
<p>
<%= label_tag 'mail_contact_name', t(:"info.subject") + ":" %>
<%= f.text_field(:subject, { :size => 15}) %>
</p>
<p>
<%= label_tag 'mail_contact_comment', t(:"info.your_request") +
":" %>
<%= f.text_area(:comment, { :size => 15}) %>
</p>
</div>
<div class="question_form_submit">
<%= submit_tag t(:"common.send") %>
</div>
<% end %>

<<<<<<<<<<<<<<<<
my contact_form_presenter.rb

class ContactFormPresenter
include Validatable

acts_as_translatable_model

ATTRIBUTES = %w(name email subject comment)
ATTRIBUTES.each { |name| attr_accessor name }

ATTRIBUTES.each { |name| validates_presence_of name }
validates_format_of :email, :with => RegexCollection::EMAIL_REGEX

def initialize(attributes = nil)
ATTRIBUTES.each { |name| self.send("#{name}=",
attributes[name.to_sym]) } if attributes.is_a?(Hash)
end
end
<<<<<<<<<<<<<<
my yml:
de:
activerecord:
attributes:
contact_form_presenter:
name: "Name_de"
subject: "Subject_de"
email: "Email_de"
comment: "Comment_de"
en:
activerecord:
attributes:
contact_form_presenter:
name: "Name_en"
subject: "Subject_en"
email: "Email_en"
comment: "Comment_en"
<<<<<<<<<<<<<<
script/console:

>> I18n.locale
=> :"en-US"
>> ContactFormPresenter.human_attribute_name("subject")
=> "Subject_en"
>> I18n.locale=:"de-DE"
=> :"de-DE"
>> ContactFormPresenter.human_attribute_name("subject")
=> "Subject_de"
<<<<<<<<<<<<<<


i have set @mail_contact = ContactFormPresenter.new in my controller

but the email label is not translated,

and when i display the error_messages_for :mail_contact

but the attributes are not translated.

did i do something wrong?

thx, florian

Iain Hecker

unread,
Sep 30, 2008, 6:30:26 AM9/30/08
to rails...@googlegroups.com
Wow, you're about to use all of my plugins ;)

About the labels: http://iain.nl/2008/09/form-labels-in-rails-22/
Make sure you pass the instance variable to the form though:

<% form_for(@mail_contact) do |f| %>
<%= f.label :name %>
etc...

fwalter

unread,
Sep 30, 2008, 6:56:49 AM9/30/08
to rails-i18n
hey, i tried to follow your instructions:

<% form_for ContactFormPresenter.new, :url => contact_path do |f| %>
<%= f.label :email %>
<%= f.text_field :email %>
<% end %>

but it is not translated.
OUTPUT:

<form id="edit_contact_form_presenter_55524240"
class="edit_contact_form_presenter" method="post" action="/contact">
<div style="margin: 0pt; padding: 0pt;">
</div>
<label for="contact_form_presenter_email">Email</label>
<input id="contact_form_presenter_email" type="text" size="30"
name="contact_form_presenter[email]"/>
</form>

what am i doing wrong?

thx, florian

On Sep 30, 12:30 pm, "Iain Hecker" <i...@iain.nl> wrote:
> Wow, you're about to use all of my plugins ;)
>
> About the labels:http://iain.nl/2008/09/form-labels-in-rails-22/
> Make sure you pass the instance variable to the form though:
>
> <% form_for(@mail_contact) do |f| %>
>   <%= f.label :name %>
> etc...
>

fwalter

unread,
Sep 30, 2008, 7:04:08 AM9/30/08
to rails-i18n
man, i am an idiot.

i didnt check, that it is a different plugin..

it works fine with the labels!

thanks
Reply all
Reply to author
Forward
0 new messages