Skip to content

Commit

Permalink
ENH: display locale pref as description not a code
Browse files Browse the repository at this point in the history
- still need a few translations of language names for mappings
  • Loading branch information
jeremygray committed Jul 24, 2014
1 parent 5d8fc9e commit 56c0a45
Show file tree
Hide file tree
Showing 4 changed files with 260 additions and 74 deletions.
3 changes: 0 additions & 3 deletions psychopy/app/_psychopyApp.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,6 @@ def onInit(self, showSplash=True, testMode=False):

# import localization after wx:
from psychopy.app import localization # needed by splash screen
# eventually want to use wx.GetTranslation exclusively, but for now use gettext.GNUTranslations as well
#__builtins__['_'] = wx.GetTranslation # define _( ) function as app-global

self.locale = localization.wxlocale
self.localization = localization
self.locale.AddCatalog(self.GetAppName())
Expand Down
61 changes: 41 additions & 20 deletions psychopy/app/localization/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,65 +15,86 @@


import gettext
import os, sys, glob
import os, sys, glob, codecs
from psychopy import logging, prefs

# Get a dict of locale aliases (cross-platform?) from wx.Locale()
# Get a dict of locale aliases from wx.Locale() -- same cross-platform (Win 7, Mac 10.9)
import wx
locale = wx.Locale()
aliases = {}
idFromCode = {}
codeFromId = {}
for i in range(256):
wxIdFromCode = {} # int: 0, 2-229
codeFromWxId = {} # used in directory names e.g. ja_JP; never JPN ala Windows
winmap = {} # get windows 3-letter code (=val) from canonical form (=key); use only for setting locale (non-wx)
locname = {} # descriptive name, if available; 5-letter code if not
reverseMap = {}

for i in range(230):
info = locale.GetLanguageInfo(i)
if info:
aliases[info.Description] = info.CanonicalName
idFromCode[info.CanonicalName] = i
codeFromId[i] = info.CanonicalName
aliases['English'] = 'en_US'
aliases[info.Description] = info.CanonicalName # mix of forms: ja or ja_JP
wxIdFromCode[info.CanonicalName] = i
codeFromWxId[i] = info.CanonicalName

for line in codecs.open(os.path.join(os.path.dirname(__file__), 'mappings'), 'rU', 'utf8').readlines():
try:
can, win, name = line.strip().split(' ', 2) # canonical, windows, name-with-spaces
except ValueError:
can, win = line.strip().split(' ', 1)
name = can
winmap[can] = win
locname[can] = name
reverseMap[name] = can

# what are the available translations? available languages on the OS?
expr = os.path.join(os.path.dirname(__file__), '..', 'locale', '*')
available = [''] + sorted(map(os.path.basename, glob.glob(expr)))
sysAvail = [str(l) for l in codeFromId.values() # installed language packs
if l and locale.IsAvailable(idFromCode[l])]
available = [''] + [locname[key] for key in sorted(map(os.path.basename, glob.glob(expr)))]
sysAvail = [str(l) for l in codeFromWxId.values() # installed language packs
if l and locale.IsAvailable(wxIdFromCode[l])]

def getID(lang=None):
"""Get wx ID of language to use for translations: `lang`, pref, or system default.
`lang` is a two-character language code, or 5 char `language_REGION`
`lang` is a 5 char `language_REGION`, eg ja_JP
"""
if lang:
val = lang
else:
try:
val = prefs.app['locale']
except KeyError:
val = locale.GetLocale() # wx.Locale
val = locale.GetLocale() # wx.Locale, no encoding
if not val:
val = codeFromId[wx.LANGUAGE_DEFAULT]
val = codeFromWxId[wx.LANGUAGE_DEFAULT]
try:
# can't set wx.Locale here because no app yet
language = idFromCode[val]
# here just determine the value to be used when it can be set
language = wxIdFromCode[val]
except KeyError:
logging.error('locale %s not known to wx.Locale, using default' % val)
language = wx.LANGUAGE_DEFAULT
return language

languageID = getID()
return language, val

languageID, lang = getID()
#use lang like this:
#import locale -- the non-wx version of locale
#
#if sys.platform.startswith('win'):
# v = winmap[val]
#else: v=val
#locale.setlocale(locale.LC_ALL, (v, 'UTF-8'))

# set locale before splash screen:
if locale.IsAvailable(languageID):
wxlocale = wx.Locale(languageID)
else:
wxlocale = wx.Locale(wx.LANGUAGE_DEFAULT)
lang = codeFromId[languageID]

