Bug#856105: Improve locale-gen to accept locales to generate from command line
Package: locales
Version: 2.24.9
Tags: patch
Attached is a patch to allow locale-gen to accept one or more locales
to generate.
One or more locales can be provided, and all of them will be generated
instead of looking in /etc/locale.gen. Locales in arguments will be
uncommented in /etc/locale.gen.
Thanks,
Anthony Wong
From f5357055243813daaf7d9c0f8f7b7819e4eac521 Mon Sep 17 00:00:00 2001
From: Anthony Wong <anthony.wong@ubuntu.com>
Date: Fri, 24 Feb 2017 23:31:05 -0500
Subject: [PATCH] Locales to generate can be provided as command line argument.
One or more locales can be provided, and all of them will be generated
instead of looking in /etc/locale.gen. Locales in arguments will be
uncommented in /etc/locale.gen.
Signed-off-by: Anthony Wong <anthony.wong@ubuntu.com>
---
debian/local/usr_sbin/locale-gen | 43 ++++++++++++++++++++++++++++++++++++++--
1 file changed, 41 insertions(+), 2 deletions(-)
diff --git a/debian/local/usr_sbin/locale-gen b/debian/local/usr_sbin/locale-gen
index 3d13dadf..ff0be194 100755
--- a/debian/local/usr_sbin/locale-gen
+++ b/debian/local/usr_sbin/locale-gen
@@ -5,6 +5,7 @@ set -e
LOCALEGEN=/etc/locale.gen
LOCALES=/usr/share/i18n/locales
USER_LOCALES=/usr/local/share/i18n/locales
+SUPPORTED=/usr/share/i18n/SUPPORTED
if [ -n "$POSIXLY_CORRECT" ]; then
unset POSIXLY_CORRECT
fi
@@ -35,8 +36,46 @@ is_entry_ok() {
fi
}
+add_to_locale_gen() {
+ echo "$1" | while read locale; do
+ if ! grep -q "^[# ]*$locale *\$" $LOCALEGEN; then
+ echo "# $locale" >> $LOCALEGEN
+ fi
+ sed -i -e "0,/^[# ]*$locale *$/ s/^[# ]*$locale *$/$locale/" $LOCALEGEN
+ done
+}
+
+if [ -z "$1" ]; then
+ GENERATE="`cat $LOCALEGEN 2>/dev/null || true`"
+else
+ GENERATE=
+ while [ -n "$1" ]; do
+ LL="`echo "$1" | sed 's/\./ /'`"
+ # Check validity of supplied locale string
+ if ! echo "$1" | grep -q -e "^[a-z]\{2,3\}_[A-Z]\{2\}\(@[a-z0-9]\+\)\{0,1\}\.[A-Z0-9-]\+$" \
+ -e "^[a-z]\{2,3\}_[A-Z]\{2\}$"; then
+ echo "Error: '$1' is not a valid locale format" >&2
+ elif L=`grep "^$1 " $SUPPORTED`; then
+ # for cases that match the complete locale string
+ GENERATE="$GENERATE\n$L"
+ echo "Adding $L"
+ add_to_locale_gen "$L"
+ elif L=`grep "^$LL" $SUPPORTED`; then
+ # for cases like "aa_ER.UTF-8", where the entry is written as "aa_ER UTF-8"
+ GENERATE="$GENERATE\n$L"
+ echo "Adding $L"
+ add_to_locale_gen "$L"
+ else
+ echo "Error: '$1' is not a supported language or locale" >&2
+ fi
+ shift
+ done
+fi
+
+[ -z "$GENERATE" ] && echo "Error: nothing to do" >&2 && exit 1
+
echo "Generating locales (this might take a while)..."
-while read locale charset; do \
+echo "$GENERATE" | sort -u | while read locale charset; do \
case $locale in \#*) continue;; "") continue;; esac; \
is_entry_ok || continue
if [ "$KEEP" ] && PERL_BADLANG=0 perl -MPOSIX -e \
@@ -59,5 +98,5 @@ while read locale charset; do \
fi
localedef -i $input -c -f $charset -A /usr/share/locale/locale.alias $locale || :; \
echo ' done'; \
-done < $LOCALEGEN
+done
echo "Generation complete."
--
2.11.0
Reply to: