列出带有完整键的Rails I18n字符串 [英] List Rails I18n strings with full keys

查看:86
本文介绍了列出带有完整键的Rails I18n字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够生成所有I18n键的完整列表以及包含完整键的语言环境的值.换句话说,如果我有这些文件:

I'd like to be able to generate a complete list of all the I18n keys and values for a locale including the full keys. In other words if I have these files:

config/locales/en.yml

en:
  greeting:
    polite: "Good evening"
    informal: "What's up?"

config/locales/second.en.yml

en:
  farewell:
    polite: "Goodbye"
    informal: "Later"

我想要以下输出:

greeting.polite: "Good evening"
greeting.informal: "What's up?"
farewell.polite: "Goodbye"
farewell.informal: "Later"

我该怎么做?

推荐答案

尼克·戈尔比科夫(Nick Gorbikoff)的回答是一个开始,但没有发出我所希望的输出,如问题所述.我最终在下面编写了自己的脚本get_translations来完成它.

Nick Gorbikoff's answer was a start but did not emit the output I wanted as described in the question. I ended up writing my own script get_translations to do it, below.

#!/usr/bin/env ruby

require 'pp'
require './config/environment.rb'

def print_translations(prefix, x)
  if x.is_a? Hash
    if (not prefix.empty?)
        prefix += "."
    end
    x.each {|key, value|
      print_translations(prefix + key.to_s, value)
    }
  else
      print prefix + ": "
      PP.singleline_pp x
      puts ""
  end
end

I18n.translate(:foo)
translations_hash = I18n.backend.send(:translations)
print_translations("", translations_hash)

这篇关于列出带有完整键的Rails I18n字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