Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows you to easily overwrite the translation of object_name in resource_controller by adding a method called translated_object_name. #419

Closed
wants to merge 1 commit into from

Conversation

citrus
Copy link
Contributor

@citrus citrus commented Jun 7, 2011

By adding the translated_object_name method, users and extension developers can easily avoid errors that would otherwise be caused by resource_controller looking up the object_name in a translation file.

For example, If I store my models attributes in my en.yml like so: (and I often do)

en: 
  post:
    title: Title
    posted_at: Posted At
    body: Body
    ...

Then resource controller would try to make the flash message out of a hash in any of the actions:

def update
  invoke_callbacks(:update, :before)
  if @object.update_attributes(params[object_name])
    invoke_callbacks(:update, :after)

    # object_name in this case is 'post'
    resource_desc = I18n.t(object_name)

    resource_desc += " \"#{@object.name}\"" if @object.respond_to?(:name)
    flash[:notice] = I18n.t(:successfully_updated, :resource => resource_desc)
    respond_with(@object) do |format|
      format.html { redirect_to location_after_save }
      format.js   { render :layout => false }
    end
  else
    invoke_callbacks(:update, :fails)
    respond_with(@object)
  end
end

This raises a NoMethod error:

NoMethodError in Admin::Blog::PostsController#update

undefined method `+' for #Hash:0x000001067aac40

After this patch, I could easily avoid this error by adding this method and the appropriate translation to my en.yml:

def translated_object_name
  I18n.t("post.model_name")
end

Thanks!

@schof
Copy link
Contributor

schof commented Jun 8, 2011

@romul I'll let you review this one since you use i18n more than me

@citrus
Copy link
Contributor Author

citrus commented Jun 8, 2011

thanks for checking this out @schof

just fyi, I spoke about this with @BDQ earlier on IRC..

@romul
Copy link
Member

romul commented Jun 8, 2011

@citrus: First of all you have I18n error in your en.yml. Correct option should be

en: 
  activerecord:
    attributes:
      post:
        title: Title
        posted_at: Posted At
        body: Body

Although in any case I'll improve flash message generation tomorrow, but in more general manner. Thanks for pointing this issue.

@citrus
Copy link
Contributor Author

citrus commented Jun 8, 2011

sure @romul .. whatever makes you happy...

@citrus
Copy link
Contributor Author

citrus commented Jun 8, 2011

Maybe we should look beyond 'my errors' and examine the underlying issue here... there is no way to customize the translation for the flash message, regardless of how your en.yml is set up...

@romul
Copy link
Member

romul commented Jun 8, 2011

Yup, I plan to handle precisely the underlying issue.

@romul romul closed this in 4619b41 Jun 9, 2011
@citrus
Copy link
Contributor Author

citrus commented Jun 9, 2011

cool thanks @romul

@citrus
Copy link
Contributor Author

citrus commented Jun 9, 2011

I'm a bit confused though.. What if we don't want to use object.class.model_name.human as our resource descriptor? Say I have a model called BlogComment or something.. but want the flash message just to say Comment... and maybe something else in a different language?

Basically your solution fixes the error but doesn't allow for any customization. Can we please add a translated_object_name method?

def translated_object_name
  object.class.model_name.human
end

@romul
Copy link
Member

romul commented Jun 10, 2011

Basically your solution fixes the error but doesn't allow for any customization.

You can customize model name via locales:

en:
  activerecord:
    models:
      blog_comment:
        one: Comment

Besides, you can even override model_name method (it should returns instance of ActiveModel::Name) in your model to handle extraordinary cases.
Besides, if you want fully custom notices for some controllers, you can override whole flash_message_for method.

romul added a commit to romul/spree that referenced this pull request Jun 29, 2011
…separate method to allow easily override it for different locales.

fixes spree#419
jordan-brough added a commit to jordan-brough/spree that referenced this pull request Jul 15, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants