Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate host locales in container (fixed issue #1399) #1404

Merged
merged 3 commits into from
Jun 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 34 additions & 15 deletions distrobox-init
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,21 @@ if stat /run/host/etc/shadow > /dev/null &&
rootful=1
fi

# Get host $LANG
if [ -f "/run/host/etc/locale.conf" ]; then
HOST_LOCALE=$(grep -e '^LANG=' /run/host/etc/locale.conf | sed 's/LANG=//' | sed 's/"//g')
HOST_LOCALE_ENCODING=$(echo "${HOST_LOCALE}" | sed -n 's/^[^.]*\.\(.*\)$/\1/p')
HOST_LOCALE_LANG=$(echo "${HOST_LOCALE}" | sed -n 's/^\([^.]*\)\..*$/\1/p')
elif [ -f "/run/host/etc/default/locale" ]; then
HOST_LOCALE=$(grep -e '^LANG=' /run/host/etc/default/locale | sed 's/LANG=//' | sed 's/"//g')
HOST_LOCALE_ENCODING=$(echo "${HOST_LOCALE}" | sed -n 's/^[^.]*\.\(.*\)$/\1/p')
HOST_LOCALE_LANG=$(echo "${HOST_LOCALE}" | sed -n 's/^\([^.]*\)\..*$/\1/p')
fi
# Add fallback values in case host's locale is not set correctly
[ -z "${HOST_LOCALE}" ] && HOST_LOCALE="en_US.UTF-8"
[ -z "${HOST_LOCALE_ENCODING}" ] && HOST_LOCALE_ENCODING="UTF-8 en_US.UTF-8"
[ -z "${HOST_LOCALE_LANG}" ] && HOST_LOCALE_LANG="en_US"

# get_locked_mount_flags will print mount flags considered "locked".
# Arguments:
# src: path to the file/directory
Expand Down Expand Up @@ -617,10 +632,11 @@ if [ "${upgrade}" -ne 0 ] ||

# In case the locale is not available, install it
# will ensure we don't fallback to C.UTF-8
if ! locale -a | grep -qi en_us.utf8; then
if ! locale -a | grep -qi en_us.utf8 || ! locale -a | grep -qi "${HOST_LOCALE}"; then
sed -i "s|#.*en_US.UTF-8|en_US.UTF-8|g" /etc/locale.gen
sed -i "s|#.*${HOST_LOCALE}|${HOST_LOCALE}|g" /etc/locale.gen
locale-gen
update-locale LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8
update-locale LC_ALL="${HOST_LOCALE}" LANG="${HOST_LOCALE}"
dpkg-reconfigure locales
fi

Expand Down Expand Up @@ -713,8 +729,8 @@ if [ "${upgrade}" -ne 0 ] ||

# In case the locale is not available, install it
# will ensure we don't fallback to C.UTF-8
if ! locale -a | grep -qi en_us.utf8; then
LANG=en_US.UTF-8 localedef -i en_US -f UTF-8 en_US.UTF-8
if ! locale -a | grep -qi en_us.utf8 || ! locale -a | grep -qi "${HOST_LOCALE}"; then
LANG="${HOST_LOCALE}" localedef -i "${HOST_LOCALE_LANG}" -f "${HOST_LOCALE_ENCODING}" "${HOST_LOCALE}"
fi

# Ensure we have tzdata installed and populated, sometimes container
Expand Down Expand Up @@ -778,12 +794,13 @@ if [ "${upgrade}" -ne 0 ] ||

# In case the locale is not available, install it
# will ensure we don't fallback to C.UTF-8
if ! locale -a | grep -qi en_us.utf8; then
if ! locale -a | grep -qi en_us.utf8 || ! locale -a | grep -qi "${HOST_LOCALE}"; then
sed -i "s|#.*en_US.UTF-8|en_US.UTF-8|g" /etc/locale.gen
sed -i "s|#.*${HOST_LOCALE}|${HOST_LOCALE}|g" /etc/locale.gen
locale-gen
cat << EOF > /etc/env.d/02locale
LANG=en_US.UTF-8
LC_CTYPE=en_US.UTF-8
LANG=${HOST_LOCALE}
LC_CTYPE=${HOST_LOCALE}
EOF
fi

Expand Down Expand Up @@ -870,8 +887,8 @@ EOF

# In case the locale is not available, install it
# will ensure we don't fallback to C.UTF-8
if ! locale -a | grep -qi en_us.utf8; then
LANG=en_US.UTF-8 localedef -i en_US -f UTF-8 en_US.UTF-8
if ! locale -a | grep -qi en_us.utf8 || ! locale -a | grep -qi "${HOST_LOCALE}"; then
LANG="${HOST_LOCALE}" localedef -i "${HOST_LOCALE_LANG}" -f "${HOST_LOCALE_ENCODING}" "${HOST_LOCALE}"
fi

# Ensure we have tzdata installed and populated, sometimes container
Expand Down Expand Up @@ -904,8 +921,9 @@ EOF

# In case the locale is not available, install it
# will ensure we don't fallback to C.UTF-8
if ! locale -a | grep -qi en_us.utf8; then
if ! locale -a | grep -qi en_us.utf8 || ! locale -a | grep -qi "${HOST_LOCALE}"; then
sed -i "s|#.*en_US.UTF-8|en_US.UTF-8|g" /etc/locale.gen
sed -i "s|#.*${HOST_LOCALE}|${HOST_LOCALE}|g" /etc/locale.gen
locale-gen -a
fi

Expand Down Expand Up @@ -1159,8 +1177,9 @@ EOF

# In case the locale is not available, install it
# will ensure we don't fallback to C.UTF-8
if command -v locale && ! locale -a | grep -qi en_us.utf8; then
if command -v locale && ! locale -a | grep -qi en_us.utf8 || ! locale -a | grep -qi "${HOST_LOCALE}"; then
sed -i "s|#.*en_US.UTF-8|en_US.UTF-8|g" /etc/default/libc-locales
sed -i "s|#.*${HOST_LOCALE}|${HOST_LOCALE}|g" /etc/default/libc-locales
xbps-reconfigure --force glibc-locales
fi

Expand Down Expand Up @@ -1255,8 +1274,8 @@ EOF

# In case the locale is not available, install it
# will ensure we don't fallback to C.UTF-8
if ! locale -a | grep -qi en_us.utf8; then
LANG=en_US.UTF-8 localedef -i en_US -f UTF-8 en_US.UTF-8
if ! locale -a | grep -qi en_us.utf8 || ! locale -a | grep -qi "${HOST_LOCALE}"; then
LANG="${HOST_LOCALE}" localedef -i "${HOST_LOCALE_LANG}" -f "${HOST_LOCALE_ENCODING}" "${HOST_LOCALE}"
fi

# Ensure we have tzdata installed and populated, sometimes container
Expand Down Expand Up @@ -1350,8 +1369,8 @@ EOF

# In case the locale is not available, install it
# will ensure we don't fallback to C.UTF-8
if ! locale -a | grep -qi en_us.utf8; then
LANG=en_US.UTF-8 localedef -i en_US -f UTF-8 en_US.UTF-8 || true
if ! locale -a | grep -qi en_us.utf8 || ! locale -a | grep -qi "${HOST_LOCALE}"; then
LANG="${HOST_LOCALE}" localedef -i "${HOST_LOCALE_LANG}" -f "${HOST_LOCALE_ENCODING}" "${HOST_LOCALE}" || true
fi

# Ensure we have tzdata installed and populated, sometimes container
Expand Down