Bug #4302: Language settings are wrong when an english variant is first

Rewrote the launcher in python with much improved language handling; 
renamed it to rungramps.py, and replaced launcher.sh with a minimal 
shell launcher.

Any english selection will now have $LANG set to "C", while $LC_ALL will 
be set to whatever english variant the user selected (and en_US if she 
specified just "en"). For languages other than English, $LC_ALL will be 
set to xx_XX if the language selection had no region (e.g., if the 
language selection is de, $LANG will be de and $LC_ALL will be de_DE. 
$LANG will be truncated if the language selection has a region that 
Gramps doesn't support, but does support the bare language (e.g., if the 
language selection is de_DE, $LANG will be de because there is no de_DE 
translation. Gettext is smart enough to get this right without the help, 
but the check is necessary to ensure that we don't skip over a supported 
generic language.

The language settings can now be overridden for gramps by using the 
defaults system.


svn: r17342
This commit is contained in:
John Ralls 2011-04-29 21:21:33 +00:00
parent 35628aa699
commit d0265cebaf
2 changed files with 137 additions and 168 deletions

View File

@ -1,169 +1,3 @@
#!/bin/sh
name="`basename $0`"
tmp="`pwd`/$0"
tmp=`dirname "$tmp"`
tmp=`dirname "$tmp"`
bundle=`dirname "$tmp"`
bundle_contents="$bundle"/Contents
bundle_res="$bundle_contents"/Resources
bundle_lib="$bundle_res"/lib
bundle_bin="$bundle_res"/bin
bundle_data="$bundle_res"/share
bundle_etc="$bundle_res"/etc
export XDG_DATA_DIRS="$bundle_data"
export DYLD_LIBRARY_PATH="$bundle_lib"
export LD_LIBRARY_PATH="$DYLD_LIBRARY_PATH"
export GTK_DATA_PREFIX="$bundle_res"
export GTK_EXE_PREFIX="$bundle_res"
export GTK_PATH="$bundle_res"
export GTK2_RC_FILES="$bundle_etc/gtk-2.0/gtkrc"
export GTK_IM_MODULE_FILE="$bundle_etc/gtk-2.0/gtk.immodules"
export GDK_PIXBUF_MODULE_FILE="$bundle_etc/gtk-2.0/gdk-pixbuf.loaders"
export PANGO_RC_FILE="$bundle_etc/pango/pangorc"
#Set $PYTHON to point inside the bundle
export PYTHON="$bundle_contents/MacOS/python"
#Add the bundle's python modules
PYTHONPATH="$bundle_lib/python2.6:$PYTHONPATH"
PYTHONPATH="$bundle_lib/python2.6/site-packages:$PYTHONPATH"
PYTHONPATH="$bundle_lib/python2.6/site-packages/gtk-2.0:$PYTHONPATH"
PYTHONPATH="$bundle_lib/python2.6/lib-dynload:$PYTHONPATH"
#Add our program's modules to $PYTHONPATH.
PYTHONPATH="$bundle_lib/pygtk/2.0:$PYTHONPATH"
export PYTHONPATH
export GRAMPSDIR="$bundle_data"/gramps
export GRAMPSI18N="$bundle_data"/locale
export GRAMPSHOME="$HOME/Library/Application Support"
# Set the locale-related variables appropriately:
unset LANG LC_MESSAGES LC_MONETARY
# Has a language ordering been set?
# If so, set LC_MESSAGES accordingly; otherwise skip it.
# Set the locale-related variables appropriately:
unset LANG LC_MESSAGES LC_MONETARY LC_COLLATE
# Has a language ordering been set?
# If so, set LC_MESSAGES and LANG accordingly; otherwise skip it.
# First step uses sed to clean off the quotes and commas, to change - to _, and change the names for the chinese scripts from "Hans" to CN and "Hant" to TW.
APPLELANGUAGES=`defaults read .GlobalPreferences AppleLanguages | sed -En -e 's/\-/_/' -e 's/Hant/TW/' -e 's/Hans/CN/' -e 's/[[:space:]]*\"?([[:alnum:]_]+)\"?,?/\1/p' `
if test "$APPLELANGUAGES"; then
# A language ordering exists.
# Test, item per item, to see whether there is an corresponding locale.
for L in $APPLELANGUAGES; do
#test for exact matches:
if test -f "$GRAMPSI18N/${L}/LC_MESSAGES/gramps.mo"; then
export LANG=$L
break
fi
#This is a special case, because often the original strings are in US
#English and there is no translation file.
if test "x$L" == "xen_US"; then
export LANG=$L
break
fi
#OK, now test for just the first two letters:
if test -f "$GRAMPSI18N/${L:0:2}/LC_MESSAGES/gramps.mo"; then
export LANG=${L:0:2}
break
fi
#Same thing, but checking for any english variant.
if test "x${L:0:2}" == "xen"; then
export LANG=$L
break
fi;
done
fi
unset APPLELANGUAGES L
# If we didn't get a language from the language list, try the Collation preference, in case it's the only setting that exists.
APPLECOLLATION=`defaults read .GlobalPreferences AppleCollationOrder`
if test -z "$LANG" -a -n "$APPLECOLLATION"; then
if test -f "$GRAMPSI18N/$APPLECOLLATION/LC_MESSAGES/gramps.mo"; then
export LANG=$APPLECOLLATION
fi
fi
if test -n "$APPLECOLLATION"; then
export LC_COLLATE=$APPLECOLLATION
fi
unset APPLECOLLATION
# Continue by attempting to find the Locale preference.
APPLELOCALE=`defaults read .GlobalPreferences AppleLocale`
if test -f "$GRAMPSI18N/${APPLELOCALE:0:5}/LC_MESSAGES/gramps.mo"; then
if test -z $LANG; then
export LANG="${APPLELOCALE:0:5}"
fi
elif test -z $LANG -a -f "$GRAMPSI18N/${APPLELOCALE:0:2}/LC_MESSAGES/gramps.mo"; then
export LANG="${APPLELOCALE:0:2}"
fi
#Next we need to set LC_MESSAGES. If at all possilbe, we want a full
#5-character locale to avoid the "Locale not supported by C library"
#warning from Gtk -- even though Gtk will translate with a
#two-character code.
if test -n $LANG; then
#If the language code matches the applelocale, then that's the message
#locale; otherwise, if it's longer than two characters, then it's
#probably a good message locale and we'll go with it.
if test $LANG == ${APPLELOCALE:0:5} -o $LANG != ${LANG:0:2}; then
export LC_MESSAGES=$LANG
#Next try if the Applelocale is longer than 2 chars and the language
#bit matches $LANG
elif test $LANG == ${APPLELOCALE:0:2} -a $APPLELOCALE > ${APPLELOCALE:0:2}; then
export LC_MESSAGES=${APPLELOCALE:0:5}
#Fail. Get a list of the locales in $PREFIX/share/locale that match
#our two letter language code and pick the first one, special casing
#english to set en_US
elif test $LANG == "en"; then
export LC_MESSAGES="en_US"
else
LOC=`find $PREFIX/share/locale -name $LANG???`
echo "Locales $LOC"
for L in $LOC; do
export LC_MESSAGES=$L
done
fi
else
#All efforts have failed, so default to US english
export LANG="en_US"
export LC_MESSAGES="en_US"
fi
CURRENCY=`echo $APPLELOCALE | sed -En 's/.*currency=([[:alpha:]]+).*/\1/p'`
if test "x$CURRENCY" != "x"; then
#The user has set a special currency. Gtk doesn't install LC_MONETARY files, but Apple does in /usr/share/locale, so we're going to look there for a locale to set LC_CURRENCY to.
if test -f /usr/local/share/$LC_MESSAGES/LC_MONETARY; then
if test -a `cat /usr/local/share/$LC_MESSAGES/LC_MONETARY` == $CURRENCY; then
export LC_MONETARY=$LC_MESSAGES
fi
fi
if test -z "$LC_MONETARY"; then
FILES=`find /usr/share/locale -name LC_MONETARY -exec grep -H $CURRENCY {} \;`
if test -n "$FILES"; then
export LC_MONETARY=`echo $FILES | sed -En 's%/usr/share/locale/([[:alpha:]_]+)/LC_MONETARY.*%\1%p'`
fi
fi
fi
#No currency value means that the AppleLocale governs:
if test -z "$LC_MONETARY"; then
export LC_MONETARY=${APPLELOCALE:0:5}
fi
#To turn on the appropriate dictionary:
export LC_ALL=$LC_MESSAGES
unset APPLELOCALE FILES LOC
# Strip out the argument added by the OS.
if [ x`echo "x$1" | sed -e "s/^x-psn_.*//"` == x ]; then
shift 1
fi
#Note that we're calling $PYTHON here to override the version in
#pygtk-demo's shebang.
exec $PYTHON -O "$GRAMPSDIR/gramps.py" "$@"
PWD=`dirname $0`
exec "$PWD/python" "$PWD/rungramps.py" $@

135
mac/rungramps.py Executable file
View File

@ -0,0 +1,135 @@
#!/usr/bin/env python
import sys, os, subprocess
_home = os.environ["HOME"]
_res_path = os.path.normpath(os.path.join(sys.path[0], "..", "Resources"))
_lib_path = os.path.join(_res_path, "lib")
_share_path = os.path.join(_res_path, "share")
_pylib_path = os.path.join(_lib_path, "python2.6")
_site_lib_path = os.path.join(_pylib_path, "site-packages")
_gramps_path = os.path.join(_share_path, "gramps")
_gramps_locale = os.path.join(_share_path, "locale")
_conf_path = os.path.join(_res_path, "etc");
_gtk2_conf = os.path.join(_conf_path, "gtk-2.0")
sys.path = [_gramps_path,
os.path.join(_pylib_path, "lib-dynload"),
os.path.join(_site_lib_path, "pygtk", "2.0"),
os.path.join(_site_lib_path, "gtk-2.0"),
_site_lib_path,
_pylib_path]
os.environ["GTK_PATH"] = _res_path
os.environ["GTK2_RC_FILES"] = os.path.join(_gtk2_conf, "gtkrc")
os.environ["GTK_IM_MODULE_FILE"]= os.path.join(_gtk2_conf, "immodules")
os.environ["GDK_PIXBUF_LOADERS"] = os.path.join(_gtk2_conf, "gdk-pixbuf.loaders")
os.environ["PANGO_RC_FILES"] = os.path.join(_conf_path, "pango", "pangorc")
os.environ["GRAMPSDIR"] = _gramps_path
os.environ["GRAMPSI18N"] = _gramps_locale
os.environ["GRAMPSHOME"] = os.path.join(_home, "Library", "Application Support")
LANG = "C" #Default
defaults = "/usr/bin/defaults"
_languages = ""
_collation = ""
_locale = ""
_language = ""
try:
_languages = subprocess.Popen(
[defaults, "read", "-app", "Gramps", "AppleLanguages"],
stderr=open("/dev/null"),
stdout=subprocess.PIPE).communicate()[0].strip("()\n").split(",\n")
except:
pass
if not _languages:
try:
_languages = subprocess.Popen(
[defaults, "read", "-g", "AppleLanguages"],
stderr=open("/dev/null"),
stdout=subprocess.PIPE).communicate()[0].strip("()\n").split(",\n")
except:
pass
for _lang in _languages:
_lang=_lang.strip().strip('"').replace("-", "_", 1)
if _lang == "cn_Hant": #Traditional; Gettext uses cn_TW
_lang = "cn_TW"
if _lang == "cn_Hans": #Simplified; Gettext uses cn_CN
_lang = "cn_CN"
_language = _lang
if _lang.startswith("en"): #Gramps doesn't have explicit English translation, use C
break
if os.path.exists(os.path.join(_gramps_locale, _lang, "LC_MESSAGES",
"gramps.mo")):
LANG = _lang
break
elif os.path.exists(os.path.join(_gramps_locale, _lang[:2], "LC_MESSAGES",
"gramps.mo")):
LANG = _lang[:2]
break
try:
_collation=subprocess.Popen(
[defaults, "read", "-app", "Gramps", "AppleCollationOrder"],
stderr=open("/dev/null"),
stdout=subprocess.PIPE).communicate()[0]
except:
pass
if not _collation:
try:
_collation=subprocess.Popen(
[defaults, "read", "-g", "AppleCollationOrder"],
stderr=open("/dev/null"),
stdout=subprocess.PIPE).communicate()[0]
except:
pass
if _collation:
if LANG == "C" and not _language and os.path.exists(os.path.join(_gramps_locale, _collation,
"LC_MESSAGES", "gramps.mo")):
LANG = _collation
LC_COLLATE = _collation
if LANG == "C" and not _language:
try:
_locale=subprocess.Popen(
[defaults, "read", "-app", "Gramps", "AppleLocale"],
stderr=open("/dev/null"),
stdout=subprocess.PIPE).communicate()[0]
except:
pass
if not _locale:
try:
_locale=subprocess.Popen(
[defaults, "read", "-g", "AppleLocale"],
stderr=open("/dev/null"),
stdout=subprocess.PIPE).communicate()[0]
except:
pass
if _locale:
if os.path.exists(os.path.join(_gramps_locale, _locale[:5],
"LC_MESSAGES", "gramps.mo")):
LANG = _locale[:5]
elif os.path.exists(os.path.join(_gramps_locale, _locale[:2],
"LC_MESSAGES", "gramps.mo")):
LANG = _locale[:2]
os.environ["LANG"] = LANG
if not _language:
_language = LANG
if LC_COLLATE:
os.environ["LC_COLLATE"] = LC_COLLATE
if _language == "C" or _language == "en":
LC_ALL = "en_US"
elif len(_language) == 2:
LC_ALL = _language + "_" + _language.upper() #Because setlocale gets cranky
#if it only has two letters
else:
LC_ALL = _language
os.environ["LC_ALL"] = LC_ALL #Spell-checker dictionary support
print LANG, LC_ALL
#LaunchServices sticks this argument on the front of argument
#lists. It must make sense to somebody, but Gramps isn't that
#somebody.
for _arg in sys.argv:
if _arg.startswith("-psn"):
sys.argv.remove(_arg)
import gramps