dev-cpp/pangomm: stable 2.42.1 for hppa, bug #717144
[gentoo.git] / eclass / l10n.eclass
1 # Copyright 1999-2018 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3
4 # @ECLASS: l10n.eclass
5 # @MAINTAINER:
6 # Ulrich Müller <ulm@gentoo.org>
7 # @AUTHOR:
8 # Ben de Groot <yngwin@gentoo.org>
9 # @BLURB: convenience functions to handle localizations
10 # @DESCRIPTION:
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.
16
17 # @ECLASS-VARIABLE: PLOCALES
18 # @DEFAULT_UNSET
19 # @DESCRIPTION:
20 # Variable listing the locales for which localizations are offered by
21 # the package.
22 #
23 # Example: PLOCALES="cy de el_GR en_US pt_BR vi zh_CN"
24
25 # @ECLASS-VARIABLE: PLOCALE_BACKUP
26 # @DEFAULT_UNSET
27 # @DESCRIPTION:
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.
31 #
32 # Example: PLOCALE_BACKUP="en_US"
33
34 # @FUNCTION: l10n_for_each_locale_do
35 # @USAGE: <function>
36 # @DESCRIPTION:
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.
40 #
41 # Example: l10n_for_each_locale_do install_locale
42 l10n_for_each_locale_do() {
43         local locs x
44         locs=$(l10n_get_locales)
45         for x in ${locs}; do
46                 "${@}" ${x} || die "failed to process enabled ${x} locale"
47         done
48 }
49
50 # @FUNCTION: l10n_for_each_disabled_locale_do
51 # @USAGE: <function>
52 # @DESCRIPTION:
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() {
57         local locs x
58         locs=$(l10n_get_locales disabled)
59         for x in ${locs}; do
60                 "${@}" ${x} || die "failed to process disabled ${x} locale"
61         done
62 }
63
64 # @FUNCTION: l10n_find_plocales_changes
65 # @USAGE: <translations dir> <filename pre pattern> <filename post pattern>
66 # @DESCRIPTION:
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
69 # src_prepare
70 #
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}"
76         local current= x=
77         for x in ${2}*${3} ; do
78                 x=${x#"${2}"}
79                 x=${x%"${3}"}
80                 current+="${x} "
81         done
82         popd >/dev/null
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:]]}\""
90                 return 1
91         else
92                 eend 0
93         fi
94 }
95
96 # @FUNCTION: l10n_get_locales
97 # @USAGE: [disabled]
98 # @DESCRIPTION:
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.
103 l10n_get_locales() {
104         local loc locs
105         if [[ -z ${LINGUAS+set} ]]; then
106                 # enable all if unset
107                 locs=${PLOCALES}
108         else
109                 for loc in ${LINGUAS}; do
110                         has ${loc} ${PLOCALES} && locs+="${loc} "
111                 done
112         fi
113         [[ -z ${locs} ]] && locs=${PLOCALE_BACKUP}
114         if [[ ${1} == disabled ]]; then
115                 local disabled_locs
116                 for loc in ${PLOCALES}; do
117                         has ${loc} ${locs} || disabled_locs+="${loc} "
118                 done
119                 locs=${disabled_locs}
120         fi
121         printf "%s" "${locs}"
122 }