2023-08-16 00:20:26 +10:00
|
|
|
# SPDX-FileCopyrightText: 2013-2023 Blender Authors
|
2023-06-15 13:09:04 +10:00
|
|
|
#
|
2022-02-11 09:07:11 +11:00
|
|
|
# SPDX-License-Identifier: GPL-2.0-or-later
|
2013-02-24 08:50:55 +00:00
|
|
|
|
|
|
|
# Update "languages" text file used by Blender at runtime to build translations menu.
|
|
|
|
|
|
|
|
|
|
|
|
OK = 0
|
|
|
|
MISSING = 1
|
|
|
|
TOOLOW = 2
|
2022-08-29 11:05:03 +02:00
|
|
|
SKIPPED = 3
|
2013-02-24 08:50:55 +00:00
|
|
|
FLAG_MESSAGES = {
|
|
|
|
OK: "",
|
2022-08-29 11:05:03 +02:00
|
|
|
MISSING: "No translation yet.",
|
|
|
|
TOOLOW: "Not complete enough to be included.",
|
|
|
|
SKIPPED: "Skipped (see IMPORT_LANGUAGES_SKIP in settings.py).",
|
2013-02-24 08:50:55 +00:00
|
|
|
}
|
|
|
|
|
2013-06-27 03:05:19 +00:00
|
|
|
|
2013-02-24 08:50:55 +00:00
|
|
|
def gen_menu_file(stats, settings):
|
2023-07-18 18:05:14 +02:00
|
|
|
# Generate languages file content used by Blender's i18n system.
|
2023-09-05 10:49:20 +10:00
|
|
|
# First, match all entries in LANGUAGES to a `lang` in stats, if possible!
|
2023-07-18 18:05:14 +02:00
|
|
|
# Returns a iterable of text lines.
|
2013-02-24 08:50:55 +00:00
|
|
|
tmp = []
|
2020-12-04 14:55:23 +01:00
|
|
|
for uid_num, label, uid in settings.LANGUAGES:
|
2013-02-24 08:50:55 +00:00
|
|
|
if uid in stats:
|
|
|
|
if uid in settings.IMPORT_LANGUAGES_SKIP:
|
2022-08-29 11:05:03 +02:00
|
|
|
tmp.append((stats[uid], uid_num, label, uid, SKIPPED))
|
2013-02-24 08:50:55 +00:00
|
|
|
else:
|
|
|
|
tmp.append((stats[uid], uid_num, label, uid, OK))
|
|
|
|
else:
|
|
|
|
tmp.append((0.0, uid_num, label, uid, MISSING))
|
|
|
|
stats = tmp
|
|
|
|
stats = sorted(stats, key=lambda it: it[0], reverse=True)
|
2025-06-11 13:11:40 +02:00
|
|
|
langs = []
|
2013-02-24 08:50:55 +00:00
|
|
|
highest_uid = 0
|
|
|
|
for lvl, uid_num, label, uid, flag in stats:
|
|
|
|
if lvl < settings.IMPORT_MIN_LEVEL and flag == OK:
|
|
|
|
flag = TOOLOW
|
2025-06-11 13:11:40 +02:00
|
|
|
if uid_num == 0:
|
|
|
|
# Insert default language (Automatic) at index 0, after sorting.
|
|
|
|
default_lang = (uid_num, label, uid, flag, lvl)
|
|
|
|
continue
|
|
|
|
langs.append((uid_num, label, uid, flag, lvl))
|
2013-02-24 08:50:55 +00:00
|
|
|
if abs(uid_num) > highest_uid:
|
|
|
|
highest_uid = abs(uid_num)
|
2025-06-11 13:11:40 +02:00
|
|
|
# Sort languages by name.
|
|
|
|
langs.sort(key=lambda it: it[1])
|
|
|
|
langs.insert(0, default_lang)
|
2013-02-24 08:50:55 +00:00
|
|
|
data_lines = [
|
|
|
|
"# File used by Blender to know which languages (translations) are available, ",
|
|
|
|
"# and to generate translation menu.",
|
|
|
|
"#",
|
|
|
|
"# File format:",
|
|
|
|
"# ID:MENULABEL:ISOCODE",
|
|
|
|
"# ID must be unique, except for 0 value (marks categories for menu).",
|
|
|
|
"# Line starting with a # are comments!",
|
|
|
|
"#",
|
2025-06-11 13:11:40 +02:00
|
|
|
"# Automatically generated by bl_i18n_utils/utils_languages_menu.py script.",
|
2013-02-24 08:50:55 +00:00
|
|
|
"# Highest ID currently in use: {}".format(highest_uid),
|
|
|
|
]
|
2025-06-11 13:11:40 +02:00
|
|
|
for uid_num, label, uid, flag, lvl in langs:
|
|
|
|
if flag == OK:
|
|
|
|
data_lines.append("{}:{}:{}:{}%".format(uid_num, label, uid, round(lvl * 100)))
|
2013-02-24 08:50:55 +00:00
|
|
|
else:
|
2025-06-11 13:11:40 +02:00
|
|
|
# Non-existing, commented entry!
|
|
|
|
data_lines.append("# {} #{}:{}:{}:{}%".format(FLAG_MESSAGES[flag], uid_num, label, uid, round(lvl * 100)))
|
2023-07-18 18:05:14 +02:00
|
|
|
return data_lines
|