GrampsLocale: Get the language code from the global translation

Instead of probing locale.getlocale() and environment variables

svn: r21151
This commit is contained in:
John Ralls 2013-01-17 19:48:24 +00:00
parent 9e8b2af5c1
commit fba8cdddd1
4 changed files with 16 additions and 67 deletions

View File

@ -1792,49 +1792,15 @@ def get_relationship_calculator(reinit=False):
global __RELCALC_CLASS global __RELCALC_CLASS
if __RELCALC_CLASS is None or reinit: if __RELCALC_CLASS is None or reinit:
lang = ' ' lang = glocale.get_translation().language()
try:
lang = os.environ["LANG"]
lang_set = True
except:
# if LANG is not set
import locale
lang = locale.getlocale()[0]
if not lang:
# if lang is empty/None
lang = locale.getdefaultlocale()[0]
lang_set = False
__RELCALC_CLASS = RelationshipCalculator __RELCALC_CLASS = RelationshipCalculator
# If lang not set default to English relationship calulator # If lang not set default to English relationship calulator
if not lang:
return __RELCALC_CLASS()
# See if lang begins with en_, English_ or english_ # See if lang begins with en_, English_ or english_
# If so return standard relationship calculator. # If so return standard relationship calculator.
enlang = lang.split('_')[0].lower() if lang == "en":
if enlang in ('en', 'english', 'c'):
return __RELCALC_CLASS() return __RELCALC_CLASS()
# set correct non English relationship calculator based on LANG # set correct non English relationship calculator based on lang
# or on locale.getlocale() or if that fails locale.getdeafultlocale()
relation_translation_found = False relation_translation_found = False
for plugin in PluginRegister.get_instance().relcalc_plugins():
if lang in plugin.lang_list:
pmgr = BasePluginManager.get_instance()
# the loaded module is put in variable mod
mod = pmgr.load_plugin(plugin)
if mod:
__RELCALC_CLASS = eval('mod.' + plugin.relcalcclass)
relation_translation_found = True
break
if not relation_translation_found:
# LANG set but with non recognizable language info. Try getlocale
import locale
lang = locale.getlocale()[0]
if not lang:
# if lang is empty/None
try:
lang = locale.getdefaultlocale()[0]
except:
pass
for plugin in PluginRegister.get_instance().relcalc_plugins(): for plugin in PluginRegister.get_instance().relcalc_plugins():
if lang in plugin.lang_list: if lang in plugin.lang_list:
pmgr = BasePluginManager.get_instance() pmgr = BasePluginManager.get_instance()

View File

@ -423,7 +423,7 @@ class GrampsNullTranslations(gettext.NullTranslations):
Note that it's necessary for msgid to be unicode. If it's not, Note that it's necessary for msgid to be unicode. If it's not,
neither will be the returned string. neither will be the returned string.
""" """
def sgettext(self, msgid): def sgettext(self, msgid, sep='|'):
msgval = self.gettext(msgid) msgval = self.gettext(msgid)
if msgval == msgid: if msgval == msgid:
sep_idx = msgid.rfind(sep) sep_idx = msgid.rfind(sep)

View File

@ -23,7 +23,7 @@
from gramps.gen.const import URL_MANUAL_PAGE, URL_WIKISTRING from gramps.gen.const import URL_MANUAL_PAGE, URL_WIKISTRING
from gramps.gen.constfunc import is_quartz from gramps.gen.constfunc import is_quartz
from gramps.gen.config import config from gramps.gen.config import config
import locale from gramps.gen.const import GRAMPS_LOCALE as glocale
import os import os
#list of manuals on wiki, map locale code to wiki extension, add language codes #list of manuals on wiki, map locale code to wiki extension, add language codes
@ -40,28 +40,11 @@ MANUALS = {
} }
#first, determine language code, so nl_BE --> wiki /nl #first, determine language code, so nl_BE --> wiki /nl
LANG = locale.getlocale()[0] lang = glocale.get_translation().language()
if not LANG: if lang in MANUALS:
LANG = 'C' EXTENSION = MANUALS[lang]
#support environment overrule: else:
try: EXTENSION = ''
if not os.environ['LANGUAGE'] or \
os.environ['LANGUAGE'].split(':')[0] == LANG:
pass
else:
LANG = os.environ['LANGUAGE'].split(':')[0]
except:
pass
EXTENSION = ''
try:
EXTENSION = MANUALS[LANG]
except KeyError:
pass
try:
if not EXTENSION :
EXTENSION = MANUALS[LANG.split('_')[0]]
except KeyError:
pass
def display_help(webpage='', section=''): def display_help(webpage='', section=''):
""" """

View File

@ -8531,15 +8531,15 @@ def first_letter(string):
else: else:
letter = cuni(' ') letter = cuni(' ')
# See : http://www.gramps-project.org/bugs/view.php?id = 2933 # See : http://www.gramps-project.org/bugs/view.php?id = 2933
(lang_country, modifier ) = locale.getlocale() lang_country = glocale.get_translation().language()
if lang_country == "sv_SE" and (letter == cuni('W') or letter == cuni('V')): if lang_country == "sv" and (letter == cuni('W') or letter == cuni('V')):
letter = 'V,W' letter = 'V,W'
# See : http://www.gramps-project.org/bugs/view.php?id = 4423 # See : http://www.gramps-project.org/bugs/view.php?id = 4423
elif (lang_country == "cs_CZ" or lang_country == "sk_SK") and letter == cuni('C') and len(string) > 1: elif (lang_country == "cs" or lang_country == "sk") and letter == cuni('C') and len(string) > 1:
second_letter = normalize('NFKC', str(string))[1].upper() second_letter = normalize('NFKC', str(string))[1].upper()
if second_letter == cuni('H'): if second_letter == cuni('H'):
letter += cuni('h') letter += cuni('h')
elif lang_country == "sk_SK" and letter == cuni('D') and len(string) > 1: elif lang_country == "sk" and letter == cuni('D') and len(string) > 1:
second_letter = normalize('NFKC', cuni(string))[1].upper() second_letter = normalize('NFKC', cuni(string))[1].upper()
if second_letter == cuni('Z'): if second_letter == cuni('Z'):
letter += cuni('z') letter += cuni('z')
@ -8594,7 +8594,7 @@ def alphabet_navigation(menu_set):
# #
# See : http://www.gramps-project.org/bugs/view.php?id = 2933 # See : http://www.gramps-project.org/bugs/view.php?id = 2933
# #
(lang_country, modifier) = locale.getlocale() lang_country = glocale.get_translation().language()
for menu_item in menu_set: for menu_item in menu_set:
sorted_set[menu_item] += 1 sorted_set[menu_item] += 1
@ -8621,7 +8621,7 @@ def alphabet_navigation(menu_set):
while (cols <= num_of_cols and index < num_ltrs): while (cols <= num_of_cols and index < num_ltrs):
menu_item = sorted_alpha_index[index] menu_item = sorted_alpha_index[index]
if lang_country == "sv_SE" and menu_item == cuni('V'): if lang_country == "sv" and menu_item == cuni('V'):
hyper = Html("a", "V,W", href = "#V,W", title = "V,W") hyper = Html("a", "V,W", href = "#V,W", title = "V,W")
else: else:
# adding title to hyperlink menu for screen readers and braille writers # adding title to hyperlink menu for screen readers and braille writers