Article View: linux.gentoo.dev
Article #1Re: [gentoo-dev] [PATCH v3] locale-utils.eclass: Add eclass to generate locales
From: Sam James
Date: Wed, 07 Oct 2026 23:00
Date: Wed, 07 Oct 2026 23:00
446 lines
15924 bytes
15924 bytes
--=-=-=
Content-Type: text/plain
Matt Turner <mattst88@gentoo.org> writes:
> Test suites, and occasionally builds, call setlocale(3) with specific
> locales and fail if they are not available. Which locales exist is
> system configuration (/etc/locale.gen), so an ebuild can neither depend
> on them nor expect them to be present.
>
> Add elocale_gen, which compiles the requested locales with localedef
> into a directory under ${T} and exports LOCPATH so that glibc finds
> them.
>
> bash itself does not see LOCPATH, and warns that it cannot change locale
> when an ebuild assigns LC_ALL or similar to a locale that exists only in
> that directory. elocale_gen --set exports the variable without the
> warning.
This is great, thank you. I've wanted this many times and didn't think
of it.
>
> Signed-off-by: Matt Turner <mattst88@gentoo.org>
> ---
> eclass/locale-utils.eclass | 245 +++++++++++++++++++++++++++++++++++
> eclass/tests/locale-utils.sh | 143 ++++++++++++++++++++
> 2 files changed, 388 insertions(+)
> create mode 100644 eclass/locale-utils.eclass
> create mode 100755 eclass/tests/locale-utils.sh
>
> diff --git a/eclass/locale-utils.eclass b/eclass/locale-utils.eclass
> new file mode 100644
> index 000000000000..8cf1a2b2640c
> --- /dev/null
> +++ b/eclass/locale-utils.eclass
> @@ -0,0 +1,245 @@
> +# Copyright 2026 Gentoo Authors
> +# Distributed under the terms of the GNU General Public License v2
> +
> +# @ECLASS: locale-utils.eclass
> +# @MAINTAINER:
> +# Matt Turner <mattst88@gentoo.org>
> +# @AUTHOR:
> +# Matt Turner <mattst88@gentoo.org>
> +# @SUPPORTED_EAPIS: 8 9
> +# @BLURB: Generate locales into a private directory
> +# @DESCRIPTION:
> +# Test suites frequently call setlocale(3) with specific locales and fail if
> +# they are not available. Which locales exist is system configuration
> +# (/etc/locale.gen), so an ebuild can neither depend on them nor expect
> +# them to be present. elocale_gen generates them instead.
> +#
> +# Example:
> +# @CODE
> +# inherit locale-utils
> +#
> +# src_test() {
> +# local -x LOCPATH
> +# if elocale_gen cs_CZ.UTF-8 de_DE@euro; then
> +# meson_src_test
> +# else
> +# meson_src_test --no-suite locale
> +# fi
> +# }
> +# @CODE
> +#
> +# If the locale also has to be selected, pass --set instead of assigning the
> +# variable:
> +# @CODE
> +# src_test() {
> +# local -x LOCPATH LC_ALL
> +# elocale_gen --set LC_ALL en_US.UTF-8
> +# cmake_src_test
> +# }
> +# @CODE
> +
> +case ${EAPI} in
> + 8|9) ;;
> + *) die "${ECLASS}: EAPI ${EAPI:-0} not supported" ;;
> +esac
> +
> +if [[ -z ${_LOCALE_UTILS_ECLASS} ]]; then
> +_LOCALE_UTILS_ECLASS=1
> +
> +# @ECLASS_VARIABLE: _LOCALE_UTILS_RE
> +# @INTERNAL
> +# @DESCRIPTION:
> +# Regular expression that splits a locale name into its parts:
> +# language[_territory][.codeset][@modifier]
> +_LOCALE_UTILS_RE='^([^.@]+)(\.([^@]+))?(@.+)?$'
> +
> +# @FUNCTION: _locale-utils_installed
> +# @INTERNAL
> +# @USAGE: <locale>
> +# @DESCRIPTION:
> +# Check whether "locale -a" lists the locale, whichever way the codeset is
> +# spelled on either side.
> +_locale-utils_installed() {
> + local want have codeset
> +
> + [[ ${1} =~ ${_LOCALE_UTILS_RE} ]] || return 1
> + codeset=${BASH_REMATCH[3]//[^[:alnum:]]}
> + want=${BASH_REMATCH[1]}${codeset:+.${codeset,,}}${BASH_REMATCH[4]}
> +
> + while read -r have; do
> + [[ ${have} =~ ${_LOCALE_UTILS_RE} ]] || continue
> + codeset=${BASH_REMATCH[3]//[^[:alnum:]]}
> + have=${BASH_REMATCH[1]}${codeset:+.${codeset,,}}${BASH_REMATCH[4]}
> + [[ ${have} == "${want}" ]] && return 0
> + done < <(locale -a 2>/dev/null)
> +
> + return 1
> +}
> +
> +# @FUNCTION: _locale-utils_gen
> +# @INTERNAL
> +# @USAGE: <log command> <directory> <locale>
> +# @DESCRIPTION:
> +# Compile a single locale into the directory, unless it is already there.
> +# Returns non-zero if glibc does not support the locale or if localedef fails,
> +# after reporting the reason using the log command, e.g. eerror or debug-print.
> +_locale-utils_gen() {
> + local log=${1} dir=${2} name=${3}
> + local re=${_LOCALE_UTILS_RE}
> +
> + if [[ ! ${name} =~ ${re} ]]; then
> + ${log} "${FUNCNAME}: ${name}: not a valid locale name"
> + return 1
> + fi
> + local lang=${BASH_REMATCH[1]} modifier=${BASH_REMATCH[4]}
> + local codeset=${BASH_REMATCH[3]//[^[:alnum:]]}
> + codeset=${codeset,,}
> + # A codeset without a single letter or digit, which would otherwise be
> + # taken for no codeset at all.
> + if [[ -n ${BASH_REMATCH[3]} && -z ${codeset} ]]; then
> + ${log} "${FUNCNAME}: ${name}: not a valid locale name"
> + return 1
> + fi
> +
> + # glibc looks a locale up both under the name it was asked for and
> + # under that name with the codeset normalized, e.g. cs_CZ.UTF-8 and
> + # cs_CZ.utf8. Using the normalized name makes every spelling work.
> + local out=${dir}/${lang}${codeset:+.${codeset}}${modifier}
> +
> + [[ -d ${out} ]] && return 0
> +
> + local supported=${BROOT}/usr/share/i18n/SUPPORTED
> + local entry charmap normalized found
> + while read -r entry charmap; do
> + if [[ ${entry} =~ ${re} && ${BASH_REMATCH[1]} == "${lang}" &&
> + ${BASH_REMATCH[4]} == "${modifier}" ]]
> + then
> + if [[ -n ${codeset} ]]; then
> + # Compare against the charmap rather than the name, so
> + # that en_US.ISO-8859-1 finds the "en_US ISO-8859-1"
> + # entry.
> + normalized=${charmap//[^[:alnum:]]}
> + [[ ${normalized,,} == "${codeset}" ]] || continue
> + else
> + # Without a codeset, only the entry that has none in
> + # its name matches, e.g. "en_US ISO-8859-1" and not
> + # "en_US.UTF-8 UTF-8".
> + [[ -z ${BASH_REMATCH[3]} ]] || continue
> + fi
> + found=1
> + break
> + fi
> + done < "${supported}"
> +
> + if [[ -z ${found} ]]; then
> + ${log} "${FUNCNAME}: ${name}: not listed in ${supported}"
> + return 1
> + fi
> +
> + local msg line
> + if ! msg=$(localedef -i "${lang}${modifier}" -f "${charmap}" "${out}" 2>&1)
> + then
> + rm -rf "${out}" || die
> + ${log} "${FUNCNAME}: ${name}: localedef failed:"
> + while read -r line; do
> + ${log} "${line}"
> + done <<< "${msg}"
> + return 1
> + fi
> +}
> +
> +# @FUNCTION: elocale_gen
> +# @USAGE: [--set <variable>] <locale>...
> +# @RETURN: 0 if the locales are available, 1 if they are not
> +# @DESCRIPTION:
> +# Generate the given locales into a directory under ${T} and export LOCPATH
> +# so that programs run afterwards can use them. Locales are named as in
> +# /usr/share/i18n/SUPPORTED, e.g. cs_CZ.UTF-8, en_US or de_DE@euro. Other
> +# spellings of the codeset, e.g. cs_CZ.utf8, are accepted.
> +#
> +# Setting LOCPATH hides the system's locales, so the locales in use by the
> +# environment and C.UTF-8 are generated as well. Any other locale that is
> +# needed must be requested, even if it happens to be installed.
> +#
> +# "locale -a" does not honor LOCPATH and never lists the generated locales.
> +#
> +# Dies if a locale cannot be generated, unless called under nonfatal.
> +#
> +# Only glibc can compile locales. With any other libc nothing is generated
> +# and LOCPATH is left alone. Returns 0 if "locale -a" lists all of the given
> +# locales, e.g. because sys-apps/musl-locales provides them, and otherwise 1
> +# without dying, so that the caller can skip the affected tests.
> +#
> +# With --set, the variable, which must be LANG or one of LC_*, is exported
> +# with the first locale as its value. Use this rather than assigning the
> +# variable. LOCPATH only takes effect in programs started afterwards, so bash
> +# itself cannot find the generated locales and warns that it cannot change
> +# locale whenever such a variable is assigned. For the same reason, bash keeps
> +# using its previous locale. If the libc is not glibc, the variable is
> +# exported whether or not the locale is available.
> +#
> +# LOCPATH is saved with the rest of the environment, and the directory is
> +# gone by the time pkg_prerm and pkg_postrm run. Declare it local in the
> +# calling phase, as in the examples, along with the variable given to --set,
> +# which would otherwise cause the warning again whenever the environment is
> +# loaded. If the locales are needed in several phases, call elocale_gen in
> +# each of them. Locales that are already there are not generated again.
> +# pkg_setup is not suitable, since it runs as root and for binary packages.
> +#
> +# May be called more than once. Locales accumulate.
> +elocale_gen() {
> + debug-print-function ${FUNCNAME} "$@"
> +
> + local setvar
> + if [[ ${1} == --set ]]; then
> + setvar=${2}
> + [[ ${setvar} =~ ^(LANG|LC_[A-Z]+)$ ]] ||
> + die "${FUNCNAME}: --set needs LANG or LC_*, not '${setvar}'"
> + shift 2
> + fi
> +
> + [[ $# -ge 1 ]] || die "${FUNCNAME}: at least one locale needed"
> +
> + if ! use elibc_glibc; then
> + [[ -n ${setvar} ]] && export "${setvar}=${1}"
> +
> + local name
> + for name; do
> + if ! _locale-utils_installed "${name}"; then
> + debug-print "${FUNCNAME}: ${name}: not listed by locale -a"
> + return 1
> + fi
> + done
> + return 0
> + fi
> +
> + local dir=${T}/locale
> + mkdir -p "${dir}" || die
> +
> + local name var
> + for var in LANG "${!LC_@}"; do
> + name=${!var}
> + [[ -z ${name} || ${name} == @(C|POSIX) ]] && continue
> + # Best effort. The locale may not be available on the system
> + # either, in which case nothing is lost.
> + _locale-utils_gen debug-print "${dir}" "${name}"
> + done
> +
> + for name in C.UTF-8 "$@"; do
> + einfo "Generating locale ${name}"
> + if ! _locale-utils_gen eerror "${dir}" "${name}"; then
> + die -n "${FUNCNAME}: failed to generate locale ${name}"
> + return 1
> + fi
> + done
> +
> + export LOCPATH=${dir}
> + if [[ -n ${setvar} ]]; then
> + # bash does not see LOCPATH and warns if the locale is not
> + # installed on the system.
> + export "${setvar}=${1}" 2>/dev/null
> + fi
> + return 0
> +}
> +
> +fi
> diff --git a/eclass/tests/locale-utils.sh b/eclass/tests/locale-utils.sh
> new file mode 100755
> index 000000000000..ad52fa356b9c
> --- /dev/null
> +++ b/eclass/tests/locale-utils.sh
> @@ -0,0 +1,143 @@
> +#!/bin/bash
> +# Copyright 2026 Gentoo Authors
> +# Distributed under the terms of the GNU General Public License v2
> +
> +EAPI=8
> +source tests-common.sh || exit
> +
> +inherit locale-utils
> +
> +if ! type -P localedef >/dev/null; then
> + ewarn "localedef not found, skipping tests"
> + texit
> +fi
> +
> +IUSE="elibc_glibc"
> +BROOT=
> +mkdir -p "${T}" || die
> +
> +# Print the charmap of a locale, or nothing if the locale is unavailable.
> +charmap() {
> + local out
> + out=$(LC_ALL=${1} locale charmap 2>&1) || return
> + [[ ${out} == *$'\n'* ]] || echo "${out}"
> +}
> +
> +test_locale() {
> + local name=${1} exp=${2}
> + local have
> + tbegin "${name} -> ${exp:-unavailable}"
> + have=$(charmap "${name}")
> + [[ ${have} == "${exp}" ]]
> + tend $? "charmap: ${have:-unavailable}"
> +}
> +
> +tbegin "elocale_gen cs_CZ.UTF-8 en_US de_DE@euro ja_JP.eucjp"
> +LANG=sv_SE.utf8 elocale_gen cs_CZ.UTF-8 en_US de_DE@euro ja_JP.eucjp >/dev/null
> +tend $?
> +
> +tbegin "LOCPATH is exported"
> +[[ $(declare -p LOCPATH 2>/dev/null) == "declare -x LOCPATH=\"${T}/locale\"" ]]
> +tend $?
> +
> +test_locale cs_CZ.UTF-8 UTF-8
> +test_locale cs_CZ.utf8 UTF-8
> +test_locale en_US ISO-8859-1
> +test_locale de_DE@euro ISO-8859-15
> +test_locale ja_JP.EUC-JP EUC-JP
> +test_locale C.UTF-8 UTF-8
> +# taken from the environment
> +test_locale sv_SE.UTF-8 UTF-8
> +# never requested
> +test_locale fr_FR.UTF-8 ""
> +
> +tbegin "elocale_gen accumulates"
> +elocale_gen en_US.ISO-8859-1 >/dev/null
> +tend $?
> +test_locale en_US.iso88591 ISO-8859-1
> +test_locale cs_CZ.UTF-8 UTF-8
> +
> +tbegin "a local LOCPATH does not outlive the caller"
> +unset LOCPATH
> +scoped() {
> + local -x LOCPATH
> + elocale_gen cs_CZ.UTF-8 >/dev/null && [[ ${LOCPATH} == "${T}/locale" ]]
> +}
> +scoped && [[ ! -v LOCPATH ]]
> +tend $?
> +
> +tbegin "--set exports the variable without a warning from bash"
> +set_scoped() {
> + local -x LOCPATH LC_ALL
> + elocale_gen --set LC_ALL cs_CZ.UTF-8 de_DE@euro >/dev/null &&
> + [[ $(declare -p LC_ALL) == 'declare -x LC_ALL="cs_CZ.UTF-8"' ]] &&
> + [[ $(locale charmap) == UTF-8 ]]
> +}
> +set_scoped 2> "${T}/stderr" && [[ ! -s ${T}/stderr && ! -v LC_ALL ]]
> +tend $?
> +
> +tbegin "--set rejects other variables"
> +! (elocale_gen --set PATH cs_CZ.UTF-8) &>/dev/null
> +tend $?
> +
> +tbegin "unknown locale fails"
> +! (elocale_gen xx_XX.UTF-8) &>/dev/null
> +tend $?
> +
> +tbegin "codeset without letters or digits fails"
> +! (elocale_gen en_US.-) &>/dev/null
> +tend $?
> +
> +# What sys-apps/musl-locales would list.
> +musl_locale() {
> + [[ ${1} == -a ]] || return 1
> + printf '%s\n' C C.UTF-8 cs_CZ.UTF-8 de_DE.UTF-8
> +}
> +
> +tbegin "non-glibc returns 0 if the system has the locales"
> +(
> + IUSE=
> + locale() { musl_locale "$@"; }
> + elocale_gen cs_CZ.utf8 de_DE.UTF-8 C.UTF-8 && [[ ! -v LOCPATH ]]
> +)
> +tend $?
> +
> +tbegin "non-glibc returns 1 if a locale is missing"
> +(
> + IUSE=
> + locale() { musl_locale "$@"; }
> + elocale_gen cs_CZ.UTF-8 fr_FR.UTF-8
> + [[ $? -eq 1 ]]
> +)
> +tend $?
> +
> +tbegin "non-glibc does not take cs_CZ.UTF-8 for cs_CZ"
> +(
> + IUSE=
> + locale() { musl_locale "$@"; }
> + elocale_gen cs_CZ
> + [[ $? -eq 1 ]]
> +)
> +tend $?
> +
> +tbegin "non-glibc returns 1 without a locale command"
> +(
> + IUSE=
> + locale() { return 127; }
> + elocale_gen C.UTF-8
> + [[ $? -eq 1 ]]
> +)
> +tend $?
> +
> +tbegin "non-glibc still exports the variable"
> +(
> + IUSE=
> + unset LC_ALL
> + locale() { musl_locale "$@"; }
> + # bash on glibc warns here. It does not with the libcs this is for.
> + elocale_gen --set LC_ALL fr_FR.UTF-8 2>/dev/null
> + [[ $? -eq 1 && $(declare -p LC_ALL) == 'declare -x LC_ALL="fr_FR.UTF-8"' ]]
> +)
> +tend $?
> +
> +texit
--=-=-=
Content-Type: application/pgp-signature; name="signature.asc"
-----BEGIN PGP SIGNATURE-----
iQEBBAEWCgCpFiEEJaa7iN2bdkxrVUHCc4QJ9SDfkZAFAmrGznAbFIAAAAAABAAO
bWFudTIsMi41KzEuMTIsMiwyXxSAAAAAAC4AKGlzc3Vlci1mcHJAbm90YXRpb25z
Lm9wZW5wZ3AuZmlmdGhob3JzZW1hbi5uZXQyNUE2QkI4OEREOUI3NjRDNkI1NTQx
QzI3Mzg0MDlGNTIwREY5MTkwDxxzYW1AZ2VudG9vLm9yZwAKCRBzhAn1IN+RkOwd
AQCYBXfz2ErIml9GE7XKHLECzKGwnoM+K1V/37gRCwz4ewD/aG5hGHZAU5nCwQws
+Y2BnaUZAaXmt+9Nl3fOSKg+TgM=
=f8xK
-----END PGP SIGNATURE-----
--=-=-=--
Technical Details
Message-ID:
<NLIM9-49Rx-3@gated-at.bofh.it>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!news.samoylyk.net!gothmog.csi.it!bofh.it!news.nic.it!robomodReferences:
<NKVBL-3K11-9@gated-at.bofh.it>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!news.samoylyk.net!gothmog.csi.it!bofh.it!news.nic.it!robomod From: Sam James <sam@gentoo.org> Newsgroups: linux.gentoo.dev Subject: Re: [gentoo-dev] [PATCH v3] locale-utils.eclass: Add eclass to generate locales Date: Thu, 08 Oct 2026 01:00:01 +0200 Message-ID: <NLIM9-49Rx-3@gated-at.bofh.it> References: <NKVBL-3K11-9@gated-at.bofh.it> Organization: Gentoo User-Agent: mu4e 1.14.3; emacs 32.0.50 List-ID: Gentoo Linux mail <gentoo-dev.gentoo.org> Reply-To: gentoo-dev@lists.gentoo.org X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply MIME-Version: 1.0 Content-Type: multipart/signed; boundary="=-=-="; micalg=pgp-sha512; protocol="application/pgp-signature" Approved: robomod@news.nic.it Lines: 446 Sender: robomod@news.nic.it X-Original-Cc: Matt Turner <mattst88@gentoo.org> X-Original-Date: Wed, 07 Oct 2026 23:57:52 +0100 X-Original-Message-ID: <87v77d3yq7.fsf@gentoo.org> X-Original-References: <20261005182738.3735404-1-mattst88@gentoo.org>