123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- # -*- coding: utf-8 -*-
- ########################################################################
- # Searx-qt - Lightweight desktop application for SearX.
- # Copyright (C) 2020 CYBERDEViL
- #
- # This file is part of Searx-qt.
- #
- # Searx-qt is free software: you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation, either version 3 of the License, or
- # (at your option) any later version.
- #
- # Searx-qt is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program. If not, see <https://www.gnu.org/licenses/>.
- #
- ########################################################################
- import gettext
- import locale
- import os
- from time import strftime, localtime
- __SYSTEM_LOCALE_FOUND = False
- # List all available languages.
- Languages = [
- ('de_DE', 'Deutsch'),
- ('nl_NL', 'Nederlands'),
- ('es_ES', 'Español')
- ]
- # Set searx-qt default language
- Language = None
- # Find system default
- defaultLanguage, defaultEncoding = locale.getlocale()
- # Set current language to system default if found.
- 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 += "{0} {1} ".format(
- years,
- str_[0] if years == 1 else str_[1]
- )
- days = int(minutes / oneDay)
- if days:
- minutes -= days * oneDay
- returnStr += "{0} {1} ".format(days, str_[2] if days == 1 else str_[3])
- hours = int(minutes / oneHour)
- if hours:
- minutes -= hours * oneHour
- returnStr += "{0} {1} ".format(
- hours,
- str_[4] if hours == 1 else str_[5]
- )
- if minutes:
- returnStr += "{0} {1} ".format(
- 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:
- # Set LC_TIME if system locale found.
- __SYSTEM_LOCALE_FOUND = localeTest(
- normalize(Language[0])
- )
- if not __SYSTEM_LOCALE_FOUND:
- print("Warning! `{0}` not found. Cannot translate dates and time."
- .format(normalize(Language[0])))
- # Set local .mo file.
- lang = None
- try:
- # First try system locale directory
- lang = gettext.translation('searx-qt', languages=[Language[0]])
- except FileNotFoundError:
- 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 lang:
- lang.install()
- _ = lang.gettext
- else:
- print("Warning! Locale file (.mo) for language `{0}` not found!"
- .format(Language[0]))
|