Skip to content

Instantly share code, notes, and snippets.

@dlupu
Forked from sunny/application_controller.rb
Created October 5, 2020 20:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dlupu/612f143c9360ebca3b5a13f1e035cd7f to your computer and use it in GitHub Desktop.
Save dlupu/612f143c9360ebca3b5a13f1e035cd7f to your computer and use it in GitHub Desktop.
Rails helper so that "?_locale_keys=1" in URL shows locale keys
class ApplicationController < ActionController::Base
include I18nHelper
end
# frozen_string_literal: true
module I18nHelper
# Print out the keys if you add ?_locale_keys=1 in params
def t(key, options = {})
key = scope_key_by_partial(key) if respond_to?(:scope_key_by_partial)
# Make sure params isn't triggered in Mailers by looking for request
if (respond_to?(:request) && request && params[:_locale_keys]) || \
(@mail_params && @mail_params[:_locale_keys])
if options.any?
"#{key} (#{options.map { |k, v| "#{k}: #{v.inspect}" }.join(',')})"
else
key
end
else
translate_with_default_scope(key, conf.site, options)
end
end
private
def translate_with_default_scope(key, scope, options = {})
default_lookup = proc { I18n.translate(key, options) }
I18n.translate(key, options.merge(scope: scope, default: default_lookup))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment