kde-plasma/breeze-gtk: x86 stable wrt bug #613144
[gentoo.git] / eclass / l10n.eclass
1 # Copyright 1999-2016 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3
4 # @ECLASS: l10n.eclass
5 # @MAINTAINER:
6 # Ben de Groot <yngwin@gentoo.org>
7 # @BLURB: convenience functions to handle localizations
8 # @DESCRIPTION:
9 # The l10n (localization) eclass offers a number of functions to more
10 # conveniently handle localizations (translations) offered by packages.
11 # These are meant to prevent code duplication for such boring tasks as
12 # determining the cross-section between the user's set LINGUAS and what
13 # is offered by the package; and generating the right list of linguas_*
14 # USE flags.
15
16 # @ECLASS-VARIABLE: PLOCALES
17 # @DEFAULT_UNSET
18 # @DESCRIPTION:
19 # Variable listing the locales for which localizations are offered by
20 # the package. Check profiles/desc/linguas.desc to see if the locales
21 # are listed there. Add any missing ones there.
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 # Add linguas useflags
35 for u in ${PLOCALES}; do
36         IUSE+=" linguas_${u}"
37 done
38
39 # @FUNCTION: l10n_for_each_locale_do
40 # @USAGE: <function>
41 # @DESCRIPTION:
42 # Convenience function for processing localizations. The parameter should
43 # be a function (defined in the consuming eclass or ebuild) which takes
44 # an individual localization as (last) parameter.
45 #
46 # Example: l10n_for_each_locale_do install_locale
47 l10n_for_each_locale_do() {
48         local locs x
49         locs=$(l10n_get_locales)
50         for x in ${locs}; do
51                 "${@}" ${x} || die "failed to process enabled ${x} locale"
52         done
53 }
54
55 # @FUNCTION: l10n_for_each_disabled_locale_do
56 # @USAGE: <function>
57 # @DESCRIPTION:
58 # Complementary to l10n_for_each_locale_do, this function will process
59 # locales that are disabled. This could be used for example to remove
60 # locales from a Makefile, to prevent them from being built needlessly.
61 l10n_for_each_disabled_locale_do() {
62         local locs x
63         locs=$(l10n_get_locales disabled)
64         for x in ${locs}; do
65                 "${@}" ${x} || die "failed to process disabled ${x} locale"
66         done
67 }
68
69 # @FUNCTION: l10n_find_plocales_changes
70 # @USAGE: <translations dir> <filename pre pattern> <filename post pattern>
71 # @DESCRIPTION:
72 # Ebuild maintenance helper function to find changes in package offered
73 # locales when doing a version bump. This could be added for example to
74 # src_prepare
75 #
76 # Example: l10n_find_plocales_changes "${S}/src/translations" "${PN}_" '.ts'
77 l10n_find_plocales_changes() {
78         [[ $# -ne 3 ]] && die "Exactly 3 arguments are needed!"
79         ebegin "Looking in ${1} for new locales"
80         pushd "${1}" >/dev/null || die "Cannot access ${1}"
81         local current= x=
82         for x in ${2}*${3} ; do
83                 x=${x#"${2}"}
84                 x=${x%"${3}"}
85                 current+="${x} "
86         done
87         popd >/dev/null
88         # RHS will be sorted with single spaces so ensure the LHS is too
89         # before attempting to compare them for equality. See bug #513242.
90         # Run them both through the same sorting algorithm so we don't have
91         # to worry about them being the same.
92         if [[ "$(printf '%s\n' ${PLOCALES} | LC_ALL=C sort)" != "$(printf '%s\n' ${current} | LC_ALL=C sort)" ]] ; then
93                 eend 1 "There are changes in locales! This ebuild should be updated to:"
94                 eerror "PLOCALES=\"${current%[[:space:]]}\""
95                 return 1
96         else
97                 eend 0
98         fi
99 }
100
101 # @FUNCTION: l10n_get_locales
102 # @USAGE: [disabled]
103 # @DESCRIPTION:
104 # Determine which LINGUAS USE flags the user has enabled that are offered
105 # by the package, as listed in PLOCALES, and return them. In case no locales
106 # are selected, fall back on PLOCALE_BACKUP. When the disabled argument is
107 # given, return the disabled useflags instead of the enabled ones.
108 l10n_get_locales() {
109         local disabled_locales enabled_locales loc locs
110         for loc in ${PLOCALES}; do
111                 if use linguas_${loc}; then
112                         enabled_locales+="${loc} "
113                 else
114                         disabled_locales+="${loc} "
115                 fi
116         done
117         if [[ ${1} == disabled ]]; then
118                 locs=${disabled_locales}
119         else
120                 locs=${enabled_locales:-$PLOCALE_BACKUP}
121         fi
122         printf "%s" "${locs}"
123 }