einstein/einsteingame/i18n.py

109 lines
2.3 KiB
Python
Raw Permalink 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):
2015-11-04 12:46:33 +00:00
self.__country = None
self.__language = None
2015-11-03 16:38:03 +00:00
self.parseLocale(setlocale(LC_ALL, ""))
# hack because of numbers in Lua
setlocale(LC_NUMERIC, "C")
2015-11-04 12:46:33 +00:00
@property
def country(self):
return self.__country
@property
def language(self):
return self.__language
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:
2015-11-04 12:46:33 +00:00
self.__language = langAndCountry
self.__country = ''
2015-11-03 16:38:03 +00:00
else:
2015-11-04 12:46:33 +00:00
self.__language = langAndCountry[0:pos]
self.__country = langAndCountry[pos + 1:]
2015-11-03 16:38:03 +00:00
2015-11-04 12:46:33 +00:00
self.__language = self.__language.lower()
self.__country = self.__country.lower()
2015-11-03 16:38:03 +00:00
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-04 12:46:33 +00:00
if locale.country == country:
2015-11-03 18:26:33 +00:00
score += 2
2015-11-04 12:46:33 +00:00
if locale.language == lang:
2015-11-03 18:26:33 +00:00
score += 4
return score
2015-11-03 16:38:03 +00:00
2015-11-03 18:26:33 +00:00
locale = Locale()