einstein/einsteingame/i18n.py

99 lines
2.1 KiB
Python
Raw Normal View History

2015-11-03 18:26:33 +00:00
from locale import setlocale, LC_ALL, LC_NUMERIC
2015-11-03 16:38:03 +00:00
class Locale(object):
def __init__(self):
self.parseLocale(setlocale(LC_ALL, ""))
# hack because of numbers in Lua
setlocale(LC_NUMERIC, "C")
2015-11-03 18:26:33 +00:00
def parseLocale(self, name):
2015-11-03 16:38:03 +00:00
pos = name.find('.')
langAndCountry = None
if pos >= 0:
encoding = name[pos + 1:]
langAndCountry = name[0:pos]
else:
encoding = ""
langAndCountry = name
pos = langAndCountry.find('_')
if pos < 0:
language = langAndCountry
country = ""
else:
language = langAndCountry[0:pos]
country = langAndCountry[pos + 1:]
language = language.lower()
country = country.lower()
encoding = encoding.upper()
def splitFileName(fileName):
"""
Returns tuple of name, extension, lang, country
"""
pos = fileName.rfind('.')
if pos <= 0:
ext = "";
name = fileName
else:
name = fileName[0:pos]
ext = fileName[pos + 1:]
pos = name.rfind('_')
if pos <= 0 or len(name) - pos != 3:
lang = ""
country = ""
else:
s = name[0:pos]
l = name[pos + 1:]
if l.isupper():
name = s
country = l
pos = name.rfind('_')
if pos <= 0 or len(name) - pos != 3:
lang = ""
else:
l = name[pos + 1:]
s = name[0:pos]
if l.islower():
name = s
lang = l
else:
lang = ""
elif l.islower():
name = s
lang = l
country = ""
else:
lang = ""
country = ""
return name, ext, lang, country
def getScore(lang, country, locale):
2015-11-03 18:26:33 +00:00
if len(country) == 0 and len(lang) == 0:
2015-11-03 16:38:03 +00:00
return 1
score = 0
2015-11-03 18:26:33 +00:00
if locale.getCountry() == country:
score += 2
if locale.getLanguage() == lang:
score += 4
return score
2015-11-03 16:38:03 +00:00
2015-11-03 18:26:33 +00:00
locale = Locale()