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

Already on GitHub? Sign in to your account

add lazy look up in abstract controller's translate method #7082

Merged
merged 1 commit into from Jul 18, 2012

Conversation

Projects
None yet
7 participants
Contributor

jalkoby commented Jul 18, 2012

As we know in views rails provides a lazy lookup to translate the text. This facilitates reducing code size. But I've always seemed very strange that such a functional is not in the controllers and mailers. In every new project I had a similar method. It would be nice if this was part of the core. So

class ProductController < ApplicationController

  def create
    if @product.save
      redirect_to root_path, :notice => t('.saved')
    else
      flash[:error] = t('.failed')
    end
  end

end

# will equal to 

class ProductController < ApplicationController

  def create
    if @product.save
      redirect_to root_path, :notice => t('product.create.saved')
    else
      flash[:error] = t('product.create.failed')
    end
  end

end

@josevalim josevalim commented on an outdated diff Jul 18, 2012

actionpack/lib/abstract_controller/translation.rb
@@ -1,6 +1,12 @@
module AbstractController
module Translation
def translate(*args)
+ key = args.first
+ if key.is_a?(String) && (key[0] == '.')
+ key = "#{controller_path}.#{action_name}#{key}".underscore.gsub('/', '.')
@josevalim

josevalim Jul 18, 2012

Contributor

This gsub call should apply just to the controller_path.

Also, why the need for underscore?

Contributor

josevalim commented Jul 18, 2012

Thanks, I think this is a fine proposal. Please just take a look at the improvements i proposed.

Contributor

jalkoby commented Jul 18, 2012

Sure, underscore is not needed

Contributor

josevalim commented Jul 18, 2012

Please rebase everything and repush? Ping me when you are done and I will merge it straight away. :)

Contributor

jalkoby commented Jul 18, 2012

I updated master and fit changes to one commit

josevalim added a commit that referenced this pull request Jul 18, 2012

Merge pull request #7082 from jalkoby/lazy_lookup_in_abstract_controller
add lazy look up in abstract controller's translate method

@josevalim josevalim merged commit 1b39d39 into rails:master Jul 18, 2012

Contributor

josevalim commented Jul 18, 2012

Victory! Thanks.

fabn commented Mar 22, 2013

Will be this backported in 3.2.x? It would be very useful.

Member

steveklabnik commented Mar 22, 2013

Features aren't getting backported, just bugfixes.

fabn commented Mar 22, 2013

Ok, thanks. I'll look on how to add this as an extension to the Abstract Controller class.

m5o commented May 13, 2014

I have a behavior, want to share: (Rails 4.1)

class ProductController < ApplicationController
  def create
    flash[:notice] = t('.notice')         # translation: en.product.create.notice 
    flash[:error] = I18n.t('.error')      # translation: en.error
  end
end

carlosramireziii added a commit to carlosramireziii/rails that referenced this pull request Feb 4, 2015

Document lazy lookup behavior for controllers
Pull request #7082 added lazy lookup to controllers using the translate
method, but the documentation still stated that it was available for
views only.

[ci skip]

@m5o I've just uncovered this behavior the hard way ;-). I suppose this happens because t is a helper method, thus available in both views and controllers, whereas I18n.t is not a helper method. Am I wrong?

@m5o and @designrubenz http://guides.rubyonrails.org/i18n.html#lazy-lookup

Automatic translation scoping by partial is only available from the translate view helper method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment