]>
git.ipfire.org Git - collecty.git/blob - src/collecty/locales.py
2 ###############################################################################
4 # collecty - A system statistics collection daemon for IPFire #
5 # Copyright (C) 2015 IPFire development team #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
20 ###############################################################################
25 from .constants
import *
26 from .i18n
import TEXTDOMAIN
29 log
= logging
.getLogger("collecty.locale")
33 def __init__(self
, lang
):
36 def translate(self
, message
, plural_message
=None, count
=None):
37 if plural_message
is not None:
38 assert count
is not None
40 # Replace the message by the plural message if
41 # we are using plural.
43 message
= plural_message
48 class GettextLocale(Locale
):
49 def __init__(self
, lang
, translation
):
50 Locale
.__init
__(self
, lang
)
52 self
.translation
= translation
54 # Bind gettext functions
55 self
.gettext
= self
.translation
.gettext
56 self
.ngettext
= self
.translation
.ngettext
58 def translate(self
, message
, plural_message
=None, count
=None):
59 if plural_message
is not None:
60 assert count
is not None
61 return self
.ngettext(message
, plural_message
, count
)
63 return self
.gettext(message
)
66 def _find_all_locales(domain
, directory
):
68 DEFAULT_LOCALE
: Locale(DEFAULT_LOCALE
),
71 for lang
in os
.listdir(directory
):
72 if lang
.startswith("."):
75 filename
= os
.path
.join(directory
, lang
, "LC_MESSAGES",
78 # Check if a translation file exists and go on if not
79 if not os
.path
.exists(filename
):
83 translation
= gettext
.translation(domain
,
84 directory
, languages
=[lang
])
85 except Exception as e
:
86 log
.error("Cound not load translation for %s: %s" \
90 locales
[lang
] = GettextLocale(lang
, translation
)
92 log
.debug("Loaded translations: %s" % ", ".join(locales
.keys()))
96 _locales
= _find_all_locales(TEXTDOMAIN
, "/usr/share/locale")
98 def get_supported_locales():
99 return list(_locales
.keys())
101 def get_closest(*langs
):
106 lang
= lang
.replace("-", "_")
107 parts
= lang
.split("_")
112 elif len(parts
) == 2:
113 parts
[0] = parts
[0].lower()
114 parts
[1] = parts
[1].upper()
115 lang
= "_".join(parts
)
117 for l
in (lang
, parts
[0]):
124 return get_closest(*langs
) or _locales
.get(DEFAULT_LOCALE
, None)