Gentoo Archives: gentoo-dev

From: "Ulrich Müller" <ulm@g.o>
To: gentoo-dev@l.g.o
Cc: "Ulrich Müller" <ulm@g.o>
Subject: [gentoo-dev] [PATCH v2 1/2] plocale.eclass: Copied from l10n.eclass
Date: Mon, 28 Jun 2021 06:48:35
Message-Id: 20210628064746.27590-1-ulm@gentoo.org
In Reply to: [gentoo-dev] [PATCH 2/3] eclass/plocale.eclass: Copied from l10n.eclass by "Ulrich Müller"
1 Ever since the L10N USE_EXPAND variable was introduced, the name of
2 this eclass was somewhat confusing, because it operates on LINGUAS and
3 is unrelated to L10N. Take the EAPI 8 bump as an opportunity to rename
4 the eclass.
5
6 Signed-off-by: Ulrich Müller <ulm@g.o>
7 ---
8 v2: Expanded eclass documentation
9
10 eclass/plocale.eclass | 169 ++++++++++++++++++++++++++++++++++++++++++
11 1 file changed, 169 insertions(+)
12 create mode 100644 eclass/plocale.eclass
13
14 diff --git a/eclass/plocale.eclass b/eclass/plocale.eclass
15 new file mode 100644
16 index 000000000000..3a7b78e93ff2
17 --- /dev/null
18 +++ b/eclass/plocale.eclass
19 @@ -0,0 +1,169 @@
20 +# Copyright 2012-2021 Gentoo Authors
21 +# Distributed under the terms of the GNU General Public License v2
22 +
23 +# @ECLASS: plocale.eclass
24 +# @MAINTAINER:
25 +# Ulrich Müller <ulm@g.o>
26 +# @AUTHOR:
27 +# Ben de Groot <yngwin@g.o>
28 +# @SUPPORTED_EAPIS: 6 7 8
29 +# @BLURB: convenience functions to handle localizations
30 +# @DESCRIPTION:
31 +# The plocale (localization) eclass offers a number of functions to more
32 +# conveniently handle localizations (translations) offered by packages.
33 +# These are meant to prevent code duplication for such boring tasks as
34 +# determining the cross-section between the user's set LINGUAS and what
35 +# is offered by the package.
36 +#
37 +# Typical usage in an ebuild looks like this:
38 +#
39 +# @CODE
40 +# PLOCALES="de en fr pt_BR zh_CN"
41 +# PLOCALE_BACKUP="en"
42 +# @CODE
43 +#
44 +# There, PLOCALES is the list of locales that the package supports.
45 +# PLOCALE_BACKUP is optional and specifies a single locale, which is
46 +# used as a fallback if none of the PLOCALES matches the user's
47 +# LINGUAS selection.
48 +#
49 +# The eclass functions then operate on the intersection of the package's
50 +# PLOCALES with the user's LINGUAS setting. (As a special case, if the
51 +# LINGUAS variable is unset then all items in PLOCALES will be used,
52 +# emulating the behaviour of gettext.)
53 +#
54 +# In the following simple example, locale specific README files
55 +# (e.g. README.de, README.en) are added to the DOCS variable:
56 +#
57 +# @CODE
58 +# my_add_to_docs() {
59 +# DOCS+=( ${1}.${2} )
60 +# }
61 +# plocale_for_each_locale my_add_to_docs README
62 +# @CODE
63 +#
64 +# A complementary function plocale_for_each_disabled_locale exists as
65 +# well, which operates on the set difference of PLOCALES and LINGUAS,
66 +# i.e. on the locales that the user hasn't enabled. For example, it can
67 +# be used to remove unnecessary locale files.
68 +#
69 +# Finally, plocale_find_changes is purely a helper function for ebuild
70 +# maintenance. It can be used to scan a directory for available
71 +# translations and check if the ebuild's PLOCALES are still up to date.
72 +
73 +case ${EAPI} in
74 + 6|7|8) ;;
75 + *) die "${ECLASS}: EAPI ${EAPI:-0} not supported" ;;
76 +esac
77 +
78 +if [[ -z ${_PLOCALE_ECLASS} ]]; then
79 +_PLOCALE_ECLASS=1
80 +
81 +# @ECLASS-VARIABLE: PLOCALES
82 +# @DEFAULT_UNSET
83 +# @DESCRIPTION:
84 +# Variable listing the locales for which localizations are offered by
85 +# the package.
86 +#
87 +# Example: PLOCALES="cy de el_GR en_US pt_BR vi zh_CN"
88 +
89 +# @ECLASS-VARIABLE: PLOCALE_BACKUP
90 +# @DEFAULT_UNSET
91 +# @DESCRIPTION:
92 +# In some cases the package fails when none of the offered PLOCALES are
93 +# selected by the user. In that case this variable should be set to a
94 +# default locale (usually 'en' or 'en_US') as backup.
95 +#
96 +# Example: PLOCALE_BACKUP="en_US"
97 +
98 +# @FUNCTION: plocale_for_each_locale
99 +# @USAGE: <function> [<args>...]
100 +# @DESCRIPTION:
101 +# Convenience function for processing all enabled localizations.
102 +# The parameter should be a function (defined in the consuming eclass
103 +# or ebuild) which takes an individual locale as its (last) parameter.
104 +#
105 +# Example: plocale_for_each_locale install_locale
106 +plocale_for_each_locale() {
107 + local locs x
108 + locs=$(plocale_get_locales)
109 + for x in ${locs}; do
110 + "$@" ${x} || die "failed to process enabled ${x} locale"
111 + done
112 +}
113 +
114 +# @FUNCTION: plocale_for_each_disabled_locale
115 +# @USAGE: <function> [<args>...]
116 +# @DESCRIPTION:
117 +# Complementary to plocale_for_each_locale, this function will process
118 +# locales that are disabled. This could be used for example to remove
119 +# locales from a Makefile, to prevent them from being built needlessly.
120 +plocale_for_each_disabled_locale() {
121 + local locs x
122 + locs=$(plocale_get_locales disabled)
123 + for x in ${locs}; do
124 + "$@" ${x} || die "failed to process disabled ${x} locale"
125 + done
126 +}
127 +
128 +# @FUNCTION: plocale_find_changes
129 +# @USAGE: <translations dir> <filename pre pattern> <filename post pattern>
130 +# @DESCRIPTION:
131 +# Ebuild maintenance helper function to find changes in package offered
132 +# locales when doing a version bump. This could be added for example
133 +# to src_prepare.
134 +#
135 +# Example: plocale_find_changes "${S}/src/translations" "${PN}_" '.ts'
136 +plocale_find_changes() {
137 + [[ $# -eq 3 ]] || die "Exactly 3 arguments are needed!"
138 + ebegin "Looking in ${1} for new locales"
139 + pushd "${1}" >/dev/null || die "Cannot access ${1}"
140 + local current="" x
141 + for x in ${2}*${3}; do
142 + x=${x#"${2}"}
143 + x=${x%"${3}"}
144 + current+="${x} "
145 + done
146 + popd >/dev/null || die
147 + # RHS will be sorted with single spaces so ensure the LHS is too
148 + # before attempting to compare them for equality. See bug #513242.
149 + # Run them both through the same sorting algorithm so we don't have
150 + # to worry about them being the same.
151 + [[ "$(printf '%s\n' ${PLOCALES} | LC_ALL=C sort)" \
152 + == "$(printf '%s\n' ${current} | LC_ALL=C sort)" ]]
153 + if ! eend $? "There are changes in locales!"; then
154 + eerror "This ebuild should be updated to:"
155 + eerror "PLOCALES=\"${current%[[:space:]]}\""
156 + return 1
157 + fi
158 +}
159 +
160 +# @FUNCTION: plocale_get_locales
161 +# @USAGE: [disabled]
162 +# @DESCRIPTION:
163 +# Determine which LINGUAS the user has enabled that are offered by the
164 +# package, as listed in PLOCALES, and return them. In case no locales
165 +# are selected, fall back on PLOCALE_BACKUP. When the disabled argument
166 +# is given, return the disabled locales instead of the enabled ones.
167 +plocale_get_locales() {
168 + local loc locs
169 + if [[ -z ${LINGUAS+set} ]]; then
170 + # enable all if unset
171 + locs=${PLOCALES}
172 + else
173 + for loc in ${LINGUAS}; do
174 + has ${loc} ${PLOCALES} && locs+="${loc} "
175 + done
176 + fi
177 + [[ -z ${locs} ]] && locs=${PLOCALE_BACKUP}
178 + if [[ ${1} == disabled ]]; then
179 + local disabled_locs
180 + for loc in ${PLOCALES}; do
181 + has ${loc} ${locs} || disabled_locs+="${loc} "
182 + done
183 + locs=${disabled_locs}
184 + fi
185 + printf "%s" "${locs}"
186 +}
187 +
188 +fi
189 --
190 2.32.0