Merge 'remotes/trunk'
[0ad.git] / source / tools / i18n / creditTranslators.py
blob8501bd4195ab3f92e20e8ced9cdaecc23c5377df
1 #!/usr/bin/env python3
3 # Copyright (C) 2022 Wildfire Games.
4 # This file is part of 0 A.D.
6 # 0 A.D. is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 2 of the License, or
9 # (at your option) any later version.
11 # 0 A.D. is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
19 """
20 This file updates the translators credits located in the public mod GUI files, using
21 translators names from the .po files.
23 If translators change their names on Transifex, the script will remove the old names.
24 TODO: It should be possible to add people in the list manually, and protect them against
25 automatic deletion. This has not been needed so far. A possibility would be to add an
26 optional boolean entry to the dictionary containing the name.
28 Translatable strings will be extracted from the generated file, so this should be run
29 once before updateTemplates.py.
30 """
32 import json, os, glob, re
34 from i18n_helper import l10nFolderName, transifexClientFolder, projectRootDirectory
36 # We credit everyone that helps translating even if the translations don't
37 # make it into the game.
38 # Note: Needs to be edited manually when new languages are added on Transifex.
39 langs = {
40 'af': 'Afrikaans',
41 'ar': 'الدارجة (Arabic)',
42 'ast': 'Asturianu',
43 'az': 'Azərbaycan dili',
44 'bar': 'Bairisch',
45 'be': 'Беларуская мова (Belarusian)',
46 'bg': 'Български (Bulgarian)',
47 'bn': 'বাংলা (Bengali)',
48 'br': 'Brezhoneg',
49 'ca': 'Català',
50 'cs': 'Čeština ',
51 'cy': 'Cymraeg',
52 'da': 'Dansk',
53 'de': 'Deutsch',
54 'el': 'Ελληνικά (Greek)',
55 'en_GB': 'English (United Kingdom)',
56 'eo': 'Esperanto',
57 'es': 'Español',
58 'es_AR': 'Español (Argentina)',
59 'es_CL': 'Español (Chile)',
60 'es_MX': 'Español (Mexico)',
61 'et': 'Eesti keel',
62 'eu': 'Euskara',
63 'fa': 'فارسی (Farsi)',
64 'fi': 'Suomi',
65 'fr': 'Français',
66 'fr_CA': 'Français (Canada)',
67 'frp': 'Franco-Provençal (Arpitan)',
68 'ga': 'Gaeilge',
69 'gd': 'Gàidhlig',
70 'gl': 'Galego',
71 'he': 'עברית (Hebrew)',
72 'hi': 'हिन्दी (Hindi)',
73 'hr': 'Croatian',
74 'hu': 'Magyar',
75 'hy': 'Հայերէն (Armenian)',
76 'id': 'Bahasa Indonesia',
77 'it': 'Italiano',
78 'ja': '日本語 (Japanese)',
79 'jbo': 'Lojban',
80 'ka': 'ქართული ენა (Georgian)',
81 'ko': '한국어 (Korean)',
82 'krl': 'Karjalan kieli',
83 'ku': 'کوردی (Kurdish)',
84 'la': 'Latin',
85 'lt': 'Lietuvių kalba',
86 'lv': 'Latviešu valoda',
87 'mk': 'македонски (Macedonian)',
88 'ml': 'മലയാളം (Malayalam)',
89 'mr': 'मराठी (Marathi)',
90 'ms': 'بهاس ملايو (Malay)',
91 'nb': 'Norsk Bokmål',
92 'nl': 'Nederlands',
93 'pl': 'Polski',
94 'pt_BR': 'Português (Brazil)',
95 'pt_PT': 'Português (Portugal)',
96 'ro': 'Românește',
97 'ru': 'Русский язык (Russian)',
98 'sk': 'Slovenčina',
99 'sl': 'Slovenščina',
100 'sq': 'Shqip',
101 'sr': 'Cрпски (Serbian)',
102 'sv': 'Svenska',
103 'szl': 'ślōnskŏ gŏdka',
104 'ta_IN': 'தமிழ் (India)',
105 'te': 'తెలుగు (Telugu)',
106 'th': 'ภาษาไทย (Thai)',
107 'tl': 'Tagalog',
108 'tr': 'Türkçe (Turkish)',
109 'uk': 'Українська (Ukrainian)',
110 'uz': 'Ўзбек тили (Uzbek)',
111 'vi': 'Tiếng Việt (Vietnamese)',
112 'zh': '中文, 汉语, 漢語 (Chinese)',
113 'zh_TW': '臺灣話 Chinese (Taiwan)'}
115 poLocations = []
116 for root, folders, filenames in os.walk(projectRootDirectory):
117 for folder in folders:
118 if folder == l10nFolderName:
119 if os.path.exists(os.path.join(root, folder, transifexClientFolder)):
120 poLocations.append(os.path.join(root, folder))
122 creditsLocation = os.path.join(projectRootDirectory, 'binaries', 'data', 'mods', 'public', 'gui', 'credits', 'texts', 'translators.json')
124 # This dictionnary will hold creditors lists for each language, indexed by code
125 langsLists = {}
127 # Create the new JSON data
128 newJSONData = {'Title': 'Translators', 'Content': []}
130 # Now go through the list of languages and search the .po files for people
132 # Prepare some regexes
133 translatorMatch = re.compile('# (.*)')
134 deletedUsernameMatch = re.compile('[0-9a-f]{32}')
136 # Search
137 for lang in langs.keys():
138 if lang not in langsLists.keys():
139 langsLists[lang] = []
141 for location in poLocations:
142 files = glob.glob(os.path.join(location, lang + '.*.po'))
143 for file in files:
144 poFile = open(file.replace('\\', '/'), encoding='utf-8')
145 reached = False
146 for line in poFile:
147 if reached:
148 m = translatorMatch.match(line)
149 if not m:
150 break
152 username = m.group(1)
153 if not deletedUsernameMatch.match(username):
154 langsLists[lang].append(username)
155 if line.strip() == '# Translators:':
156 reached = True
157 poFile.close()
159 # Sort and remove duplicates
160 # Sorting should ignore case to have a neat credits list
161 langsLists[lang] = sorted(set(langsLists[lang]), key=lambda s: s.lower())
163 # Now insert the new data into the new JSON file
164 for (langCode, langList) in sorted(langsLists.items()):
165 newJSONData['Content'].append({'LangName': langs[langCode], 'List': []})
166 for name in langList:
167 newJSONData['Content'][-1]['List'].append({'name': name})
169 # Save the JSON data to the credits file
170 creditsFile = open(creditsLocation, 'w', encoding='utf-8')
171 json.dump(newJSONData, creditsFile, indent=4)
172 creditsFile.close()