views:

142

answers:

2

Hi,

I'm using rails 2.3.5 with i18n. I's there a way to find all not yet translated locales in all views? Maybe a after_filter in the application controller, but which code I can use for this job?

thanks

A: 

I couldn't find a simple trick to do this, so I did this. First implement a 'before_filter' in your application_controller.rb

before_filter :set_user_language

# set the language, 'zen' is a special URL parameter that makes localizations the use the 't' method visible
def set_user_language
  # turn on 'zen' to see localization by adding 'zen=true' to query string, will stay on until a query with 'zen=false'
  session[:zen] = (session[:zen] || params[:zen] == "true") && params[:zen] != "false"
  I18n.locale = 'en'
end

The above finds 'zen=true' and 'zen=false' in the query string. Then add this method to your application_helper.rb:

def t(*args)
  result = super(*args)
  result = "[#{result}]" if session[:zen] && result.is_a?(String)
  result
end

With this method 'zen=true' makes the 't' method display localized strings in square brackets []. To turn it off enter a query string with 'zen=false'.

JustRonin
+2  A: 

When using the i18n gem (which Rails does), you can specify your own exception handler. Try this code:

# A simple exception handler that behaves like the default exception handler
# but additionally logs missing translations to a given log.
#
module I18n
  class << self
    def missing_translations_logger
      @@missing_translations_logger ||= Logger.new("#{RAILS_ROOT}/log/missing_translations.log")
    end

    def missing_translations_log_handler(exception, locale, key, options)
      if MissingTranslationData === exception
        puts "logging #{exception.message}"
        missing_translations_logger.warn(exception.message)
        return exception.message
      else
        raise exception
      end
    end
  end
end

I18n.exception_handler = :missing_translations_log_handler

(put it for example into RAILS_ROOT/config/initializers/i18n.rb)

Now, whenever you try to translate a key for which you have no translation specified, a warning gets printed into RAILS_ROOT/log/missing_translations.log.

Hope this helps!

severin