proj/gentoo: Initial commit
[gentoo.git] / eclass / l10n.eclass
1 # Copyright 1999-2012 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3 # $Id$
4
5 # @ECLASS: l10n.eclass
6 # @MAINTAINER:
7 # Ben de Groot <yngwin@gentoo.org>
8 # @BLURB: convenience functions to handle localizations
9 # @DESCRIPTION:
10 # The l10n (localization) eclass offers a number of functions to more
11 # conveniently handle localizations (translations) offered by packages.
12 # These are meant to prevent code duplication for such boring tasks as
13 # determining the cross-section between the user's set LINGUAS and what
14 # is offered by the package; and generating the right list of linguas_*
15 # USE flags.
16
17 # @ECLASS-VARIABLE: PLOCALES
18 # @DEFAULT_UNSET
19 # @DESCRIPTION:
20 # Variable listing the locales for which localizations are offered by
21 # the package. Check profiles/desc/linguas.desc to see if the locales
22 # are listed there. Add any missing ones there.
23 #
24 # Example: PLOCALES="cy de el_GR en_US pt_BR vi zh_CN"
25
26 # @ECLASS-VARIABLE: PLOCALE_BACKUP
27 # @DEFAULT_UNSET
28 # @DESCRIPTION:
29 # In some cases the package fails when none of the offered PLOCALES are
30 # selected by the user. In that case this variable should be set to a
31 # default locale (usually 'en' or 'en_US') as backup.
32 #
33 # Example: PLOCALE_BACKUP="en_US"
34
35 # Add linguas useflags
36 for u in ${PLOCALES}; do
37         IUSE+=" linguas_${u}"
38 done
39
40 # @FUNCTION: l10n_for_each_locale_do
41 # @USAGE: <function>
42 # @DESCRIPTION:
43 # Convenience function for processing localizations. The parameter should
44 # be a function (defined in the consuming eclass or ebuild) which takes
45 # an individual localization as (last) parameter.
46 #
47 # Example: l10n_for_each_locale_do install_locale
48 l10n_for_each_locale_do() {
49         local locs x
50         locs=$(l10n_get_locales)
51         for x in ${locs}; do
52                 "${@}" ${x} || die "failed to process enabled ${x} locale"
53         done
54 }
55
56 # @FUNCTION: l10n_for_each_disabled_locale_do
57 # @USAGE: <function>
58 # @DESCRIPTION:
59 # Complementary to l10n_for_each_locale_do, this function will process
60 # locales that are disabled. This could be used for example to remove
61 # locales from a Makefile, to prevent them from being built needlessly.
62 l10n_for_each_disabled_locale_do() {
63         local locs x
64         locs=$(l10n_get_locales disabled)
65         for x in ${locs}; do
66                 "${@}" ${x} || die "failed to process disabled ${x} locale"
67         done
68 }
69
70 # @FUNCTION: l10n_find_plocales_changes
71 # @USAGE: <translations dir> <filename pre pattern> <filename post pattern>
72 # @DESCRIPTION:
73 # Ebuild maintenance helper function to find changes in package offered
74 # locales when doing a version bump. This could be added for example to
75 # src_prepare
76 #
77 # Example: l10n_find_plocales_changes "${S}/src/translations" "${PN}_" '.ts'
78 l10n_find_plocales_changes() {
79         [[ $# -ne 3 ]] && die "Exactly 3 arguments are needed!"
80         einfo "Looking in ${1} for new locales ..."
81         pushd "${1}" >/dev/null || die "Cannot access ${1}"
82         local current= x=
83         for x in ${2}*${3} ; do
84                 x=${x#"${2}"}
85                 x=${x%"${3}"}
86                 current+="${x} "
87         done
88         popd >/dev/null
89         if [[ ${PLOCALES} != ${current%[[:space:]]} ]] ; then
90                 einfo "There are changes in locales! This ebuild should be updated to:"
91                 einfo "PLOCALES=\"${current%[[:space:]]}\""
92         else
93                 einfo "Done"
94         fi
95 }
96
97 # @FUNCTION: l10n_get_locales
98 # @USAGE: [disabled]
99 # @DESCRIPTION:
100 # Determine which LINGUAS USE flags the user has enabled that are offered
101 # by the package, as listed in PLOCALES, and return them. In case no locales
102 # are selected, fall back on PLOCALE_BACKUP. When the disabled argument is
103 # given, return the disabled useflags instead of the enabled ones.
104 l10n_get_locales() {
105         local disabled_locales enabled_locales loc locs
106         for loc in ${PLOCALES}; do
107                 if use linguas_${loc}; then
108                         enabled_locales+="${loc} "
109                 else
110                         disabled_locales+="${loc} "
111                 fi
112         done
113         if [[ ${1} == disabled ]]; then
114                 locs=${disabled_locales}
115         else
116                 locs=${enabled_locales:-$PLOCALE_BACKUP}
117         fi
118         printf "%s" "${locs}"
119 }