[gcompris] macosx: fixed launchers to properly grab MacOSX locale information



commit 247b18686086384b8d286b30f8d00fc25dd4d4d7
Author: Bruno Coudoin <bruno coudoin gcompris net>
Date:   Tue Jul 10 22:52:26 2012 +0200

    macosx: fixed launchers to properly grab MacOSX locale information
    
    Now GCompris starts in the system language.
    The patch comes from gtk-mac-bundler-0.6.1

 macosx/GComprisAdmin |  168 ++++++++++++++++++++------------------------------
 macosx/launcher.sh   |  168 ++++++++++++++++++++------------------------------
 2 files changed, 136 insertions(+), 200 deletions(-)
---
diff --git a/macosx/GComprisAdmin b/macosx/GComprisAdmin
index caed4f5..5e9124b 100755
--- a/macosx/GComprisAdmin
+++ b/macosx/GComprisAdmin
@@ -10,8 +10,8 @@ else
     EXEC=exec
 fi
 
-name="`basename $0`"
-tmp="`pwd`/$0"
+name=`basename "$0"`
+tmp="$0"
 tmp=`dirname "$tmp"`
 tmp=`dirname "$tmp"`
 # We point to the 'real' bundle relatively to where we are
@@ -42,124 +42,92 @@ export PYTHONPATH=$bundle_lib/python2.7/site-packages:$bundle_lib/python2.7/site
 
 APP=gcompris
 I18NDIR="$bundle_data/locale"
-# 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 "$I18NDIR/${L}/LC_MESSAGES/$APP.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 "$I18NDIR/${L:0:2}/LC_MESSAGES/$APP.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
+# Localization settings. It's better to do this inside your program
+# using NSLocale if possible.
 
-# If we didn't get a language from the language list, try the Collation preference, in case it's the only setting that exists.
+# Set the locale-related variables appropriaty for GnuCash.
+unset LANG LC_MESSAGES LC_MONETARY
+
+# Start by trying 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 "$I18NDIR/${APPLECOLLATION:0:2}/LC_MESSAGES/$APP.mo"; then
-	export LANG=${APPLECOLLATION:0:2}
-    fi
+if test "${APPLECOLLATION}"; then
+  COLL=`ls -d /usr/share/locale/${APPLECOLLATION}*.UTF-8 2>> /dev/null`
+  if test "${COLL}"; then
+    # $COLL is potentially multi-line; concatenate lines by not using quotes.
+    export LANG=`echo ${COLL} | awk '{print $1}' | awk -F/ '{print $5}'`
+  fi
 fi
-if test ! -z $APPLECOLLATION; then
-    export LC_COLLATE=$APPLECOLLATION
-fi
-unset APPLECOLLATION
+unset APPLECOLLATION COLL
 
 # Continue by attempting to find the Locale preference.
 APPLELOCALE=`defaults read .GlobalPreferences AppleLocale`
-
-if test -f "$I18NDIR/${APPLELOCALE:0:5}/LC_MESSAGES/$APP.mo"; then
-    if test -z $LANG; then
-        export LANG="${APPLELOCALE:0:5}"
+if test "${APPLELOCALE}"; then
+    LOCALELANG=`echo "${APPLELOCALE}" | awk -F@ '{print $1".UTF-8"}'`
+    if test -d "/usr/share/locale/${LOCALELANG}"; then
+        export LANG="${LOCALELANG}"
     fi
-
-elif test -z $LANG -a -f "$I18NDIR/${APPLELOCALE:0:2}/LC_MESSAGES/$APP.mo"; then
-    export LANG="${APPLELOCALE:0:2}"
 fi
+unset LOCALELANG
 
-#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???`
-	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"
+# If there is still no locale value, then set US English as a default.
+if test -z "${LANG}"; then
+    export LANG=en_US.UTF-8
 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
+
+# The AppleLocale setting may contain a currency-related substring.
+# Attempt to act on it.
+# First strip the string to just the currency symbol and the language symbol
+APPLECURRENCY=`echo "${APPLELOCALE}" | awk -F= '{print $2}'`
+APPLELANG=`echo "${APPLELOCALE}" | awk -F_ '{print $1}'`
+if test "${APPLECURRENCY}"; then
+    # The user has set a currency different from that of their locale.
+    # Search for a locale that uses that currency, and set LC_MONETARY accordingly.
+
+    # First try to find an LC_MONETARY file that combines the language with the currency.
+    FILES=`find /usr/share/locale/${APPLELANG}*UTF-8 -name LC_MONETARY -exec grep -H $APPLECURRENCY {} \;`
+    if test -z "$FILES"; then
+        # Otherwise try to find any LC_MONETARY file that uses that currency.
+        FILES=`find /usr/share/locale/*UTF-8 -name LC_MONETARY -exec grep -H $APPLECURRENCY {} \;`
     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
