Extract I18n translation keys from rails project

北战南征 提交于 2019-12-12 18:30:37

问题


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

so if I have in the code:

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

It would add to the yml file

homepage:
  header: "Cool website"

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.


回答1:


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>.*?)\"\)

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).




回答2:


If you're using default: everywhere then there is a solution that might work for you :

  • Head over to Locale and create an account and a new project
  • Set up the localeapp gem in your rails project, configure it with the API key etc...
  • By default when you visit a page with a translation call like in your example, it will send the translation up to the Locale server and it will sync it back down to your YAML.
  • Alternatively, if you have good test coverage, you can just configure localeapp to run in the test environment and run your test suite.

Not ideal, and not the primary purpose of Locale (which is to avoid editing YAML files by hand), but it should work in this case.

Full disclosure : I co-founded Locale.




回答3:


For anyone who comes here from Google, there is now a gem for all this kind of stuff.

https://github.com/glebm/i18n-tasks



来源:https://stackoverflow.com/questions/13011410/extract-i18n-translation-keys-from-rails-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!