2

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.

egze
  • 3,200
  • 23
  • 23

3 Answers3

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

Vassilis
  • 2,801
  • 1
  • 20
  • 16
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.

tigrish
  • 2,488
  • 18
  • 21
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).

Paweł Dyda
  • 18,366
  • 7
  • 57
  • 79