RetroArch/intl/pseudolocalize.py
2016-08-26 17:47:46 +02:00

105 lines
3.0 KiB
Python
Executable File

#!/usr/bin/env python
# coding=UTF-8
# RetroArch - A frontend for libretro.
# Copyright (C) 2011-2016 - Daniel De Matteis
#
# RetroArch 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 Found-
# ation, either version 3 of the License, or (at your option) any later version.
#
# RetroArch 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 RetroArch.
# If not, see <http://www.gnu.org/licenses/>.
# You probably don't need this script. It's only needed to update menu_hash_uspseudo.c,
# and that's not necessary either, because outdated translations fall back to English.
# Usage: ./pseudolocalize.py < msg_hash_us.c > msg_hash_uspseudo.c
replacements = {
# These characters all exist in ISO-8859-1.
#" ": "´¨¯¸",
"!": "¡",
"?": "¿",
"a": "áàâåäã",
"A": "ÁÀÂÅÄÃ",
"c": "ç",
"C": "Ç",
"D": "Ð",
"e": "éèêë",
"E": "ÉÈÊË",
"i": "íìîï",
"I": "ÍÌÎÏ",
"n": "ñ",
"N": "Ñ",
"o": "óòôöõø",
"O": "ÓÒÔÖÕØ",
"u": "úùûü",
"U": "ÚÙÛÜ",
"y": "ýÿ",
"Y": "Ý",
}
import sys,random
if sys.version_info >= (3, 0):
import io
sys.stdin = io.TextIOWrapper(sys.stdin.buffer, encoding='ISO-8859-1')
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='ISO-8859-1')
else:
import codecs
sys.stdin = codecs.getreader('ISO-8859-1')(sys.stdin)
sys.stdout = codecs.getwriter('UTF-8')(sys.stdout)
nreplacements = {}
for k in replacements:
nreplacements[k] = replacements[k].decode("UTF-8")
replacements = nreplacements
def pseudolocalize(string):
ret = ""
it = iter(string)
for ch in it:
if ch == '\\' or ch == '%':
ret += ch
ret += next(it)
continue
try:
if random.randint(0,2) == 0:
cand = replacements[ch]
ret += random.choice(cand)
else:
ret += ch
except KeyError:
ret += ch
return ret
def pseudolocalize_code(string):
if '"' not in string: return string
if '"null"' in string: return string
if '"Main Menu"' in string: return string # RetroArch bug, this string can't be translated.
parts = string.split('"')
parts[1] = pseudolocalize(parts[1])
return '"'.join(parts);
print("/* Autogenerated, do not edit. Your changes will be undone. */")
localize = True
for line in sys.stdin:
line = line.strip("\n")
if 'const char *hash_to_str_us_label_enum' in line: localize = False
if 'return "null"' in line: localize = True
if line=='#ifdef HAVE_PSEUDOLOCALIZE': line="#if 0"
if '#include' in line: pass
elif not localize: pass
else: line = pseudolocalize_code(line)
print(line)