[英]find untranslated locales in rails
I'm using rails 2.3.5 with i18n. 我正在i18n上使用Rails 2.3.5。 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? 也许是应用程序控制器中的after_filter,但是我可以为此工作使用哪些代码?
thanks 谢谢
When using the i18n gem (which Rails does), you can specify your own exception handler. 使用i18n gem(Rails可以使用)时,可以指定自己的异常处理程序。 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 # use MissingTranslation in Rails 3.x !!!
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) (例如,将其放入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. 现在,每当您尝试翻译未指定翻译的键时,都会在RAILS_ROOT / log / missing_translations.log中打印警告。
Hope this helps! 希望这可以帮助!
I couldn't find a simple trick to do this, so I did this. 我找不到执行此操作的简单技巧,所以我做到了。 First implement a 'before_filter' in your application_controller.rb 首先在application_controller.rb中实现一个“ before_filter”
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. 上面在查询字符串中找到“ zen = true”和“ zen = false”。 Then add this method to your application_helper.rb: 然后将此方法添加到您的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 []. 使用此方法,“ zen = true”使“ t”方法在方括号[]中显示本地化的字符串。 To turn it off enter a query string with 'zen=false'. 要关闭它,请输入带有“ zen = false”的查询字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.