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
19# <pep8 compliant>
20
21# Update "languages" text file used by Blender at runtime to build translations menu.
22
23
24import os
25
26
27OK = 0
28MISSING = 1
29TOOLOW = 2
30FORBIDDEN = 3
31FLAG_MESSAGES = {
32    OK: "",
33    MISSING: "No translation yet!",
34    TOOLOW: "Not enough advanced to be included...",
35    FORBIDDEN: "Explicitly forbidden!",
36}
37
38
39def gen_menu_file(stats, settings):
40    # Generate languages file used by Blender's i18n system.
41    # First, match all entries in LANGUAGES to a lang in stats, if possible!
42    tmp = []
43    for uid_num, label, uid, in settings.LANGUAGES:
44        if uid in stats:
45            if uid in settings.IMPORT_LANGUAGES_SKIP:
46                tmp.append((stats[uid], uid_num, label, uid, FORBIDDEN))
47            else:
48                tmp.append((stats[uid], uid_num, label, uid, OK))
49        else:
50            tmp.append((0.0, uid_num, label, uid, MISSING))
51    stats = tmp
52    limits = sorted(settings.LANGUAGES_CATEGORIES, key=lambda it: it[0], reverse=True)
53    idx = 0
54    stats = sorted(stats, key=lambda it: it[0], reverse=True)
55    langs_cats = [[] for i in range(len(limits))]
56    highest_uid = 0
57    for lvl, uid_num, label, uid, flag in stats:
58        if lvl < limits[idx][0]:
59            # Sub-sort languages by iso-codes.
60            langs_cats[idx].sort(key=lambda it: it[2])
61            idx += 1
62        if lvl < settings.IMPORT_MIN_LEVEL and flag == OK:
63            flag = TOOLOW
64        langs_cats[idx].append((uid_num, label, uid, flag))
65        if abs(uid_num) > highest_uid:
66            highest_uid = abs(uid_num)
67    # Sub-sort last group of languages by iso-codes!
68    langs_cats[idx].sort(key=lambda it: it[2])
69    data_lines = [
70        "# File used by Blender to know which languages (translations) are available, ",
71        "# and to generate translation menu.",
72        "#",
73        "# File format:",
74        "# ID:MENULABEL:ISOCODE",
75        "# ID must be unique, except for 0 value (marks categories for menu).",
76        "# Line starting with a # are comments!",
77        "#",
78        "# Automatically generated by bl_i18n_utils/update_languages_menu.py script.",
79        "# Highest ID currently in use: {}".format(highest_uid),
80    ]
81    for cat, langs_cat in zip(limits, langs_cats):
82        data_lines.append("#")
83        # Write "category menu label"...
84        if langs_cat:
85            data_lines.append("0:{}:".format(cat[1]))
86        else:
87            # Do not write the category if it has no language!
88            data_lines.append("# Void category! #0:{}:".format(cat[1]))
89        # ...and all matching language entries!
90        for uid_num, label, uid, flag in langs_cat:
91            if flag == OK:
92                data_lines.append("{}:{}:{}".format(uid_num, label, uid))
93            else:
94                # Non-existing, commented entry!
95                data_lines.append("# {} #{}:{}:{}".format(FLAG_MESSAGES[flag], uid_num, label, uid))
96    with open(os.path.join(settings.TRUNK_MO_DIR, settings.LANGUAGES_FILE), 'w', encoding="utf8") as f:
97        f.write("\n".join(data_lines))
98    with open(os.path.join(settings.GIT_I18N_ROOT, settings.LANGUAGES_FILE), 'w', encoding="utf8") as f:
99        f.write("\n".join(data_lines))
100