File: creditTranslators.py

package info (click to toggle)
0ad 0.0.23.1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 78,292 kB
  • sloc: cpp: 245,166; ansic: 200,249; python: 13,754; sh: 6,104; perl: 4,620; makefile: 977; xml: 810; java: 533; ruby: 229; erlang: 46; pascal: 30; sql: 21; tcl: 4
file content (137 lines) | stat: -rwxr-xr-x 4,663 bytes parent folder | download | duplicates (2)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env python2
# -*- coding:utf-8 -*-
#
# Copyright (C) 2018 Wildfire Games.
# This file is part of 0 A.D.
#
# 0 A.D. is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# 0 A.D. is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.

"""
This file imports the translators credits located in the public mod GUI files and
runs through .po files to add possible new translators to it.
It only appends new people, so it is possible to manually add names in the credits
file and they won't be overwritten by running this script.

Translatable strings will be extracted from the generated file, so this should be ran
before updateTemplates.py.
"""

from __future__ import absolute_import, division, print_function, unicode_literals

import json, os, glob, re

# Credited languages - Keep in sync with source/tools/dist/remove-incomplete-translations.sh
langs = {
    'ast': 'Asturianu',
    'bg': 'Български',
    'ca': 'Català',
    'cs': 'Čeština',
    'de': 'Deutsch',
    'el': 'Ελληνικά',
    'en_GB': 'English (UK)',
    'es': 'Español',
    'eu': 'Euskara',
    'fr': 'Français',
    'gd': 'Gàidhlig',
    'gl': 'Galego',
    'hu': 'Magyar',
    'id': 'Bahasa Indonesia',
    'it': 'Italiano',
    'ms': 'Bahasa Melayu',
    'nb': 'Norsk bokmål',
    'nl': 'Nederlands',
    'pl': 'Polski',
    'pt_BR': 'Português (Brasil)',
    'pt_PT': 'Português (Portugal)',
    'ru': 'Русский',
    'sk': 'Slovenčina',
    'sv': 'Svenska',
    'tr': 'Türkçe',
    'uk': 'Українська'}

root = '../../../'

poLocations = [
    'binaries/data/l10n/',
    'binaries/data/mods/public/l10n/',
    'binaries/data/mods/mod/l10n/']

creditsLocation = 'binaries/data/mods/public/gui/credits/texts/translators.json'

# Load JSON data
creditsFile = open(root + creditsLocation)
JSONData = json.load(creditsFile)
creditsFile.close()

# This dictionnary will hold creditors lists for each language, indexed by code
langsLists = {}

# Create the new JSON data
newJSONData = {'Title': 'Translators', 'Content': []}

# First get the already existing lists. If they correspond with some of the credited languages,
# add them to the new data after processing, else add them immediately.
# NB: All of this is quite inefficient
for element in JSONData['Content']:
    if 'LangName' not in element or element['LangName'] not in langs.values():
        newJSONData['Content'].append(element)
        continue

    for (langCode, langName) in langs.items():
        if element['LangName'] == langName:
            langsLists[langCode] = element['List']
            break

# Now actually go through the list of languages and search the .po files for people

# Prepare some regexes
commentMatch = re.compile('#.*')
translatorMatch = re.compile('# ([\w\s]*)(?: <.*>)?, [0-9-]', re.UNICODE)

# Search
for lang in langs.keys():
    if lang not in langsLists.keys():
        langsLists[lang] = []

    for location in poLocations:
        files = glob.glob(root + location + lang + '.*.po')
        for file in files:
            poFile = open(file.replace('\\', '/'))
            reached = False
            for line in poFile:
                line = line.decode('utf8')
                if reached:
                    if not commentMatch.match(line):
                        break
                    m = translatorMatch.match(line)
                    if m:
                        langsLists[lang].append(m.group(1))
                if line.strip() == '# Translators:':
                    reached = True
            poFile.close()

    # Sort and remove duplicates
    # Sorting should ignore case to have a neat credits list
    langsLists[lang] = sorted(set(langsLists[lang]), cmp=lambda x,y: cmp(x.lower(), y.lower()))

# Now insert the new data into the new JSON file
for (langCode, langList) in sorted(langsLists.items()):
    newJSONData['Content'].append({'LangName': langs[langCode], 'List': []})
    for name in langList:
        newJSONData['Content'][-1]['List'].append({'name': name})

# Save the JSON data to the credits file
creditsFile = open(root + creditsLocation, 'w')
json.dump(newJSONData, creditsFile, indent=4)
creditsFile.close()