从Rails项目中提取I18n翻译密钥 [英] Extract I18n translation keys from rails project

查看:60
本文介绍了从Rails项目中提取I18n翻译密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一些gem或工具可用来解析我的代码并将I18n密钥提取到yml文件?

Is there some gem or tool that I can use to parse my code and extract I18n keys to yml file?

所以如果我在代码中:

<%= t("homepage.header", default: "Cool website") %>

它将添加到yml文件

homepage:
  header: "Cool website"

GetText可以做到,但是对于我的项目,我们现在使用默认的rails设置.当我需要新的翻译密钥时,必须手动更新yml文件使我感到很烦.

GetText can do it, but for my project now we are using default rails setup. It bugs me a lot that I have to manually update yml files, when I need new translation keys.

推荐答案

我还没有听说过Ruby on Rails这样的工具,但是我很确定它存在.另一方面,您可以自己创建一个-可以使用相对简单的正则表达式来完成,例如:

I haven't heard of such tool for Ruby on Rails, but I am pretty sure it exists. On the other hand, you can create one yourself - it could be done using relatively simple Regular Expression like:

\s+t\(\"(?<key>.*?)\"\s*,\s*default:\s*\"(?<value>.*?)\"\)

然后,您要做的就是访问命名的组(上面示例中的键和值)并将其写到YAML文件中.根据您要用来捕获此正则表达式的编程语言,可能要在正则表达式前添加(?s)或(?-s),以允许将行尾视为空白(即多行声明)

All you have to do then, is to access the named groups (key and value in the example above) and write it down to YAML file. Depending on the programming language you want to use to capture this regexp you might want to add (?s) or (?-s) in front of the regexp to make allow for treating line ending as white space (i.e. multi-line declarations).

这篇关于从Rails项目中提取I18n翻译密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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