+
+    if test "$FILES"; then
+        # We found a locale that matches; set LC_MONETARY.
+        export LC_MONETARY=`echo ${FILES} | awk -F: '{print $1}' | awk -F/ '{print $5}'`
     fi
 fi
-#No currency value means that the AppleLocale governs:
-if test -z "$LC_MONETARY"; then
-    LC_MONETARY=${APPLELOCALE:0:5}
-fi
-#For Gtk, which only looks at LC_ALL:
-export LC_ALL=$LC_MESSAGES
+unset APPLECURRENCY APPLELANG APPLELOCALE FILES
 
-unset APPLELOCALE FILES LOC
+# Has a language ordering been set?
+# If so, set LC_MESSAGES accordingly; otherwise skip it.
+APPLELANGUAGES=`defaults read .GlobalPreferences AppleLanguages | awk 'length > 2' | awk -F, '{print $1}' | sed s/\ //g | sed s/-/_/ | sed s/\"//g`
+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
+        POS=`echo ${L} | awk '{print index(ENVIRON["LANG"], $0)}'`
+        if test $POS -eq 1; then
+            # The language symbol is a subset of the $LANG variable. We're done!
+            break
+        fi
+        # NOTE: the following may fail for the alternate Chinese localizations.
+        LC=`ls -d /usr/share/locale/${L}*.UTF-8 2>> /dev/null`
+        # $LC is potentially multi-line; concatenate lines by not using quotes.
+        if test $LC; then
+            # There is a UTF-8 locale matching this language.
+            export LC_MESSAGES=`echo ${LC} | awk '{print $1}' | awk -F/ '{print $5}'`
+            break
+        fi
+    done  
+fi
+unset APPLELANGUAGES POS LC L
 
 if test -f "$bundle_lib/charset.alias"; then
     export CHARSETALIASDIR="$bundle_lib"
 fi
 
+
 # Extra arguments can be added in environment.sh.
 EXTRA_ARGS=-a
 if test -f "$bundle_res/environment.sh"; then
diff --git a/macosx/launcher.sh b/macosx/launcher.sh
index ba2def7..92dfd92 100755
--- a/macosx/launcher.sh
+++ b/macosx/launcher.sh
@@ -10,8 +10,8 @@ else
     EXEC=exec
 fi
 
-name="`basename $0`"
-tmp="`pwd`/$0"
+name=`basename "$0"`
+tmp="$0"
 tmp=`dirname "$tmp"`
 tmp=`dirname "$tmp"`
 bundle=`dirname "$tmp"`
@@ -41,124 +41,92 @@ export PYTHONPATH=$bundle_lib/python2.7/site-packages:$bundle_lib/python2.7/site
 
 APP=gcompris
 I18NDIR="$bundle_data/locale"
-# 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 "$I18NDIR/${L}/LC_MESSAGES/$APP.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 "$I18NDIR/${L:0:2}/LC_MESSAGES/$APP.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
+# Localization settings. It's better to do this inside your program
+# using NSLocale if possible.
 
-# If we didn't get a language from the language list, try the Collation preference, in case it's the only setting that exists.
+# Set the locale-related variables appropriaty for GnuCash.
+unset LANG LC_MESSAGES LC_MONETARY
+
+# Start by trying 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 "$I18NDIR/${APPLECOLLATION:0:2}/LC_MESSAGES/$APP.mo"; then
-	export LANG=${APPLECOLLATION:0:2}
-    fi
+if test "${APPLECOLLATION}"; then
+  COLL=`ls -d /usr/share/locale/${APPLECOLLATION}*.UTF-8 2>> /dev/null`
+  if test "${COLL}"; then
+    # $COLL is potentially multi-line; concatenate lines by not using quotes.
+    export LANG=`echo ${COLL} | awk '{print $1}' | awk -F/ '{print $5}'`
+  fi
 fi
