KoBeWi

Godot .pot generator

Sep 26th, 2018
884
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.51 KB | None | 0 0
  1. ROOT_DIRECTORY = ".."
  2. OUTPUT_PATH = "./en.pot"
  3. EXCLUDE_DIRECTORIES = [".import", ".git"]
  4. EXCLUDE_STRINGS = ["", "..."]
  5. FILE_HEADER = '
  6. msgid ""
  7. msgstr ""
  8. "MIME-Version: 1.0\n"
  9. "Content-Type: text/plain; charset=UTF-8\n"
  10. "Content-Transfer-Encoding: 8bit\n"
  11. '
  12.  
  13. def entry_list(dir) #purely out of convenience
  14.     Dir.entries(dir) - [".", ".."]
  15. end
  16.  
  17. dir_stack = [ROOT_DIRECTORY]
  18. visited_dirs = []
  19. strings = []
  20.  
  21. while !dir_stack.empty?
  22.     dir = dir_stack.pop
  23.     entries = entry_list(dir)
  24.  
  25.     entries.each do |entry|
  26.         next if EXCLUDE_DIRECTORIES.include?(entry)
  27.         full_path = File.join(dir, entry)
  28.        
  29.         if Dir.exist?(full_path) and !visited_dirs.include?(full_path)
  30.             visited_dirs << full_path
  31.             dir_stack << full_path
  32.             next
  33.         end
  34.  
  35.         if File.extname(entry) == ".gd"
  36.             File.readlines(full_path).each do |line|
  37.                 strings.concat line.scan(/[^s]tr\("([^()]*)"\)/).flatten
  38.             end
  39.         elsif File.extname(entry) == ".tscn"
  40.             File.readlines(full_path).each do |line|
  41.                 strings.concat line.scan(/text = "([^()]*)"/).flatten
  42.                 strings.concat line.scan(/hint_tooltip = "([^()]*)"/).flatten
  43.             end
  44.         end
  45.     end
  46. end
  47.  
  48. output = FILE_HEADER
  49.  
  50. strings.uniq.reject{|string| EXCLUDE_STRINGS.include?(string)}.each do |string|
  51.     output.concat('
  52. msgid "' + string + '"
  53. msgstr ""
  54. ')
  55. end
  56.  
  57. file = File.new(OUTPUT_PATH, "w")
  58. file.print(output)
  59. file.close
Advertisement
Add Comment
Please, Sign In to add comment