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

Re: Language and locale setting in /root/dbootstrap_setting



[Junichi Uekawa]
> Add something like this:

OK.  Second try.  Tests if the locale is valid, generate it if it is
missing, try to set the console charset (assuming it is already
ISO-8859-1), and unset LANG and LANGUAGE if any of this fails.

Would this still break Japanese installs, or should it be safe to
include in base-config?

Index: base-config
===================================================================
RCS file: /cvs/debian-boot/base-config/base-config,v
retrieving revision 1.8
diff -u -3 -p -u -r1.8 base-config
--- base-config	2001/11/19 19:32:52	1.8
+++ base-config	2002/02/24 17:01:55
@@ -12,6 +12,69 @@ fi
 SHELL=/bin/sh
 export SHELL
 
+# Source it here to set LANG and LANGUAGE, to let debconf uses the
+# installation language if possible.  Make sure LANG is a valid locale
+# and try to generate it if it is missing.  Make sure the locale is
+# using a charset supported by the console
+if [ -e /root/dbootstrap_settings ]; then
+	. /root/dbootstrap_settings || true
+fi
+
+# Set console charset if possible, returning true if it is, and false if
+# the supplied charset is unsupported.
+set_console_charset() {
+  case $1 in
+    ISO-8859-1)
+      # Nothing to do, should be supported by default.
+      return 0
+      ;;
+    *)
+      return 1
+      ;;
+  esac
+}
+
+if [ ! -z "$LANG" ]; then
+    validlocale=/usr/sbin/validlocale
+    localegen=/usr/sbin/locale-gen
+    localeconf=/etc/locale.gen
+    tmpfile=/tmp/localeinfo.$$
+
+    LANG=C $validlocale $LANG 2> /dev/null > $tmpfile
+    read locale charset < $tmpfile
+    rm -f $tmpfile
+    unset tmpfile
+
+    if LANG=C $validlocale $LANG > /dev/null 2>&1; then
+	# Valid locale, no need to generate it
+	true
+    else
+	# Hm, should we install the 'locales' package if it is missing?
+	if [ -x $localegen ]; then
+	    echo "$locale $charset" >> $localeconf
+	    $localegen || true
+	else
+	    echo "Package 'locales' not installed.  Unable to generate $LANG"
+	fi
+    fi
+
+    # Make sure the locale is valid and the charset is usable
+    if set_console_charset $charset && \
+       LANG=C $validlocale $LANG > /dev/null 2>&1 ; then
+	if [ ! -z "$LANGUAGE" ]; then
+	    export LANGUAGE
+	fi
+	export LANG
+    else
+	unset LANG
+	unset LANGUAGE
+    fi
+    unset locale
+    unset charset
+    unset localeconf
+    unset localegen
+fi
+
 echo "Configuring the base system..."
 
 cleanup () {
Index: debian/rules
===================================================================
RCS file: /cvs/debian-boot/base-config/debian/rules,v
retrieving revision 1.34
diff -u -3 -p -u -r1.34 rules
--- debian/rules	2001/10/09 19:24:51	1.34
+++ debian/rules	2002/02/24 17:01:56
@@ -29,7 +29,7 @@ install: build
 	dh_testroot
 	dh_clean -k
 	dh_installdirs usr/share/base-config usr/sbin usr/lib/base-config
-	install apt-setup tzsetup base-config termwrap debian/base-config/usr/sbin/
+	install apt-setup tzsetup base-config termwrap validlocale debian/base-config/usr/sbin/
 	install lib/[0-9]* debian/base-config/usr/lib/base-config/
 	cp Mirrors.masterlist debian/base-config/usr/share/base-config/
 
Index: debian/changelog
===================================================================
RCS file: /cvs/debian-boot/base-config/debian/changelog,v
retrieving revision 1.117
diff -u -3 -p -u -r1.117 changelog
--- debian/changelog	2001/12/27 13:27:10	1.117
+++ debian/changelog	2002/02/24 17:01:56
@@ -1,3 +1,9 @@
+base-config (1.34.1) unstable; urgency=low
+
+  * Added initial locale support.
+
+ -- Petter Reinholdtsen <pere@hungry.com>  Sun, 24 Feb 2002 17:40:33 +0100
+
 base-config (1.34) unstable; urgency=low
 
   * Added aptitude as an option, and reworked the package selection stage.
diff -ruN ../empty/validlocale ./validlocale
--- ../empty/validlocale	Thu Jan  1 01:00:00 1970
+++ ./validlocale	Sat Feb 23 11:25:34 2002
@@ -0,0 +1,71 @@
+#!/usr/bin/perl -w
+#
+# Author: Petter Reinholdtsen <pere@hungry.com>
+# Date:   2002-02-23
+#
+# Test if the locale given as argument is a valid locale.  If it
+# isn't, print on stdout the string to add to /etc/locale.gen to make
+# locale-gen generate the locale (if it exists at all).
+
+use POSIX qw(setlocale LC_ALL);
+
+my $debug = 0;
+
+my $defaultcharset = $ENV{"DEFAULTCHARSET"} || "ISO-8859-1";
+
+my $supportedlist = "/usr/share/i18n/SUPPORTED";
+
+unless (defined $ARGV[0]) {
+    usage();
+    exit 1;
+}
+
+my $LANG = $ARGV[0];
+
+my $loc = setlocale(LC_ALL, $LANG);
+if ( ! $loc) {
+    print STDERR "locale '$LANG' not available\n";
+
+    my ($locale)   = $LANG =~ m/^([^.@]+)/;
+    my ($charset)  = $LANG =~ m/^[^.]+\.([^@]+)/;
+    my ($modifier) = $LANG =~ m/(@.+)$/;
+
+    $modifier = "" unless defined $modifier;
+
+    # Hm, if charset is missing, how to we pick the correct one to
+    # use?  Fetching the value from /usr/share/i18n/SUPPORTED should
+    # work on Debian.
+    $charset = get_default_charset("$locale$modifier")
+	unless (defined $charset);
+
+    # print "L: $locale C: $charset M: $modifier\n";
+    print "$locale$modifier $charset\n";
+
+    exit 1;
+} else {
+    print STDERR "locale '$LANG' valid and available\n";
+    exit 0;
+}
+
+sub usage {
+    print "Usage: $0 <locale>\n"
+}
+
+sub get_default_charset {
+    my ($locale) = @_;
+    my ($l, $c);
+    open(SUPPORTED, "< $supportedlist") || die "Unable to open $supportedlist";
+    while (<SUPPORTED>) {
+	chomp;
+	($l, $c) = split(/\s+/);
+	print "Checking '$l' '$c' != '$locale'\n" if $debug;
+	last if  ($l eq $locale);
+    }
+    close(SUPPORTED);
+
+    if ($l eq $locale) {
+	return $c;
+    } else {
+	return $defaultcharset;
+    }
+}
diff -ruN ../empty/validlocale.8 ./validlocale.8
--- ../empty/validlocale.8	Thu Jan  1 01:00:00 1970
+++ ./validlocale.8	Sat Feb 23 11:54:02 2002
@@ -0,0 +1,49 @@
+.TH "validlocale" "8" "0.1" "Petter Reinholdtsen" ""
+.SH "NAME"
+.LP 
+validlocale \- Test if a given locale is available
+.SH "SYNTAX"
+.LP 
+validlocale <\fIlocale\fP>
+.SH "DESCRIPTION"
+.LP 
+Test if the locale given as argument is a valid locale.  If it
+isn't, print on stdout the string to add to /etc/locale.gen to make
+locale\-gen generate the locale (if it exists at all).
+.SH "FILES"
+.LP 
+\fI/usr/sbin/validlocale\fP
+.br 
+\fI/usr/share/i18n/SUPPORTED\fP
+.SH "ENVIRONMENT VARIABLES"
+.LP 
+.TP 
+\fBDEFAULTCHARSET\fP
+Which charset to assume if the given locale is missing from the
+list of supported locales.
+.SH "EXAMPLES"
+.LP 
+If you give a valid locale as parameter, it outputs a string
+specifying this on stderr:
+.LP 
+.IP 
+% validlocale C
+.br 
+locale 'C' valid and available
+.LP 
+When given a invalid (not generated or just nonexistant), it
+outputs a string on stderr telling that this is an invalid locale, and a string to stdout with the string to add to /etc/locale.gen
+to have this locale generated:
+.LP 
+.IP 
+% validlocale de_AU@euro
+.br 
+locale 'de_AT@euro' not available
+.br 
+de_AT@euro ISO\-8859\-15
+.SH "AUTHORS"
+.LP 
+Petter Reinholdtsen <pere@hungry.com>
+.SH "SEE ALSO"
+.LP 
+locale\-gen(8), localedef(1), locale(1), base\-config(8)



Reply to: