Skip to content

Instantly share code, notes, and snippets.

@ghiculescu
Created June 3, 2017 03:49
Show Gist options
  • Save ghiculescu/43ce40145a70beeb6e9bb26fe51ea8a1 to your computer and use it in GitHub Desktop.
Save ghiculescu/43ce40145a70beeb6e9bb26fe51ea8a1 to your computer and use it in GitHub Desktop.
Monkey patch ActionView's TranslationHelper to add a custom prefix for keys accessed via lazy lookup (http://guides.rubyonrails.org/i18n.html#looking-up-translations). Use this if you don't want a top level i18n key for each controller in your app - instead all keys sit under a "views" parent (and presumably you'd have other top level keys for o…
# config/initializers/translation_helper.rb
module ActionView
# = Action View Translation Helpers
module Helpers
module TranslationHelper
private
def scope_key_by_partial_with_views_prefix(key)
if key.is_a?(String) && key.first == "."
key = scope_key_by_partial_without_views_prefix(key)
"views.#{key}"
else
scope_key_by_partial_without_views_prefix(key)
end
end
alias_method_chain :scope_key_by_partial, :views_prefix
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment