1#!/usr/bin/env python2 2# -*- coding:utf-8 -*- 3# 4# Copyright (C) 2018 Wildfire Games. 5# This file is part of 0 A.D. 6# 7# 0 A.D. is free software: you can redistribute it and/or modify 8# it under the terms of the GNU General Public License as published by 9# the Free Software Foundation, either version 2 of the License, or 10# (at your option) any later version. 11# 12# 0 A.D. is distributed in the hope that it will be useful, 13# but WITHOUT ANY WARRANTY; without even the implied warranty of 14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15# GNU General Public License for more details. 16# 17# You should have received a copy of the GNU General Public License 18# along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 19 20""" 21This file imports the translators credits located in the public mod GUI files and 22runs through .po files to add possible new translators to it. 23It only appends new people, so it is possible to manually add names in the credits 24file and they won't be overwritten by running this script. 25 26Translatable strings will be extracted from the generated file, so this should be ran 27before updateTemplates.py. 28""" 29 30from __future__ import absolute_import, division, print_function, unicode_literals 31 32import json, os, glob, re 33 34# Credited languages - Keep in sync with source/tools/dist/remove-incomplete-translations.sh 35langs = { 36 'ast': 'Asturianu', 37 'bg': 'Български', 38 'ca': 'Català', 39 'cs': 'Čeština', 40 'de': 'Deutsch', 41 'el': 'Ελληνικά', 42 'en_GB': 'English (UK)', 43 'es': 'Español', 44 'eu': 'Euskara', 45 'fr': 'Français', 46 'gd': 'Gàidhlig', 47 'gl': 'Galego', 48 'hu': 'Magyar', 49 'id': 'Bahasa Indonesia', 50 'it': 'Italiano', 51 'ms': 'Bahasa Melayu', 52 'nb': 'Norsk bokmål', 53 'nl': 'Nederlands', 54 'pl': 'Polski', 55 'pt_BR': 'Português (Brasil)', 56 'pt_PT': 'Português (Portugal)', 57 'ru': 'Русский', 58 'sk': 'Slovenčina', 59 'sv': 'Svenska', 60 'tr': 'Türkçe', 61 'uk': 'Українська'} 62 63root = '../../../' 64 65poLocations = [ 66 'binaries/data/l10n/', 67 'binaries/data/mods/public/l10n/', 68 'binaries/data/mods/mod/l10n/'] 69 70creditsLocation = 'binaries/data/mods/public/gui/credits/texts/translators.json' 71 72# Load JSON data 73creditsFile = open(root + creditsLocation) 74JSONData = json.load(creditsFile) 75creditsFile.close() 76 77# This dictionnary will hold creditors lists for each language, indexed by code 78langsLists = {} 79 80# Create the new JSON data 81newJSONData = {'Title': 'Translators', 'Content': []} 82 83# First get the already existing lists. If they correspond with some of the credited languages, 84# add them to the new data after processing, else add them immediately. 85# NB: All of this is quite inefficient 86for element in JSONData['Content']: 87 if 'LangName' not in element or element['LangName'] not in langs.values(): 88 newJSONData['Content'].append(element) 89 continue 90 91 for (langCode, langName) in langs.items(): 92 if element['LangName'] == langName: 93 langsLists[langCode] = element['List'] 94 break 95 96# Now actually go through the list of languages and search the .po files for people 97 98# Prepare some regexes 99commentMatch = re.compile('#.*') 100translatorMatch = re.compile('# ([\w\s]*)(?: <.*>)?, [0-9-]', re.UNICODE) 101 102# Search 103for lang in langs.keys(): 104 if lang not in langsLists.keys(): 105 langsLists[lang] = [] 106 107 for location in poLocations: 108 files = glob.glob(root + location + lang + '.*.po') 109 for file in files: 110 poFile = open(file.replace('\\', '/')) 111 reached = False 112 for line in poFile: 113 line = line.decode('utf8') 114 if reached: 115 if not commentMatch.match(line): 116 break 117 m = translatorMatch.match(line) 118 if m: 119 langsLists[lang].append(m.group(1)) 120 if line.strip() == '# Translators:': 121 reached = True 122 poFile.close() 123 124 # Sort and remove duplicates 125 # Sorting should ignore case to have a neat credits list 126 langsLists[lang] = sorted(set(langsLists[lang]), cmp=lambda x,y: cmp(x.lower(), y.lower())) 127 128# Now insert the new data into the new JSON file 129for (langCode, langList) in sorted(langsLists.items()): 130 newJSONData['Content'].append({'LangName': langs[langCode], 'List': []}) 131 for name in langList: 132 newJSONData['Content'][-1]['List'].append({'name': name}) 133 134# Save the JSON data to the credits file 135creditsFile = open(root + creditsLocation, 'w') 136json.dump(newJSONData, creditsFile, indent=4) 137creditsFile.close() 138