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