utils_languages_menu.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # ***** BEGIN GPL LICENSE BLOCK *****
  2. #
  3. # This program is free software; you can redistribute it and/or
  4. # modify it under the terms of the GNU General Public License
  5. # as published by the Free Software Foundation; either version 2
  6. # of the License, or (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program; if not, write to the Free Software Foundation,
  15. # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. #
  17. # ***** END GPL LICENSE BLOCK *****
  18. # <pep8 compliant>
  19. # Update "languages" text file used by Blender at runtime to build translations menu.
  20. import os
  21. OK = 0
  22. MISSING = 1
  23. TOOLOW = 2
  24. FORBIDDEN = 3
  25. FLAG_MESSAGES = {
  26. OK: "",
  27. MISSING: "No translation yet!",
  28. TOOLOW: "Not enough advanced to be included...",
  29. FORBIDDEN: "Explicitly forbidden!",
  30. }
  31. def gen_menu_file(stats, settings):
  32. # Generate languages file used by Blender's i18n system.
  33. # First, match all entries in LANGUAGES to a lang in stats, if possible!
  34. tmp = []
  35. for uid_num, label, uid, in settings.LANGUAGES:
  36. if uid in stats:
  37. if uid in settings.IMPORT_LANGUAGES_SKIP:
  38. tmp.append((stats[uid], uid_num, label, uid, FORBIDDEN))
  39. else:
  40. tmp.append((stats[uid], uid_num, label, uid, OK))
  41. else:
  42. tmp.append((0.0, uid_num, label, uid, MISSING))
  43. stats = tmp
  44. limits = sorted(settings.LANGUAGES_CATEGORIES, key=lambda it: it[0], reverse=True)
  45. idx = 0
  46. stats = sorted(stats, key=lambda it: it[0], reverse=True)
  47. langs_cats = [[] for i in range(len(limits))]
  48. highest_uid = 0
  49. for lvl, uid_num, label, uid, flag in stats:
  50. if lvl < limits[idx][0]:
  51. # Sub-sort languages by iso-codes.
  52. langs_cats[idx].sort(key=lambda it: it[2])
  53. idx += 1
  54. if lvl < settings.IMPORT_MIN_LEVEL and flag == OK:
  55. flag = TOOLOW
  56. langs_cats[idx].append((uid_num, label, uid, flag))
  57. if abs(uid_num) > highest_uid:
  58. highest_uid = abs(uid_num)
  59. # Sub-sort last group of languages by iso-codes!
  60. langs_cats[idx].sort(key=lambda it: it[2])
  61. data_lines = [
  62. "# File used by Blender to know which languages (translations) are available, ",
  63. "# and to generate translation menu.",
  64. "#",
  65. "# File format:",
  66. "# ID:MENULABEL:ISOCODE",
  67. "# ID must be unique, except for 0 value (marks categories for menu).",
  68. "# Line starting with a # are comments!",
  69. "#",
  70. "# Automatically generated by bl_i18n_utils/update_languages_menu.py script.",
  71. "# Highest ID currently in use: {}".format(highest_uid),
  72. ]
  73. for cat, langs_cat in zip(limits, langs_cats):
  74. data_lines.append("#")
  75. # Write "category menu label"...
  76. if langs_cat:
  77. data_lines.append("0:{}:".format(cat[1]))
  78. else:
  79. # Do not write the category if it has no language!
  80. data_lines.append("# Void category! #0:{}:".format(cat[1]))
  81. # ...and all matching language entries!
  82. for uid_num, label, uid, flag in langs_cat:
  83. if flag == OK:
  84. data_lines.append("{}:{}:{}".format(uid_num, label, uid))
  85. else:
  86. # Non-existing, commented entry!
  87. data_lines.append("# {} #{}:{}:{}".format(FLAG_MESSAGES[flag], uid_num, label, uid))
  88. with open(os.path.join(settings.TRUNK_MO_DIR, settings.LANGUAGES_FILE), 'w') as f:
  89. f.write("\n".join(data_lines))
  90. with open(os.path.join(settings.GIT_I18N_ROOT, settings.LANGUAGES_FILE), 'w') as f:
  91. f.write("\n".join(data_lines))