1 # Copyright 1999-2018 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
6 # Ulrich Müller <ulm@gentoo.org>
8 # Ben de Groot <yngwin@gentoo.org>
9 # @BLURB: convenience functions to handle localizations
11 # The l10n (localization) eclass offers a number of functions to more
12 # conveniently handle localizations (translations) offered by packages.
13 # These are meant to prevent code duplication for such boring tasks as
14 # determining the cross-section between the user's set LINGUAS and what
15 # is offered by the package.
17 # @ECLASS-VARIABLE: PLOCALES
20 # Variable listing the locales for which localizations are offered by
23 # Example: PLOCALES="cy de el_GR en_US pt_BR vi zh_CN"
25 # @ECLASS-VARIABLE: PLOCALE_BACKUP
28 # In some cases the package fails when none of the offered PLOCALES are
29 # selected by the user. In that case this variable should be set to a
30 # default locale (usually 'en' or 'en_US') as backup.
32 # Example: PLOCALE_BACKUP="en_US"
34 # @FUNCTION: l10n_for_each_locale_do
37 # Convenience function for processing localizations. The parameter should
38 # be a function (defined in the consuming eclass or ebuild) which takes
39 # an individual localization as (last) parameter.
41 # Example: l10n_for_each_locale_do install_locale
42 l10n_for_each_locale_do() {
44 locs=$(l10n_get_locales)
46 "${@}" ${x} || die "failed to process enabled ${x} locale"
50 # @FUNCTION: l10n_for_each_disabled_locale_do
53 # Complementary to l10n_for_each_locale_do, this function will process
54 # locales that are disabled. This could be used for example to remove
55 # locales from a Makefile, to prevent them from being built needlessly.
56 l10n_for_each_disabled_locale_do() {
58 locs=$(l10n_get_locales disabled)
60 "${@}" ${x} || die "failed to process disabled ${x} locale"
64 # @FUNCTION: l10n_find_plocales_changes
65 # @USAGE: <translations dir> <filename pre pattern> <filename post pattern>
67 # Ebuild maintenance helper function to find changes in package offered
68 # locales when doing a version bump. This could be added for example to
71 # Example: l10n_find_plocales_changes "${S}/src/translations" "${PN}_" '.ts'
72 l10n_find_plocales_changes() {
73 [[ $# -ne 3 ]] && die "Exactly 3 arguments are needed!"
74 ebegin "Looking in ${1} for new locales"
75 pushd "${1}" >/dev/null || die "Cannot access ${1}"
77 for x in ${2}*${3} ; do
83 # RHS will be sorted with single spaces so ensure the LHS is too
84 # before attempting to compare them for equality. See bug #513242.
85 # Run them both through the same sorting algorithm so we don't have
86 # to worry about them being the same.
87 if [[ "$(printf '%s\n' ${PLOCALES} | LC_ALL=C sort)" != "$(printf '%s\n' ${current} | LC_ALL=C sort)" ]] ; then
88 eend 1 "There are changes in locales! This ebuild should be updated to:"
89 eerror "PLOCALES=\"${current%[[:space:]]}\""
96 # @FUNCTION: l10n_get_locales
99 # Determine which LINGUAS the user has enabled that are offered by the
100 # package, as listed in PLOCALES, and return them. In case no locales
101 # are selected, fall back on PLOCALE_BACKUP. When the disabled argument
102 # is given, return the disabled locales instead of the enabled ones.
105 if [[ -z ${LINGUAS+set} ]]; then
106 # enable all if unset
109 for loc in ${LINGUAS}; do
110 has ${loc} ${PLOCALES} && locs+="${loc} "
113 [[ -z ${locs} ]] && locs=${PLOCALE_BACKUP}
114 if [[ ${1} == disabled ]]; then
116 for loc in ${PLOCALES}; do
117 has ${loc} ${locs} || disabled_locs+="${loc} "
119 locs=${disabled_locs}
121 printf "%s" "${locs}"