# ideally rewrite the following using wxlocale only:
path = os.path.join(os.path.dirname(__file__), '..', 'locale', lang, 'LC_MESSAGE') + os.sep
mofile = os.path.join(path, 'messages.mo')
try:
logging.debug("Opening message file %s for locale %s" % (mofile, lang))
logging.debug("Opening message catalog %s for locale %s" % (mofile, lang))
trans = gettext.GNUTranslations(open(mofile, "rb"))
except IOError:
logging.debug("Locale for '%s' not found. Using default." % lang)
Expand Down
169 changes: 169 additions & 0 deletions psychopy/app/localization/mappings
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
af_ZA AFK
am_ET AMH
ar_SA ARA
ar_LB ARB
ar_EG ARE
ar_DZ ARG
ar_BH ARH
ar_IQ ARI
ar_JO ARJ
ar_KW ARK
ar_LY ARL
ar_MA ARM
ar_OM ARO
ar_QA ARQ
ar_SY ARS
ar_TN ART
ar_AE ARU
ar_YE ARY
as_IN ASM
ba_RU BAS
be_BY BEL
bg_BG BGR
bn_BD BNG
bn_IN BNG
bo_CN BOB
br_FR BRE
ca_ES CAT
zh_CN CHS
zh_TW CHT
co_FR COS
cs_CZ CSY čeština
cy_GB CYM
da_DK DAN dansk
de_AT DEA
de_LI DEC
de_LU DEL
de_CH DES
de_DE DEU Deutsch
el_GR ELL ελληνικά
en_AU ENA
en_CB ENB
en_CA ENC
en_SG ENE
en_GB ENG English (U. K.)
en_IE ENI
en_JM ENJ
en_BZ ENL
en_MY ENM
en_IN ENN
en_PH ENP
en_ZA ENS
en_TT ENT
en_US ENU English (U. S.)
en_ZW ENW
en_NZ ENZ English (New Zealand)
es_PA ESA
es_BO ESB
es_CR ESC
es_DO ESD
es_SV ESE
es_EC ESF
es_GT ESG
es_HN ESH
es_NI ESI
es_CL ESL
es_MX ESM
es_ES ESN español
es_CO ESO
es_PE ESR
es_AR ESS
es_US EST
es_PR ESU
es_VE ESV
es_UY ESY
es_PY ESZ
et_EE ETI
eu_ES EUQ
fa_IR FAR
fi_FI FIN
fo_FO FOS
fr_FR FRA français
fr_BE FRB
fr_CA FRC
fr_LU FRL
fr_MC FRM
fr_CH FRS
fy_NL FYN
gd_GB GLA
gl_ES GLC
gu_IN GUJ
he_IL HEB
hi_IN HIN हिन्दी
hr_BA HRB
hr_HR HRV
hu_HU HUN
hy_AM HYE
ig_NG IBO
ii_CN III
id_ID IND
ga_IE IRE
is_IS ISL
it_IT ITA italiano
it_CH ITS
ja_JP JPN 日本語
kl_GL KAL
ka_GE KAT
kn_IN KDI
km_KH KHM
rw_RW KIN
kk_KZ KKZ
ko_KR KOR 한국어
ky_KG KYR
lo_LA LAO
lb_LU LBX
lt_LT LTH
lv_LV LVI
mr_IN MAR
mk_MK MKI
mt_MT MLT
mn_MN MON
mi_NZ MRI
ms_BN MSB
ms_MY MSL
ml_IN MYM
ne_NP NEP
nl_BE NLB
nl_NL NLD Nederlands
nn_NO NON
nb_NO NOR
ns_ZA NSO
oc_FR OCI
or_IN ORI
pa_IN PAN
ps_AF PAS
pl_PL PLK polski
pt_BR PTB
pt_PT PTG português
rm_CH RMC
ro_RO ROM limba română
ru_RU RUS Русский язык
sa_IN SAN
si_LK SIN
sk_SK SKY
sl_SI SLV
se_NO SME
se_SE SMF
se_FI SMG
sq_AL SQI
sv_SE SVE Svenska
sv_FI SVF
sw_KE SWK
ta_IN TAM
te_IN TEL
th_TH THA
tr_TR TRK Türkçe
tn_ZA TSN
tt_RU TTT
tk_TM TUK
ug_CN UIG
uk_UA UKR Українська
ur_PK URD
vi_VN VIT
wo_SN WOL
xh_ZA XHO
yo_NG YOR
zh_HK ZHH
zh_SG ZHI
zh_MO ZHM
zu_ZA ZUL

0 comments on commit 56c0a45

Please sign in to comment.