2016-04-22 48 views
0

我有一个包含I18n翻译密钥的.yml文件的Rails项目。我想要创建一个rake任务(或类似的),它提取添加键的路径(git认为已添加的行)。将结果写入终端或文件并不重要。使用Ruby和git从.yml文件中提取新密钥

例.yml文件:rake任务的

en: 
    index: # <-- new key 
    greeting: "Hello world!" # <-- new key 
    show: 
    title: "Old text" 
    body: "This is a text" # <-- new key 

例输出/结果:

en.index.greeting 
en.show.body 

这在某种程度上可能吗?谢谢!

+0

是的,这是绝对有可能的。 – mudasobwa

回答

1

是的,你可以。此功能将打印所有I18n按键

def print_translations(prefix, x) 
    if x.is_a? Hash 
    prefix += "." if prefix.present? 
    x.each do |key, value| 
     print_translations(prefix + key.to_s, value) 
    end 
    else 
    puts prefix 
    end 
end 

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

非常感谢您的回答。这是否打印键或与它们相关的字符串? – Linus

+0

你的意思是像'en.index.greeting:“Hello world!”'这样的键值吗? –

+0

是的 - 我不确定您的答案是否呈现密钥的值或密钥本身? (我需要的是钥匙,而不是价值。) – Linus

相关问题