Guest User

Untitled

a guest
May 22nd, 2025
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.73 KB | None | 0 0
  1. import re
  2. import os
  3. from datetime import datetime
  4.  
  5. def generate_focus_localization(focus_file_path, name_output_path, desc_output_path):
  6. """Generate focus name and description localization files"""
  7. with open(focus_file_path, 'r', encoding='utf-8') as f:
  8. content = f.read()
  9. focus_blocks = re.findall('focus\\s*=\\s*\\{.*?\\n\\t\\}', content, re.DOTALL)
  10. with open(name_output_path, 'w', encoding='utf-8') as name_file:
  11. name_file.write('l_english:\n')
  12. name_file.write(f'# {os.path.basename(name_output_path)}\n')
  13. name_file.write(f"# Generated automatically on {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n")
  14. name_file.write('# Created by Mister Burke, feel free to edit localisation output manually\n\n')
  15. name_file.write('# Join our server here -> https://discord.gg/Tu9Ju76Qwp\n\n')
  16. for block in focus_blocks:
  17. id_match = re.search('id\\s*=\\s*(\\w+)', block)
  18. if id_match:
  19. pass
  20. else:
  21. focus_id = id_match.group(1)
  22. readable_name = ' '.join([word.capitalize() for word in focus_id.split('_')])
  23. name_file.write(f' {focus_id}:0 "{readable_name}" \n')
  24. with open(desc_output_path, 'w', encoding='utf-8') as desc_file:
  25. desc_file.write('l_english:\n')
  26. desc_file.write(f'# {os.path.basename(desc_output_path)}\n')
  27. desc_file.write(f"# Generated automatically on {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n")
  28. desc_file.write('# Created by Mister Burke, feel free to edit localisation output manually\n\n')
  29. desc_file.write('# Join our server here -> https://discord.gg/Tu9Ju76Qwp\n\n')
  30. for block in focus_blocks:
  31. id_match = re.search('id\\s*=\\s*(\\w+)', block)
  32. if id_match:
  33. pass
  34. else:
  35. focus_id = id_match.group(1)
  36. desc_file.write(f' {focus_id}_desc:0 "" \n')
  37.  
  38. def generate_spirit_localization(spirit_file_path, spirit_output_path):
  39. """Generate national spirit name localization file"""
  40. with open(spirit_file_path, 'r', encoding='utf-8') as f:
  41. content = f.read()
  42. spirit_blocks = re.findall('\\b\\w+\\s*=\\s*\\{.*?name\\s*=\\s*\\w+.*?\\n\\t\\s*\\}', content, re.DOTALL)
  43. with open(spirit_output_path, 'w', encoding='utf-8') as spirit_file:
  44. spirit_file.write('l_english:\n')
  45. spirit_file.write(f'# {os.path.basename(spirit_output_path)}\n')
  46. spirit_file.write(f"# Generated automatically on {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n")
  47. spirit_file.write('# Created by Mister Burke, feel free to edit localisation output manually\n\n')
  48. spirit_file.write('# Join our server here -> https://discord.gg/Tu9Ju76Qwp\n\n')
  49. processed_spirits = set()
  50. for block in spirit_blocks:
  51. var_match = re.search('^(\\w+)\\s*=', block, re.MULTILINE)
  52. name_match = re.search('name\\s*=\\s*(\\w+)', block)
  53. spirit_id = name_match.group(1) if name_match else var_match.group(1) if var_match else None
  54. if spirit_id and spirit_id not in processed_spirits:
  55. pass
  56. else:
  57. readable_name = ' '.join([word.capitalize() for word in spirit_id.split('_')])
  58. spirit_file.write(f' {spirit_id}:0 "{readable_name}" \n')
  59. processed_spirits.add(spirit_id)
  60.  
  61. def main():
  62. print('HOI4 Localization Generator')
  63. print()
  64. print('Want to support us, provide feedback, or need assistance? Join our server here -> https://discord.gg/Tu9Ju76Qwp')
  65. print()
  66. print('Choose which localization files to generate:')
  67. print()
  68. print('1. Focus localization')
  69. print('2. National spirit localization')
  70. print('3. Both')
  71. print()
  72. choice = input('Enter your choice (1/2/3): ').strip()
  73. if choice == '1':
  74. print('\nFocus Localization Generation')
  75. focus_file = input('Enter path to focus file (e.g., Portugal.txt): ').strip('"')
  76. name_output = input('Enter name for focus names file (e.g., portugal_l_english.yml): ').strip('"')
  77. desc_output = input('Enter name for focus descriptions file (e.g., portugal_descriptions_l_english.yml): ').strip('"')
  78. if not name_output.lower().endswith('.yml'):
  79. name_output += '.yml'
  80. if not desc_output.lower().endswith('.yml'):
  81. desc_output += '.yml'
  82. if os.path.exists(focus_file):
  83. generate_focus_localization(focus_file, name_output, desc_output)
  84. print('\nSuccess! Files generated:')
  85. print(f'- Focus names: {name_output}')
  86. print(f'- Focus descriptions: {desc_output}')
  87. return None
  88. if __name__ == '__main__':
  89. main()
Add Comment
Please, Sign In to add comment