File: utils_languages_menu.py

package info (click to toggle)
blender 4.3.2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 309,564 kB
  • sloc: cpp: 2,385,210; python: 330,236; ansic: 280,972; xml: 2,446; sh: 972; javascript: 317; makefile: 170
file content (78 lines) | stat: -rwxr-xr-x 3,057 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# SPDX-FileCopyrightText: 2013-2023 Blender Authors
#
# SPDX-License-Identifier: GPL-2.0-or-later

# Update "languages" text file used by Blender at runtime to build translations menu.


OK = 0
MISSING = 1
TOOLOW = 2
SKIPPED = 3
FLAG_MESSAGES = {
    OK: "",
    MISSING: "No translation yet.",
    TOOLOW: "Not complete enough to be included.",
    SKIPPED: "Skipped (see IMPORT_LANGUAGES_SKIP in settings.py).",
}


def gen_menu_file(stats, settings):
    # Generate languages file content used by Blender's i18n system.
    # First, match all entries in LANGUAGES to a `lang` in stats, if possible!
    # Returns a iterable of text lines.
    tmp = []
    for uid_num, label, uid in settings.LANGUAGES:
        if uid in stats:
            if uid in settings.IMPORT_LANGUAGES_SKIP:
                tmp.append((stats[uid], uid_num, label, uid, SKIPPED))
            else:
                tmp.append((stats[uid], uid_num, label, uid, OK))
        else:
            tmp.append((0.0, uid_num, label, uid, MISSING))
    stats = tmp
    limits = sorted(settings.LANGUAGES_CATEGORIES, key=lambda it: it[0], reverse=True)
    idx = 0
    stats = sorted(stats, key=lambda it: it[0], reverse=True)
    langs_cats = [[] for i in range(len(limits))]
    highest_uid = 0
    for lvl, uid_num, label, uid, flag in stats:
        if lvl < limits[idx][0]:
            # Sub-sort languages by ISO-codes.
            langs_cats[idx].sort(key=lambda it: it[2])
            idx += 1
        if lvl < settings.IMPORT_MIN_LEVEL and flag == OK:
            flag = TOOLOW
        langs_cats[idx].append((uid_num, label, uid, flag))
        if abs(uid_num) > highest_uid:
            highest_uid = abs(uid_num)
    # Sub-sort last group of languages by ISO-codes!
    langs_cats[idx].sort(key=lambda it: it[2])
    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!",
        "#",
        "# Automatically generated by bl_i18n_utils/update_languages_menu.py script.",
        "# Highest ID currently in use: {}".format(highest_uid),
    ]
    for cat, langs_cat in zip(limits, langs_cats):
        data_lines.append("#")
        # Write "category menu label"...
        if langs_cat:
            data_lines.append("0:{}:".format(cat[1]))
        else:
            # Do not write the category if it has no language!
            data_lines.append("# Void category! #0:{}:".format(cat[1]))
        # ...and all matching language entries!
        for uid_num, label, uid, flag in langs_cat:
            if flag == OK:
                data_lines.append("{}:{}:{}".format(uid_num, label, uid))
            else:
                # Non-existing, commented entry!
                data_lines.append("# {} #{}:{}:{}".format(FLAG_MESSAGES[flag], uid_num, label, uid))
    return data_lines