Posted By

insanedreamer on 12/07/10


Tagged

i18n rails3


Versions (?)

Automagically find localized images


 / Published in: Rails
 

Patches Rails 3 to automatically look for images in a folder corresponding to the current locale (under 'images', ie: '/images/en'), and falling back first to the folder of the defaultlocale, and then to the main images folder ('/images') if the image was not found in the locale folder. Put any localized images in their corresponding folders ('/images/en', '/images/es', etc.) and any non-localized images in '/images'. No need to specify anything special in the view, just use imagetag as usual.

  1. module ActionView
  2. module Helpers #:nodoc:
  3. module AssetTagHelper
  4. def image_path(source)
  5. if File.exists? [RAILS_ROOT, 'public', 'images', I18n.locale, source].join('/')
  6. compute_public_path(source, "images/#{I18n.locale}")
  7. elsif File.exists? [RAILS_ROOT, 'public', 'images', I18n.default_locale, source].join('/')
  8. compute_public_path(source, "images/#{I18n.default_locale}")
  9. else
  10. compute_public_path(source, 'images')
  11. end
  12. end
  13. alias_method :path_to_image, :image_path
  14. end
  15. end
  16. end

Report this snippet  

You need to login to post a comment.