1

Is there a way to enable Rails to infer the english translations from the key names automatically?

Currently I have to explicitly write the translation, because otherwise it would just output en, devise.confirmations.new.resend_confirmation_instructions in the view.

What I currently have to write in the config/locales/en.yml file:

en:
  devise:
    confirmations:
      new:
        resend_confirmation_instructions: "Resend confirmation instructions."

What I would like to have:

en:
  devise:
    confirmations:
      new:
        resend_confirmation_instructions: 

And the view should just output "Resend confirmation instruction." which is inferred directly from that key.

Magne
  • 16,401
  • 10
  • 68
  • 88
  • 1
    There's no way to do that (that I know of) because that exactly counters the point of translations. In production however Rails will display "Resend Confirmation Instructions" if translation is missing – Mike Szyndel Feb 11 '15 at 15:25
  • Ah, will it do that in production? I didn't know that. Btw, getting the english text from the english translation key isn't exactly a translation. It's just a convenient way to avoid duplicate work when your codebase and translation keys are in english anyway. – Magne Feb 11 '15 at 17:07
  • For that you can configure fallback languages. Check here: http://stackoverflow.com/a/7886246/488195 – dgilperez Feb 11 '15 at 17:13
  • @dgilperez Fallbacks are just for falling back to english translations if other translations doesn't exist. It's not for inferring english translations from the translation keys... – Magne Feb 12 '15 at 14:20
  • hey @Magne what approach did you eventually choose for this? – dgilperez Feb 16 '15 at 20:06
  • @MichalSzyndel Actually production will output `en, devise.confirmations.new.resend_confirmation_instructions` if the translation is missing, just like development will.. – Magne Feb 17 '15 at 14:38
  • @dgilperez Haven't really solved it yet. :( Currently I'm explicitly writing the english translations. – Magne Feb 17 '15 at 14:39
  • @dgilperez Will try out your exception handler method, to see if it works as intended. – Magne Feb 17 '15 at 14:46

1 Answers1

2

You can always write your own exception handler:

#config/initializers/i18n.rb

module I18n
  class MissingTranslationExceptionHandler < ExceptionHandler
    def call(exception, locale, key, options)
      if exception.is_a?(MissingTranslation)
        key.split('.').last.try(:humanize)
      else
        super
      end
    end
  end
end

I18n.exception_handler = I18n::MissingTranslationExceptionHandler.new

You can read more on the guides.

dgilperez
  • 10,716
  • 8
  • 68
  • 96