]> git.ipfire.org Git - oddments/collecty.git/blame - src/collecty/locales.py
Remove or move as many constants as possible
[oddments/collecty.git] / src / collecty / locales.py
CommitLineData
429ba506
MT
1#!/usr/bin/python3
2###############################################################################
3# #
4# collecty - A system statistics collection daemon for IPFire #
5# Copyright (C) 2015 IPFire development team #
6# #
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. #
11# #
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. #
16# #
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/>. #
19# #
20###############################################################################
21
22import gettext
16b84672 23import logging
429ba506
MT
24import os
25
26from .constants import *
27from .i18n import TEXTDOMAIN
28
429ba506 29log = logging.getLogger("collecty.locale")
429ba506 30
c9c2415c
MT
31DEFAULT_LOCALE = "en_US.utf-8"
32
429ba506
MT
33class Locale(object):
34 def __init__(self, lang):
35 self.lang = lang
36
37 def translate(self, message, plural_message=None, count=None):
38 if plural_message is not None:
39 assert count is not None
40
41 # Replace the message by the plural message if
42 # we are using plural.
43 if count > 1:
44 message = plural_message
45
46 return message
47
48
49class GettextLocale(Locale):
50 def __init__(self, lang, translation):
51 Locale.__init__(self, lang)
52
53 self.translation = translation
54
55 # Bind gettext functions
56 self.gettext = self.translation.gettext
57 self.ngettext = self.translation.ngettext
58
59 def translate(self, message, plural_message=None, count=None):
60 if plural_message is not None:
61 assert count is not None
62 return self.ngettext(message, plural_message, count)
63
64 return self.gettext(message)
65
66
67def _find_all_locales(domain, directory):
68 locales = {
69 DEFAULT_LOCALE : Locale(DEFAULT_LOCALE),
70 }
71
72 for lang in os.listdir(directory):
73 if lang.startswith("."):
74 continue
75
76 filename = os.path.join(directory, lang, "LC_MESSAGES",
77 "%s.mo" % domain)
78
79 # Check if a translation file exists and go on if not
80 if not os.path.exists(filename):
81 continue
82
83 try:
84 translation = gettext.translation(domain,
85 directory, languages=[lang])
86 except Exception as e:
87 log.error("Cound not load translation for %s: %s" \
88 % (lang, e))
89 continue
90
91 locales[lang] = GettextLocale(lang, translation)
92
93 log.debug("Loaded translations: %s" % ", ".join(locales.keys()))
94
95 return locales
96
97_locales = _find_all_locales(TEXTDOMAIN, "/usr/share/locale")
98
99def get_supported_locales():
100 return list(_locales.keys())
101
102def get_closest(*langs):
103 for lang in langs:
104 if not lang:
105 continue
106
107 lang = lang.replace("-", "_")
108 parts = lang.split("_")
109
110 if len(parts) > 2:
111 continue
112
113 elif len(parts) == 2:
114 parts[0] = parts[0].lower()
115 parts[1] = parts[1].upper()
116 lang = "_".join(parts)
117
118 for l in (lang, parts[0]):
119 try:
120 return _locales[l]
121 except KeyError:
122 pass
123
124def get(*langs):
ac5bd9d3 125 return get_closest(*langs) or _locales.get(DEFAULT_LOCALE, None)