123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- import gettext
- import locale
- import os
- from time import strftime, localtime
- from searxqt.core.log import warning
- __SYSTEM_LOCALE_FOUND = False
- Languages = [
- ('de_DE', 'Deutsch'),
- ('nl_NL', 'Nederlands'),
- ('es_ES', 'Español')
- ]
- Language = None
- defaultLanguage, defaultEncoding = locale.getlocale()
- index = 0
- for key, name in Languages:
- if defaultLanguage == key:
- Language = Languages[index]
- break
- index += 1
- def timeToString(time_, fmt="%a %d %b %Y %H:%M:%S"):
- """
- @param time: Time in seconds to format to string.
- @type time: uint
- @param fmt: Format; see
- https://docs.python.org/3/library/time.html#time.strftime
- @type fmt: str
- """
- if __SYSTEM_LOCALE_FOUND:
- locale.setlocale(locale.LC_TIME, normalize(Language[0]))
- return strftime(fmt, localtime(time_))
- def durationMinutesToString(minutes):
- """ Convert minutes to a string. Example:
- 3 years 30 days 1 hour 30 minutes
- @param minutes: Minutes to convert to duration string.
- @type minutes: int
- 60 * 24 = 1440 minutes in 1 day.
- 365 * 1440 = 525600 minutes in 1 year.
- """
- if not minutes:
- return "-"
- oneHour = 60
- oneDay = 1440
- oneYear = 525600
- str_ = [
- _("year"), _("years"),
- _("day"), _("days"),
- _("hour"), _("hours"),
- _("min"), _("mins")
- ]
- returnStr = ""
- years = int(minutes / oneYear)
- if years:
- minutes -= years * oneYear
- returnStr += f"{years} {str_[0] if years == 1 else str_[1]}"
- days = int(minutes / oneDay)
- if days:
- minutes -= days * oneDay
- returnStr += f"{days} {str_[2] if days == 1 else str_[3]}"
- hours = int(minutes / oneHour)
- if hours:
- minutes -= hours * oneHour
- returnStr += f"{hours} {str_[4] if hours == 1 else str_[5]}"
- if minutes:
- returnStr += f"{minutes} {str_[6] if minutes == 1 else str_[7]}"
- return returnStr
- def normalize(localeName):
- str_ = locale.normalize(localeName)
- enc = locale.getpreferredencoding(do_setlocale=True)
- str_ = str_.split('.', 1)[0]
- return str_ + "." + enc
- def localeTest(normalizedLocaleName):
- try:
- locale.setlocale(locale.LC_TIME, normalizedLocaleName)
- except locale.Error:
- return False
- return True
- _ = gettext.gettext
- if Language:
-
- __SYSTEM_LOCALE_FOUND = localeTest(
- normalize(Language[0])
- )
- if not __SYSTEM_LOCALE_FOUND:
- warning(f"`{normalize(Language[0])}` not found. " \
- "Cannot translate dates and time.")
-
- lang = None
-
- xdgDataHome = os.getenv('XDG_DATA_HOME')
- if xdgDataHome is not None:
- localeDir = os.path.join(xdgDataHome, 'locale')
- try:
- lang = gettext.translation('searx-qt', localedir=localeDir,
- languages=[Language[0]])
- except FileNotFoundError:
- pass
-
- if not lang:
- try:
- lang = gettext.translation('searx-qt', languages=[Language[0]])
- except FileNotFoundError:
- pass
- if lang:
- lang.install()
- _ = lang.gettext
- else:
- warning(f"Locale file (.mo) for language `{Language[0]}` not found!")
|