Search code examples
ruby-on-railsrubyinternationalizationlocalerails-i18n

Find all i18n tags in project and generate yml


I recently started a project that has a REALLY messed up locales file. I guess about 50% of the 10.000+ lines are locales from landingpages they're not using anymore.

Is there a way to go over all the views and find the i18n strings

 t('profile.button1')

They're about to hire translators to translate their locale files but if I give them the file like this they will be stuck translating 5000 lines that will never be used anyway.


Solution

  • I wrote a quick & dirty program hope help you (me too)

    v1 = ARGV[0]
     file = File.open("keys.txt", "w")
     File.open(v1) do |f|
       f.each_line do |line|
         if line.include? "I18n"
           file.write(line[/I18n.t \((?:\'|\")(.*?)(?:\'|\")/m, 1])
         end
       end
     end
    

    call it whit the file you want to parse as argument and it should extract all I18n keys in a file called keys.txt. Maybe you should adapt to your program, but I tested on my erb files and works