-if test ! -z $APPLECOLLATION; then
-    export LC_COLLATE=$APPLECOLLATION
-fi
-unset APPLECOLLATION
+unset APPLECOLLATION COLL
 
 # Continue by attempting to find the Locale preference.
 APPLELOCALE=`defaults read .GlobalPreferences AppleLocale`
-
-if test -f "$I18NDIR/${APPLELOCALE:0:5}/LC_MESSAGES/$APP.mo"; then
-    if test -z $LANG; then
-        export LANG="${APPLELOCALE:0:5}"
+if test "${APPLELOCALE}"; then
+    LOCALELANG=`echo "${APPLELOCALE}" | awk -F@ '{print $1".UTF-8"}'`
+    if test -d "/usr/share/locale/${LOCALELANG}"; then
+        export LANG="${LOCALELANG}"
     fi
-
-elif test -z $LANG -a -f "$I18NDIR/${APPLELOCALE:0:2}/LC_MESSAGES/$APP.mo"; then
-    export LANG="${APPLELOCALE:0:2}"
 fi
+unset LOCALELANG
 
-#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 $bundle_res/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 $bundle_res/share/locale -name $LANG???`
-	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"
+# If there is still no locale value, then set US English as a default.
+if test -z "${LANG}"; then
+    export LANG=en_US.UTF-8
 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
+
+# The AppleLocale setting may contain a currency-related substring.
+# Attempt to act on it.
+# First strip the string to just the currency symbol and the language symbol
+APPLECURRENCY=`echo "${APPLELOCALE}" | awk -F= '{print $2}'`
+APPLELANG=`echo "${APPLELOCALE}" | awk -F_ '{print $1}'`
+if test "${APPLECURRENCY}"; then
+    # The user has set a currency different from that of their locale.
+    # Search for a locale that uses that currency, and set LC_MONETARY accordingly.
+
+    # First try to find an LC_MONETARY file that combines the language with the currency.
+    FILES=`find /usr/share/locale/${APPLELANG}*UTF-8 -name LC_MONETARY -exec grep -H $APPLECURRENCY {} \;`
+    if test -z "$FILES"; then
+        # Otherwise try to find any LC_MONETARY file that uses that currency.
+        FILES=`find /usr/share/locale/*UTF-8 -name LC_MONETARY -exec grep -H $APPLECURRENCY {} \;`
     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
+
+    if test "$FILES"; then
+        # We found a locale that matches; set LC_MONETARY.
+        export LC_MONETARY=`echo ${FILES} | awk -F: '{print $1}' | awk -F/ '{print $5}'`
     fi
 fi
-#No currency value means that the AppleLocale governs:
-if test -z "$LC_MONETARY"; then
-    LC_MONETARY=${APPLELOCALE:0:5}
-fi
-#For Gtk, which only looks at LC_ALL:
-export LC_ALL=$LC_MESSAGES
+unset APPLECURRENCY APPLELANG APPLELOCALE FILES
 
-unset APPLELOCALE FILES LOC
+# Has a language ordering been set?
+# If so, set LC_MESSAGES accordingly; otherwise skip it.
+APPLELANGUAGES=`defaults read .GlobalPreferences AppleLanguages | awk 'length > 2' | awk -F, '{print $1}' | sed s/\ //g | sed s/-/_/ | sed s/\"//g`
+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
+        POS=`echo ${L} | awk '{print index(ENVIRON["LANG"], $0)}'`
+        if test $POS -eq 1; then
+            # The language symbol is a subset of the $LANG variable. We're done!
+            break
+        fi
+        # NOTE: the following may fail for the alternate Chinese localizations.
+        LC=`ls -d /usr/share/locale/${L}*.UTF-8 2>> /dev/null`
+        # $LC is potentially multi-line; concatenate lines by not using quotes.
+        if test $LC; then
+            # There is a UTF-8 locale matching this language.
+            export LC_MESSAGES=`echo ${LC} | awk '{print $1}' | awk -F/ '{print $5}'`
+            break
+        fi
+    done  
+fi
+unset APPLELANGUAGES POS LC L
 
 if test -f "$bundle_lib/charset.alias"; then
     export CHARSETALIASDIR="$bundle_lib"
 fi
 
+
 # Extra arguments can be added in environment.sh.
 EXTRA_ARGS=
 if test -f "$bundle_res/environment.sh"; then



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]