Ejemplo n.º 1
0
 def test_negotiate(self):
     de_DE = Locale.negotiate(['de_DE', 'en_US'], ['de_DE', 'de_AT'])
     assert (de_DE.language, de_DE.territory) == ('de', 'DE')
     de = Locale.negotiate(['de_DE', 'en_US'], ['en', 'de'])
     assert (de.language, de.territory) == ('de', None)
     nothing = Locale.negotiate(['de_DE', 'de'], ['en_US'])
     assert nothing is None
Ejemplo n.º 2
0
def handle_request(request, tmpl_context):
    from pylons import session

    tmpl_context.language = locale = None
    if 'locale' in session:
        locale = Locale.parse(session.get('locale'))
    else:
        requested = [l.replace('-', '_') for l in request.languages]
        locale = Locale.parse(Locale.negotiate(get_available_languages(), requested))

    if locale is None:
        locale = get_default_locale()

    tmpl_context.locale = locale

    options = [str(locale), locale.language, str(get_default_locale()),
        get_default_locale().language]
    for language in options:
        try:
            set_lang(language)
            # Lose the territory part of the locale string
            tmpl_context.language = get_lang()[0].split('_')[0]
            break
        except:
            pass
Ejemplo n.º 3
0
 def test_negotiate(self):
     de_DE = Locale.negotiate(["de_DE", "en_US"], ["de_DE", "de_AT"])
     assert (de_DE.language, de_DE.territory) == ("de", "DE")
     de = Locale.negotiate(["de_DE", "en_US"], ["en", "de"])
     assert (de.language, de.territory) == ("de", None)
     nothing = Locale.negotiate(["de_DE", "de"], ["en_US"])
     assert nothing is None
Ejemplo n.º 4
0
def user_language(user, fallbacks=[]):
    # find out the locale
    locale = None
    if user and user.locale:
        locale = user.locale

    if locale is None:
        locales = map(str, LOCALES)
        locale = Locale.parse(Locale.negotiate(fallbacks, locales)) \
                 or get_default_locale()

    # determinate from which path we load the translations
    translations_module = config.get('adhocracy.translations', 'adhocracy')
    translations_module_loader = pkgutil.get_loader(translations_module)
    if translations_module_loader is None:
        raise ValueError(('Cannot import the module "%s" configured for '
                          '"adhocracy.translations". Make sure it is an '
                          'importable module (and contains the '
                          'translation files in a subdirectory '
                          '"i18n"') % translations_module)

    translations_root = translations_module_loader.filename
    translations_config = {'pylons.paths': {'root': translations_root},
                           'pylons.package': config.get('pylons.package')}

    # set language and fallback
    set_lang(locale.language, pylons_config=translations_config)
    add_fallback(get_default_locale().language,
                 pylons_config=translations_config)
    formencode.api.set_stdtranslation(domain="FormEncode",
                                      languages=[locale.language])
    return locale
Ejemplo n.º 5
0
    def list_translations(self):
        """Returns a list of all the locales translations exist for.  The
        list returned will be filled with actual locale objects and not just
        strings.

        .. versionadded:: 0.6
        """
        result = []

        for dirname in self.translation_directories:
            if not os.path.isdir(dirname):
                continue

            for folder in os.listdir(dirname):
                locale_dir = os.path.join(dirname, folder, 'LC_MESSAGES')
                if not os.path.isdir(locale_dir):
                    continue

                if filter(lambda x: x.endswith('.mo'), os.listdir(locale_dir)):
                    result.append(Locale.parse(folder))

        # If not other translations are found, add the default locale.
        if not result:
            result.append(Locale.parse(self._default_locale))

        return result
Ejemplo n.º 6
0
        def test_i18n_parse_date_datetime_meridiem(self):
            tz = datefmt.timezone('GMT +2:00')
            expected_am = datetime.datetime(2011, 2, 22, 0, 45, 56, 0, tz)
            expected_pm = datetime.datetime(2011, 2, 22, 12, 45, 56, 0, tz)
            en_US = Locale.parse('en_US')
            zh_CN = Locale.parse('zh_CN')

            self.assertEqual(expected_am,
                             datefmt.parse_date('Feb 22, 2011 0:45:56 AM', tz,
                                                en_US))
            self.assertEqual(expected_am,
                             datefmt.parse_date('Feb 22, 2011 12:45:56 AM', tz,
                                                en_US))
            self.assertEqual(expected_am,
                             datefmt.parse_date(u'2011-2-22 上午0:45:56', tz,
                                                zh_CN))
            self.assertEqual(expected_am,
                             datefmt.parse_date(u'2011-2-22 上午12:45:56', tz,
                                                zh_CN))

            self.assertEqual(expected_pm,
                             datefmt.parse_date('Feb 22, 2011 0:45:56 PM', tz,
                                                en_US))
            self.assertEqual(expected_pm,
                             datefmt.parse_date('Feb 22, 2011 12:45:56 PM', tz,
                                                en_US))
            self.assertEqual(expected_pm,
                             datefmt.parse_date(u'2011-2-22 下午0:45:56', tz,
                                                zh_CN))
            self.assertEqual(expected_pm,
                             datefmt.parse_date(u'2011-2-22 下午12:45:56', tz,
                                                zh_CN))
Ejemplo n.º 7
0
Archivo: base.py Proyecto: UfSoft/oil
    def _set_language_dropdown_values(self, initial_locale=None):
        log.debug('Setting languages dropdown menu for %r' % initial_locale)
        available_locales = self._find_available_locales()
        locale = Locale(initial_locale)
        if not locale:
            locale = Locale.parse(initial_locale)
        languages = []
        current_lang = h.get_lang()[0]
        for loc in available_locales:
            selected = False
            if loc == current_lang:
                selected = True

            local_locale = Locale(loc)
            if not local_locale:
                local_locale = Locale.parse(loc)
            languages.append((loc, local_locale.get_display_name(locale), selected))
        return locale, languages

        for loc, territory in available_locales:
            selected = False
            language = locale.languages[loc].capitalize()
            if territory:
                country = u'(%s)' % locale.territories[territory]
                value = ['%s_%s' % (loc, territory),
                         u'%s %s' % (language, country)]
                if value[0] == current_lang:
                    selected = True
            else:
                value = [loc, language]
                if value[0] == current_lang:
                    selected = True
            languages.append( value + [selected])
        return locale, languages
Ejemplo n.º 8
0
 def __call__(self, value):
     normalized_value = value.replace(u"-", u"_")
     try:
         BabelLocale.parse(normalized_value)
         return normalized_value
     except (ValueError, UnknownLocaleError):
         raise ValidateError(_(u"Incorrect locale {}"), value)
Ejemplo n.º 9
0
 def negotiate_known_locale(self, preferred_locales):
     """Given a list of preferred locales, this method returns the best
     match locale object from the known ones."""
     assert isinstance(preferred_locales, (tuple, list))
     preferred_locales = [str(l).replace("-", "_") for l in preferred_locales]
     return Locale.parse(
         Locale.negotiate(preferred_locales, self.get_available_locale_names(), aliases=self.get_aliases())
     )
Ejemplo n.º 10
0
def get_locale():
    if 'locale' in session:
        return Locale.parse(session.get('locale'))
    else:
        requested = request.accept_languages.values()
        requested = [l.replace('-', '_') for l in requested]
        available = map(unicode, babel.list_translations())
        return Locale.negotiate(available, requested)
Ejemplo n.º 11
0
    def test_parse_likely_subtags(self):
        l = Locale.parse('zh-TW', sep='-')
        assert l.language == 'zh'
        assert l.territory == 'TW'
        assert l.script == 'Hant'

        l = Locale.parse('und_AT')
        assert l.language == 'de'
        assert l.territory == 'AT'
Ejemplo n.º 12
0
 def check_locale(locale):
     if locale:
         try:
             Locale.parse(locale)
         except ValueError:
             return None
         else:
             return locale
     return None
Ejemplo n.º 13
0
def _get_locales():
    # FIXME this wants cleaning up and merging with get_locales_from_config()
    assert not config.get('lang'), \
        ('"lang" config option not supported - please use ckan.locale_default '
         'instead.')
    locales_offered = config.get('ckan.locales_offered', '').split()
    filtered_out = config.get('ckan.locales_filtered_out', '').split()
    locale_default = config.get('ckan.locale_default', 'en')
    locale_order = config.get('ckan.locale_order', '').split()

    locales = ['en']
    if config.get('ckan.i18n_directory'):
        i18n_path = os.path.join(config.get('ckan.i18n_directory'), 'i18n')
    else:
        i18n_path = os.path.dirname(ckan.i18n.__file__)

    # For every file in the ckan i18n directory see if babel can understand
    # the locale. If yes, add it to the available locales
    for locale in os.listdir(i18n_path):
        try:
            Locale.parse(locale)
            locales.append(locale)
        except (ValueError, UnknownLocaleError):
            # Babel does not know how to make a locale out of this.
            # This is fine since we are passing all files in the
            # ckan.i18n_directory here which e.g. includes the __init__.py
            pass

    assert locale_default in locales, \
        'default language "%s" not available' % locale_default

    locale_list = []
    for locale in locales:
        # no duplicates
        if locale in locale_list:
            continue
        # if offered locales then check locale is offered
        if locales_offered and locale not in locales_offered:
            continue
        # remove if filtered out
        if locale in filtered_out:
            continue
        # ignore the default as it will be added first
        if locale == locale_default:
            continue
        locale_list.append(locale)
    # order the list if specified
    ordered_list = [locale_default]
    for locale in locale_order:
        if locale in locale_list:
            ordered_list.append(locale)
            # added so remove from our list
            locale_list.remove(locale)
    # add any remaining locales not ordered
    ordered_list += locale_list

    return ordered_list
Ejemplo n.º 14
0
def defaultLocale():
    try:
        lang, _ = locale.getdefaultlocale()
    except Exception:
        lang = None

    if lang is not None:
        return Locale.parse(lang)
    else:
        return Locale.default()
Ejemplo n.º 15
0
    def load_translations(self):
        """load all translations for all languages, modules and the app.

        In order to have everything properly cached we will load all
        translations into memory. 
        
        We will also merge module based catalogs into one main catalog.  This
        means that we first fill the catalog with the module catalog and will
        then merge the app on top so you have the possibility to override
        certain translations from modules.

        """

        self.all_locales = set() # all locale objects we know about
        self.catalogs = {} # mapping from locale object to merged translations

        # go through the modules and try to load their catalogs
        for module in self.app.modules:
            dirname = pkg_resources.resource_filename(module.import_name, "translations")
            if not os.path.exists(dirname):
                continue 
            for folder in os.listdir(dirname):
                locale_dir = os.path.join(dirname, folder, 'LC_MESSAGES')
                if not os.path.isdir(locale_dir):
                    continue
                if filter(lambda x: x.endswith('.mo'), os.listdir(locale_dir)):
                    l = Locale.parse(folder)
                    self.all_locales.add(str(l))
                    trans = support.Translations.load(dirname, l)
                    if str(l) not in self.catalogs:
                        self.catalogs[str(l)] = trans
                    else:
                        # we merge if it exists already
                        self.catalogs[str(l)].merge(trans)

        # now for the app
        dirname = pkg_resources.resource_filename(self.app.import_name, "translations")
        for folder in os.listdir(dirname):
            locale_dir = os.path.join(dirname, folder, 'LC_MESSAGES')
            if not os.path.isdir(locale_dir):
                continue
            if filter(lambda x: x.endswith('.mo'), os.listdir(locale_dir)):
                l = Locale.parse(folder)
                self.all_locales.add(str(l))

                # load all domains
                for f in os.listdir(locale_dir):
                    if f.endswith(".mo"):
                        domain = os.path.splitext(f)[0]
                        trans = support.Translations.load(dirname, l, domain = domain)
                        if str(l) not in self.catalogs:
                            self.catalogs[str(l)] = trans
                        else:
                            # we merge if it exists already
                            self.catalogs[str(l)].merge(trans)
Ejemplo n.º 16
0
def select_locale(choices):
    """Selects a locale."""
    enabled = set(settings.LANGUAGE_SECTIONS)
    for locale, quality in choices:
        try:
            locale = Locale.parse(locale, sep='-')
        except UnknownLocaleError:
            continue
        if str(locale) in enabled and \
           find_catalog(locale) is not None:
            return locale
    return Locale.parse(settings.DEFAULT_LANGUAGE)
Ejemplo n.º 17
0
def select_locale(choices):
    """Selects a locale."""
    enabled = set(_settings['sections'])
    for locale, quality in choices:
        try:
            locale = Locale.parse(locale, sep='-')
        except UnknownLocaleError:
            continue
        if str(locale) in enabled and \
           find_catalog(locale) is not None:
            return locale
    return Locale.parse(_settings['default_language'])
Ejemplo n.º 18
0
 def locales(self):
     """
     returns a dict of locale codes to locale display names in both the current locale and the localized locale
     example: if the current locale is es_ES then locales['en_US'] = 'Ingles (Estados Unidos) - English (United States)'
     """
     locales = {}
     for l in config.locales:
         current_locale = Locale.parse(self.locale)
         language = current_locale.languages[l.split('_')[0]]
         territory = current_locale.territories[l.split('_')[1]]
         localized_locale_name = Locale.parse(l).display_name.capitalize()
         locales[l] = language.capitalize() + " (" + territory.capitalize() + ") - " + localized_locale_name
     return locales
Ejemplo n.º 19
0
		def list_translations(dirname):
			if not os.path.isdir(dirname):
				return []
			result = []
			for folder in os.listdir(dirname):
				locale_dir = os.path.join(dirname, folder, 'LC_MESSAGES')
				if not os.path.isdir(locale_dir):
					continue
				if filter(lambda x: x.endswith('.mo'), os.listdir(locale_dir)):
					result.append(Locale.parse(folder))
			if not result:
				result.append(Locale.parse(self._default_locale))
			return result
Ejemplo n.º 20
0
		def list_translations(dirname):
			if not os.path.isdir(dirname):
				return []
			result = []
			for entry in scandir(dirname):
				locale_dir = os.path.join(entry.path, 'LC_MESSAGES')
				if not os.path.isdir(locale_dir):
					continue
				if filter(lambda x: x.name.endswith('.mo'), scandir(locale_dir)):
					result.append(Locale.parse(entry.name))
			if not result:
				result.append(Locale.parse(self._default_locale))
			return result
Ejemplo n.º 21
0
 def check_locale(locale):
     if locale:
         try:
             Locale.parse(locale)
         except ValueError:
             return None
         except UnknownLocaleError:
             return None
         except Exception:
             traceback.print_exc()
             return None
         else:
             return locale
     return None
Ejemplo n.º 22
0
    def test_template_debug_conditions(self):
        request = mock.Mock()
        request.user = self.user
        request.locale = Locale.parse('en_US')

        from django.conf import settings

        settings.DEBUG = False
        settings.TEMPLATE_DEBUG = True

        context = global_context(request)
        self.assertFalse(context['is_debug'])

        settings.DEBUG = True
        settings.TEMPLATE_DEBUG = False

        context = global_context(request)
        self.assertFalse(context['is_debug'])

        settings.DEBUG = False
        settings.TEMPLATE_DEBUG = False

        context = global_context(request)
        self.assertFalse(context['is_debug'])

        settings.DEBUG = True
        settings.TEMPLATE_DEBUG = True

        context = global_context(request)
        self.assertTrue(context['is_debug'])
Ejemplo n.º 23
0
def test_smoke_dates(locale):
    locale = Locale.parse(locale)
    instant = datetime.now()
    for width in ("full", "long", "medium", "short"):
        assert dates.format_date(instant, format=width, locale=locale)
        assert dates.format_datetime(instant, format=width, locale=locale)
        assert dates.format_time(instant, format=width, locale=locale)
Ejemplo n.º 24
0
    def init(self, argv):
        """Subcommand for creating new message catalogs from a template.

        :param argv: the command arguments
        """
        parser = OptionParser(usage=self.usage % ('init', ''),
                              description=self.commands['init'])
        parser.add_option('--domain', '-D', dest='domain',
                          help="domain of PO file (default '%default')")
        parser.add_option('--input-file', '-i', dest='input_file',
                          metavar='FILE', help='name of the input file')
        parser.add_option('--output-dir', '-d', dest='output_dir',
                          metavar='DIR', help='path to output directory')
        parser.add_option('--output-file', '-o', dest='output_file',
                          metavar='FILE',
                          help="name of the output file (default "
                               "'<output_dir>/<locale>/LC_MESSAGES/"
                               "<domain>.po')")
        parser.add_option('--locale', '-l', dest='locale', metavar='LOCALE',
                          help='locale for the new localized catalog')

        parser.set_defaults(domain='messages')
        options, args = parser.parse_args(argv)

        if not options.locale:
            parser.error('you must provide a locale for the new catalog')
        try:
            locale = Locale.parse(options.locale)
        except UnknownLocaleError, e:
            parser.error(e)
Ejemplo n.º 25
0
    def finalize_options(self):
        if not self.input_file:
            raise DistutilsOptionError('you must specify the input file')

        if not self.locale:
            raise DistutilsOptionError('you must provide a locale for the '
                                       'new catalog')
        try:
            self._locale = Locale.parse(self.locale)
        except UnknownLocaleError as e:
            raise DistutilsOptionError(e)

        if not self.output_file and not self.output_dir:
            raise DistutilsOptionError('you must specify the output directory')
        if not self.output_file:
            self.output_file = os.path.join(self.output_dir, self.locale,
                                            'LC_MESSAGES', self.domain + '.po')

        if not os.path.exists(os.path.dirname(self.output_file)):
            os.makedirs(os.path.dirname(self.output_file))
        if self.no_wrap and self.width:
            raise DistutilsOptionError("'--no-wrap' and '--width' are mutually "
                                       "exclusive")
        if not self.no_wrap and not self.width:
            self.width = 76
        elif self.width is not None:
            self.width = int(self.width)
Ejemplo n.º 26
0
def set_locale(cls, force=None):
    """
    retrieve locale from a prioritized list of sources and then set locale and save it
    cls: self object
    force: a locale to force set (ie 'en_US')
    return: locale as string or None if i18n should be disabled
    """
    # disable i18n if config.locales array is empty or None
    if not config.locales:
        return None
    # 1. force locale if provided
    locale = force
    if locale not in config.locales:
        # 2. retrieve locale from url query string
        locale = cls.request.get("hl", None)
        if locale not in config.locales:
            # 3. retrieve locale from cookie
            locale = cls.request.cookies.get('hl', None)
            if locale not in config.locales:
                # 4. retrieve locale from accept language header
                locale = get_locale_from_accept_header(cls.request)
                if locale not in config.locales:
                    # 5. detect locale from IP address location
                    territory = get_territory_from_ip(cls) or 'ZZ'
                    locale = str(Locale.negotiate(territory, config.locales))
                    if locale not in config.locales:
                        # 6. use default locale
                        locale = i18n.get_store().default_locale
    i18n.get_i18n().set_locale(locale)
    # save locale in cookie with 26 weeks expiration (in seconds)
    cls.response.set_cookie('hl', locale, max_age = 15724800)
    return locale
Ejemplo n.º 27
0
def has_section(language):
    """Does this language have a section?"""
    try:
        language = str(Locale.parse(language))
    except UnknownLocaleError:
        return False
    return language in settings.LANGUAGE_SECTIONS
Ejemplo n.º 28
0
def get_languages():
    from babel import Locale
    from babel.core import UnknownLocaleError
    langs = []
    dir_names = [lang for lang in os.listdir(translations_path())
                       if lang != 'en_US']
    for name in dir_names:
        try:
            Locale.parse(name)
        except UnknownLocaleError:
            pass
        else:
            langs.append(name)
    langs.sort()
    langs.insert(0, 'en_US')
    return langs
Ejemplo n.º 29
0
	def fixed_list_translations(self):
		"""Returns a list of all the locales translations exist for.  The
		list returned will be filled with actual locale objects and not just
		strings.
		"""
		def list_translations(dirname):
			if not os.path.isdir(dirname):
				return []
			result = []
			for folder in os.listdir(dirname):
				locale_dir = os.path.join(dirname, folder, 'LC_MESSAGES')
				if not os.path.isdir(locale_dir):
					continue
				if filter(lambda x: x.endswith('.mo'), os.listdir(locale_dir)):
					result.append(Locale.parse(folder))
			if not result:
				result.append(Locale.parse(self._default_locale))
			return result

		dirs = additional_folders + [os.path.join(self.app.root_path, 'translations')]

		result = [Locale.parse(default_locale)]

		for dir in dirs:
			result += list_translations(dir)
		return result
Ejemplo n.º 30
0
        def test_parse_invalid_date(self):
            tz = datefmt.timezone('GMT +2:00')
            en_US = Locale.parse('en_US')

            self.assertRaises(TracError, datefmt.parse_date,
                              '',
                              tzinfo=tz, locale=en_US, hint='date')
            self.assertRaises(TracError, datefmt.parse_date,
                              '2011 Apr Mar',
                              tzinfo=tz, locale=en_US, hint='date')
            self.assertRaises(TracError, datefmt.parse_date,
                              '29 Feb',
                              tzinfo=tz, locale=en_US, hint='date')
            self.assertRaises(TracError, datefmt.parse_date,
                              'Feb 2011',
                              tzinfo=tz, locale=en_US, hint='date')
            self.assertRaises(TracError, datefmt.parse_date,
                              '29 Feb 2010',
                              tzinfo=tz, locale=en_US, hint='date')
            self.assertRaises(TracError, datefmt.parse_date,
                              '29 Xxx 2012',
                              tzinfo=tz, locale=en_US, hint='date')
            self.assertRaises(TracError, datefmt.parse_date,
                              '29 Xxx 2012 4:00:00 AM',
                              tzinfo=tz, locale=en_US, hint='datetime')
            self.assertRaises(TracError, datefmt.parse_date,
                              '29 2012 4:01:02 AM Feb',
                              tzinfo=tz, locale=en_US, hint='datetime')
            self.assertRaises(TracError, datefmt.parse_date,
                              '29 2012 4:00 Feb',
                              tzinfo=tz, locale=en_US, hint='datetime')
Ejemplo n.º 31
0
# individuals. For the exact contribution history, see the revision
# history and logs, available at http://trac.edgewall.org/log/.
#
# Author: Jonas Borgström <*****@*****.**>
#         Christopher Lenz <*****@*****.**>

from __future__ import with_statement

import doctest
import os
import unittest
import sys

try:
    from babel import Locale
    locale_en = Locale.parse('en_US')
except ImportError:
    locale_en = None

from trac.config import Configuration
from trac.core import Component, ComponentManager
from trac.env import Environment
from trac.db.api import _parse_db_str, DatabaseManager
from trac.db.sqlite_backend import SQLiteConnection
from trac.db.util import ConnectionWrapper
import trac.db.postgres_backend
import trac.db.mysql_backend
from trac.ticket.default_workflow import load_workflow_config_snippet
from trac.util import translation

Ejemplo n.º 32
0
def get_locale():
    """Return the current locale."""
    app = zine.application.get_application()
    if app is None:
        return Locale('en')
    return app.locale
Ejemplo n.º 33
0
 def _get_l10n(self, prop, locale):
     """Collapses an internationalized field into a localized one."""
     messages = self.get("metadata", {}).get(prop, {})
     return messages.get(locale) or messages.get(
         Locale.parse(default_locale()).language
     )
Ejemplo n.º 34
0
 def test_url_for_with_locale_object(self):
     url = "/foo/de/dataset/my_dataset"
     generated_url = h.url_for("/dataset/my_dataset", locale=Locale("de"))
     assert generated_url == url
Ejemplo n.º 35
0
from datetime import datetime
from flask import _request_ctx_stack
from babel import dates, numbers, support, Locale
from babel.support import NullTranslations
from werkzeug import ImmutableDict
try:
    from pytz.gae import pytz
except ImportError:
    from pytz import timezone, UTC
else:
    timezone = pytz.timezone
    UTC = pytz.UTC

from flask_babelex._compat import string_types

_DEFAULT_LOCALE = Locale.parse('en')


class Babel(object):
    """Central controller class that can be used to configure how
    Flask-Babel behaves.  Each application that wants to use Flask-Babel
    has to create, or run :meth:`init_app` on, an instance of this class
    after the configuration was initialized.
    """

    default_date_formats = ImmutableDict({
        'time':             'medium',
        'date':             'medium',
        'datetime':         'medium',
        'time.short':       None,
        'time.medium':      None,
Ejemplo n.º 36
0
 def __missing__(self, key):
     return Locale.parse(key, sep="-")
Ejemplo n.º 37
0
    def render_template(self, filename, **kwargs):
        locales = self.app.config.get('locales') or []
        locale_iso = None
        language = ''
        territory = ''
        language_id = self.app.config.get('app_lang')

        if self.locale and len(locales) > 1:
            locale_iso = Locale.parse(self.locale)
            language_id = locale_iso.language
            territory_id = locale_iso.territory
            language = locale_iso.languages[language_id]
            territory = locale_iso.territories[territory_id]

        # make all self.view variables available in jinja2 templates
        if hasattr(self, 'view'):
            kwargs.update(self.view.__dict__)

        # set or overwrite special vars for jinja templates
        kwargs.update({
            'google_analytics_domain':
            self.app.config.get('google_analytics_domain'),
            'google_analytics_code':
            self.app.config.get('google_analytics_code'),
            'app_name':
            self.app.config.get('app_name'),
            'user_id':
            self.user_id,
            'username':
            self.username,
            'email':
            self.email,
            'url':
            self.request.url,
            'path':
            self.request.path,
            'query_string':
            self.request.query_string,
            'path_for_language':
            self.path_for_language,
            'is_mobile':
            self.is_mobile,
            'locale_iso':
            locale_iso,  # babel locale object
            'locale_language':
            language.capitalize() + " (" + territory.capitalize() +
            ")",  # babel locale object
            'locale_language_id':
            language_id,  # babel locale object
            'locales':
            self.locales,
            'provider_uris':
            self.provider_uris,
            'provider_info':
            self.provider_info,
            'enable_federated_login':
            self.app.config.get('enable_federated_login'),
            'base_layout':
            self.get_base_layout
        })
        kwargs.update(self.auth_config)
        if hasattr(self, 'form'):
            kwargs['form'] = self.form
        if self.messages:
            kwargs['messages'] = self.messages

        self.response.headers.add_header('X-UA-Compatible', 'IE=Edge,chrome=1')
        self.response.write(self.jinja2.render_template(filename, **kwargs))
Ejemplo n.º 38
0
 def countries(self):
     return Locale.parse(self.locale).territories
Ejemplo n.º 39
0
#
# This software is licensed as described in the file LICENSE, which
# you should have received as part of this distribution. The terms
# are also available at http://babel.edgewall.org/wiki/License.
#
# This software consists of voluntary contributions made by many
# individuals. For the exact contribution history, see the revision
# history and logs, available at http://babel.edgewall.org/log/.
import pytest

from babel import Locale
from babel.messages import plurals


@pytest.mark.parametrize(('locale', 'num_plurals', 'plural_expr'), [
    (Locale('en'), 2, '(n != 1)'),
    (Locale('en', 'US'), 2, '(n != 1)'),
    (Locale('zh'), 1, '0'),
    (Locale('zh', script='Hans'), 1, '0'),
    (Locale('zh', script='Hant'), 1, '0'),
    (Locale('zh', 'CN', 'Hans'), 1, '0'),
    (Locale('zh', 'TW', 'Hant'), 1, '0'),
])
def test_get_plural_selection(locale, num_plurals, plural_expr):
    assert plurals.get_plural(locale) == (num_plurals, plural_expr)


def test_get_plural_accpets_strings():
    assert plurals.get_plural(locale='ga') == (
        5, '(n==1 ? 0 : n==2 ? 1 : n>=3 && n<=6 ? 2 : n>=7 && n<=10 ? 3 : 4)')
Ejemplo n.º 40
0
class AppConfig(object):
    CACHE_TYPE = 'simple'
    STATE_BASEDIR = path.abspath(env('OPWEN_STATE_DIRECTORY', gettempdir()))
    SQLITE_PATH = path.join(STATE_BASEDIR, 'users.sqlite3')
    SQLALCHEMY_DATABASE_URI = 'sqlite:///' + SQLITE_PATH
    SQLALCHEMY_MIGRATE_REPO = path.join(STATE_BASEDIR, 'app.migrate')
    SQLALCHEMY_TRACK_MODIFICATIONS = False

    SECRET_KEY = env('OPWEN_SESSION_KEY', None)

    CELERY_SQLITE_PATH = path.join(STATE_BASEDIR, 'celery.sqlite3')
    CELERY_BROKER_URL = env('CELERY_BROKER_URL', 'sqlalchemy+sqlite:///' + CELERY_SQLITE_PATH)
    CELERY_BEAT_SCHEDULE_FILENAME = path.join(STATE_BASEDIR, 'celery.cron')

    SECURITY_USER_IDENTITY_ATTRIBUTES = 'email'
    SECURITY_PASSWORD_HASH = 'bcrypt'  # nosec
    SECURITY_PASSWORD_SINGLE_HASH = True
    SECURITY_REGISTERABLE = env.bool('OPWEN_CAN_REGISTER_USER', True)
    SECURITY_CHANGEABLE = env.bool('OPWEN_CAN_CHANGE_PASSWORD', True)
    SECURITY_TRACKABLE = True
    SECURITY_SEND_PASSWORD_CHANGE_EMAIL = False
    SECURITY_POST_REGISTER_VIEW = 'register_complete'
    SECURITY_POST_LOGIN_VIEW = 'login_complete'
    SECURITY_POST_LOGOUT_VIEW = 'logout_complete'
    SECURITY_SEND_REGISTER_EMAIL = False
    SECURITY_MSG_LOGIN = i8n.LOGIN_REQUIRED, 'error'
    SECURITY_MSG_UNAUTHORIZED = i8n.UNAUTHORIZED, 'error'
    SECURITY_MSG_INVALID_PASSWORD = i8n.INVALID_PASSWORD, 'error'
    SECURITY_MSG_DISABLED_ACCOUNT = i8n.ACCOUNT_SUSPENDED, 'error'
    SECURITY_MSG_PASSWORD_INVALID_LENGTH = i8n.SHORT_PASSWORD, 'error'
    SECURITY_MSG_PASSWORD_IS_THE_SAME = i8n.SAME_PASSWORD, 'error'
    SECURITY_LOGIN_URL = '/user/login'
    SECURITY_LOGOUT_URL = '/user/logout'
    SECURITY_REGISTER_URL = '/user/register'
    SECURITY_CHANGE_URL = '/user/password/change'

    TESTING = env.bool('OPWEN_ENABLE_DEBUG', False)

    MODEM_CONFIG_DIR = path.join(STATE_BASEDIR, 'usb_modeswitch')
    SIM_CONFIG_DIR = path.join(STATE_BASEDIR, 'wvdial')
    LOCAL_EMAIL_STORE = path.join(STATE_BASEDIR, 'emails.sqlite3')
    SIM_TYPE = env('OPWEN_SIM_TYPE', None)
    RESTART_PATHS = env.dict('OPWEN_RESTART_PATH', {})
    MAX_UPLOAD_SIZE_MB = env.int('OPWEN_MAX_UPLOAD_SIZE_MB', 0)

    SYNC_SCHEDULE = env('OPWEN_SYNC_SCHEDULE', '').strip()

    EMAIL_ADDRESS_DELIMITER = ','
    EMAILS_PER_PAGE = env.int('OPWEN_EMAILS_PER_PAGE', 10)

    LOCALES_DIRECTORY = path.join(app_basedir, 'translations')
    DEFAULT_LOCALE = Locale.parse('en_ca')
    LOCALES = [DEFAULT_LOCALE] + [Locale.parse(code) for code in subdirectories(LOCALES_DIRECTORY)]

    EMAIL_SEARCHABLE = env.bool('OPWEN_CAN_SEARCH_EMAIL', True)
    COMPRESSION = env('OPWEN_COMPRESSION', 'zstd')
    EMAIL_SERVER_ENDPOINT = env('OPWEN_EMAIL_SERVER_ENDPOINT', None)
    EMAIL_SERVER_HOSTNAME = env('OPWEN_EMAIL_SERVER_HOSTNAME', None)
    EMAIL_HOST_FORMAT = '{}.' + root_domain
    STORAGE_PROVIDER = env('LOKOLE_STORAGE_PROVIDER', 'AZURE_BLOBS')
    STORAGE_CONTAINER = env('OPWEN_REMOTE_RESOURCE_CONTAINER', None)
    STORAGE_ACCOUNT_NAME = env('OPWEN_REMOTE_ACCOUNT_NAME', None)
    STORAGE_ACCOUNT_KEY = env('OPWEN_REMOTE_ACCOUNT_KEY', None)
    STORAGE_ACCOUNT_HOST = env('OPWEN_REMOTE_ACCOUNT_HOST', None)
    STORAGE_ACCOUNT_SECURE = env.bool('OPWEN_REMOTE_ACCOUNT_SECURE', True)
    CLIENT_NAME = env('OPWEN_CLIENT_NAME', None)
    CLIENT_ID = env('OPWEN_CLIENT_ID', None)
    CLIENT_EMAIL_HOST = EMAIL_HOST_FORMAT.format(CLIENT_NAME)
    NEWS_INBOX = 'news@{}'.format(CLIENT_EMAIL_HOST)
    ADMIN_INBOX = 'admin@{}'.format(CLIENT_EMAIL_HOST)
    NEWS_SENDERS = set(env.list('OPWEN_NEWS_SENDERS', []))
    FORBIDDEN_ACCOUNTS = [NEWS_INBOX, ADMIN_INBOX]

    IOC = env('LOKOLE_IOC', 'opwen_email_client.webapp.ioc.Ioc')

    APP_ROOT = env('OPWEN_APP_ROOT', '').rstrip('/')
    SECURITY_URL_PREFIX = APP_ROOT or None
Ejemplo n.º 41
0
 def _get_locale(self):
     if not self._locale:
         return None
     return Locale.parse(self._locale)
Ejemplo n.º 42
0
def get_default_locale():
    from pylons import config
    return Locale.parse(config.get('lang')) or \
            Locale.parse('en')
Ejemplo n.º 43
0
def get_language_name(locale_string):
    try:
        return LANGUAGE_OVERRIDES[locale_string]
    except KeyError:
        return Locale.parse(locale_string).english_name
Ejemplo n.º 44
0
def get_timezone_local_name(tz):
    code = getattr(settings, 'LANGUAGE_CODE', get_language())
    locale = Locale.parse(code, sep='-')
    return get_timezone_location(tz, locale=locale)
Ejemplo n.º 45
0
class Person(db.Model, BaseMixin, SerializerMixin):
    """
    Describe a member of the studio (and an API user).
    """

    first_name = db.Column(db.String(80), nullable=False)
    last_name = db.Column(db.String(80), nullable=False)
    email = db.Column(EmailType, unique=True)
    phone = db.Column(db.String(30))

    active = db.Column(db.Boolean(), default=True)
    last_presence = db.Column(db.Date())

    password = db.Column(db.LargeBinary(60))
    desktop_login = db.Column(db.String(80))
    shotgun_id = db.Column(db.Integer, unique=True)
    timezone = db.Column(
        TimezoneType(backend="pytz"),
        default=pytz_timezone(config.DEFAULT_TIMEZONE),
    )
    locale = db.Column(LocaleType, default=Locale("en", "US"))
    data = db.Column(JSONB)
    role = db.Column(db.String(30), default="user")
    has_avatar = db.Column(db.Boolean(), default=False)

    notifications_enabled = db.Column(db.Boolean(), default=False)
    notifications_slack_enabled = db.Column(db.Boolean(), default=False)
    notifications_slack_userid = db.Column(db.String(60), default="")
    notifications_mattermost_enabled = db.Column(db.Boolean(), default=False)
    notifications_mattermost_userid = db.Column(db.String(60), default="")
    notifications_discord_enabled = db.Column(db.Boolean(), default=False)
    notifications_discord_userid = db.Column(db.String(60), default="")

    departments = db.relationship("Department",
                                  secondary=department_link,
                                  lazy="joined")

    def __repr__(self):
        if sys.version_info[0] < 3:
            return "<Person %s>" % self.full_name().encode("utf-8")
        else:
            return "<Person %s>" % self.full_name()

    def full_name(self):
        return "%s %s" % (self.first_name, self.last_name)

    def serialize(self, obj_type="Person", relations=False):
        data = SerializerMixin.serialize(self, "Person", relations=relations)
        data["full_name"] = self.full_name()
        return data

    def serialize_safe(self, relations=False):
        data = SerializerMixin.serialize(self, "Person", relations=relations)
        data["full_name"] = self.full_name()
        del data["password"]
        return data

    def present_minimal(self, relations=False):
        data = SerializerMixin.serialize(self, "Person", relations=relations)
        return {
            "id": data["id"],
            "first_name": data["first_name"],
            "last_name": data["last_name"],
            "full_name": self.full_name(),
            "has_avatar": data["has_avatar"],
            "active": data["active"],
            "departments": data.get("departments", []),
            "role": data["role"],
        }

    def set_departments(self, department_ids):
        from zou.app.models.department import Department

        self.departments = []
        for department_id in department_ids:
            department = Department.get(department_id)
            if department is not None:
                self.departments.append(department)
        self.save()

    @classmethod
    def create_from_import(cls, person):
        del person["type"]
        del person["full_name"]
        is_update = False
        previous_person = cls.get(person["id"])

        if "password" in person:
            person["password"] = person["password"].encode()

        department_ids = None
        if "departments" in person:
            department_ids = person.pop("departments", None)

        if previous_person is None:
            previous_person = cls.create(**person)
        else:
            is_update = True
            previous_person.update(person)

        if department_ids is not None:
            previous_person.set_departments(department_ids)

        return (previous_person, is_update)
Ejemplo n.º 46
0
def get_day_name_offset_dict(user_locale):
    """ The day name to offset dict maps a day name to a numerical day offset which can be used to add days to the current date.
        Day names will match the provided user locale and will be in lowercase.
    """

    offset_dict = {}

    # Todays birthdays will be shown normally (as a date) so start from tomorrow
    start_date = datetime.now() + relativedelta(days=1)

    # Method 1: Babel
    try:
        babel_locale = Locale.parse(user_locale, sep='_')
        cur_date = start_date

        # Iterate through the following 7 days
        for i in range(1, 8):
            offset_dict[format_date(cur_date, 'EEEE',
                                    locale=babel_locale).lower()] = i
            cur_date = cur_date + relativedelta(days=1)

        return offset_dict
    except UnknownLocaleError as e:
        logger.debug(f'Babel UnknownLocaleError: {e}')

    # Method 2: System locale
    cur_date = start_date
    locale_check_list = [
        user_locale, user_locale + 'UTF-8', user_locale + 'utf-8'
    ]
    system_locale = None

    # Windows
    if any(platform.win32_ver()):
        for locale_to_check in locale_check_list:
            if locale_to_check in locale.windows_locale.values():
                system_locale = locale_to_check
                break
    # POSIX
    else:
        for locale_to_check in locale_check_list:
            if locale_to_check in locale.locale_alias.values():
                system_locale = locale_to_check
                break

    # Check if system locale was found
    if system_locale:
        locale.setlocale(locale.LC_ALL, system_locale)

        # Iterate through the following 7 days
        for i in range(1, 8):
            offset_dict[cur_date.strftime('%A').lower()] = i
            cur_date = cur_date + relativedelta(days=1)

        return offset_dict
    else:
        logger.debug(
            f"Unable to find system locale for provided user locale: '{user_locale}'"
        )

    # Failure
    logger.error(
        f"Failed to generate day name offset dictionary for provided user locale: '{user_locale}'"
    )
    raise SystemError
Ejemplo n.º 47
0
def keywords_analysis(text_data, language):
    complete_language_name = Locale(language).english_name.lower()
    Rake = RAKE.Rake(stopwords.words(complete_language_name))
    return Rake.run(text_data)
Ejemplo n.º 48
0
def get_locale():
    if not hasattr(state, 'locale'):
        return Locale(DEFAULT_LOCALE)
    return Locale(state.locale)
Ejemplo n.º 49
0
def get_locale():
    if "l10n" in request.values:
        return Locale.negotiate([request.values["l10n"]], LANGUAGES)
    return request.accept_languages.best_match(LANGUAGES)
Ejemplo n.º 50
0
def test_datetime_format_get_week_number():
    format = dates.DateTimeFormat(date(2006, 1, 8), Locale.parse('de_DE'))
    assert format.get_week_number(6) == 1

    format = dates.DateTimeFormat(date(2006, 1, 8), Locale.parse('en_US'))
    assert format.get_week_number(6) == 2
Ejemplo n.º 51
0
def test_en_gb_first_weekday():
    assert Locale.parse('en').first_week_day == 0  # Monday in general
    assert Locale.parse('en_US').first_week_day == 6  # Sunday in the US
    assert Locale.parse('en_GB').first_week_day == 0  # Monday in the UK
Ejemplo n.º 52
0
import json
import glob
from contextlib import suppress
from operator import itemgetter

from babel import Locale

complete = []
incomplete = []

for fn in glob.glob("orisa/locale/stats/*_stats.json"):
    with open(fn) as f:
        data = json.load(f)
        loc = data["code"]
        data["native_name"] = Locale.parse(loc).get_display_name(loc).title()
    with suppress(IOError):
        with open(f"orisa-web/src/locale/info/{loc}_info.json") as f:
            data["web_percent_translated"] = json.load(f)["percent_translated"]

    if data["percent_translated"] >= 95:
        complete.append(data)
    else:
        incomplete.append(data)

complete.append({
    "code": "en",
    "name": "English",
    "native_name": "English",
    "percent_translated": 100,
    "web_percent_translated": 100
Ejemplo n.º 53
0

def get_available_locale_identifiers(locales):
    result = set()

    # add available translations
    for locale in locales:
        result.add(locale.language)
        if locale.territory:
            # if a territory is specified, add that too
            result.add("%s_%s" % (locale.language, locale.territory))

    return result


LOCALES = [Locale.parse("en")] + babel.list_translations()
LANGUAGES = get_available_locale_identifiers(LOCALES)


@app.before_request
def before_request():
    g.locale = get_locale()


@babel.localeselector
def get_locale():
    if "l10n" in request.values:
        return Locale.negotiate([request.values["l10n"]], LANGUAGES)
    return request.accept_languages.best_match(LANGUAGES)

Ejemplo n.º 54
0
def _inject_locales() -> dict:
    return {
        'locales': AppConfig.LOCALES,
        'current_locale': Locale.parse(_localeselector()),
    }
Ejemplo n.º 55
0
def locale_from_iso(iso_code):
    return Locale(iso_code)
Ejemplo n.º 56
0
    def __init__(self,
                 default_data=False,
                 enable=None,
                 disable=None,
                 path=None,
                 destroying=False):
        """Construct a new Environment stub object.

        :param default_data: If True, populate the database with some
                             defaults.
        :param enable: A list of component classes or name globs to
                       activate in the stub environment.
        """
        ComponentManager.__init__(self)
        Component.__init__(self)

        self.systeminfo = []

        import trac
        self.path = path
        if self.path is None:
            self.path = os.path.dirname(trac.__file__)
            if not os.path.isabs(self.path):
                self.path = os.path.join(os.getcwd(), self.path)

        # -- configuration
        self.config = Configuration(None)
        # We have to have a ticket-workflow config for ''lots'' of things to
        # work.  So insert the basic-workflow config here.  There may be a
        # better solution than this.
        load_workflow_config_snippet(self.config, 'basic-workflow.ini')
        self.config.set('logging', 'log_level', 'DEBUG')
        self.config.set('logging', 'log_type', 'stderr')
        if enable is not None:
            self.config.set('components', 'trac.*', 'disabled')
        for name_or_class in enable or ():
            config_key = self._component_name(name_or_class)
            self.config.set('components', config_key, 'enabled')
        for name_or_class in disable or ():
            config_key = self._component_name(name_or_class)
            self.config.set('components', config_key, 'disabled')

        # -- logging
        from trac.log import logger_handler_factory
        self.log, self._log_handler = logger_handler_factory('test')

        # -- database
        self.config.set('components', 'trac.db.*', 'enabled')
        self.dburi = get_dburi()

        init_global = False
        if self.global_databasemanager:
            self.components[DatabaseManager] = global_databasemanager
        else:
            self.config.set('trac', 'database', self.dburi)
            self.global_databasemanager = DatabaseManager(self)
            self.config.set('trac', 'debug_sql', True)
            self.config.set('logging', 'log_type', 'stderr')
            self.config.set('logging', 'log_level', 'DEBUG')
            init_global = not destroying

        if default_data or init_global:
            self.reset_db(default_data)

        from trac.web.href import Href
        self.href = Href('/trac.cgi')
        self.abs_href = Href('http://example.org/trac.cgi')

        self.known_users = []
        translation.activate(Locale and Locale('en', 'US'))
Ejemplo n.º 57
0
 def language(self):
     return str(Locale.parse(self.locale).language)
Ejemplo n.º 58
0
    async def settings_command(self, ctx):
        if not ctx.invoked_subcommand:
            lang = main.get_lang(ctx.guild.id) if ctx.guild else main.lang
            # Localization
            lang_code = self.Localization.get_language(
                ctx.guild.id, main.languagecode).replace('-', "_")
            l = Locale.parse(lang_code)

            embed = discord.Embed(
                description=lang["settings_info_description"].format(
                    ctx.guild.name),
                color=self.embed_color)
            embed.set_author(name=lang["settings_info_header"].format(
                ctx.guild.name),
                             icon_url=str(ctx.guild.icon_url))
            embed.add_field(
                name=f'**{lang["prefix_string"]}**',
                value=
                f"{self.PrefixHandler.get_prefix(ctx.guild.id, main.prefix)}",
                inline=True)
            embed.add_field(name=f'**{lang["language_string"]}**',
                            value=f"{l.get_language_name(lang_code)}",
                            inline=True)
            embed.add_field(name=f'**{lang["guild_status_string"]}**',
                            value=lang["guild_status_standard"],
                            inline=True)

            if 'Automation' in self.bot.cogs.keys(
            ) and not self.CogController.is_disabled('Automation',
                                                     ctx.guild.id):
                # Automation Settings
                greetings_channel_id = await self.Toggles.get_greetings_channel(
                    ctx.guild.id)
                greetings_channel = (
                    await self.bot.fetch_channel(greetings_channel_id)
                ).mention if greetings_channel_id else lang[
                    "settings_greetings_default"]
                goodbye_channel_id = await self.Toggles.get_bye_channel(
                    ctx.guild.id)
                goodbye_channel = (await
                                   self.bot.fetch_channel(goodbye_channel_id)
                                   ).mention if goodbye_channel_id else lang[
                                       "settings_goodbye_default"]

                embed.add_field(name=f'**{lang["greetings_channel_string"]}**',
                                value=(greetings_channel
                                       or lang["empty_string"]),
                                inline=True)
                embed.add_field(name=f'**{lang["goodbye_channel_string"]}**',
                                value=(goodbye_channel
                                       or lang["empty_string"]),
                                inline=True)

            if 'Moderation' in self.bot.cogs.keys(
            ) and not self.CogController.is_disabled('Moderation',
                                                     ctx.guild.id):
                # Moderation Settings
                modlog_channel_id = await self.Modlog.get_channel(ctx.guild.id)
                modlog_channel = (await self.bot.fetch_channel(
                    modlog_channel_id)).mention if modlog_channel_id else lang[
                        "settings_modlog_default"]
                embed.add_field(name=f'**{lang["modlog_channel_string"]}**',
                                value=(modlog_channel or lang["empty_string"]),
                                inline=True)

            disabled_cogs = await self.CogController.disabled_cogs(ctx.guild.id
                                                                   )
            embed.add_field(
                name=f'**{lang["disabled_modules_string"]}**',
                value=
                f"```{', '.join(disabled_cogs) if disabled_cogs else lang['settings_disabled_modules_default']}```",
                inline=False)
            disabled_commands = await self.CommandController.disabled_commands(
                ctx.guild.id)
            embed.add_field(
                name=f'**{lang["disabled_modules_string"]}**',
                value=
                f"```{', '.join(disabled_commands) if disabled_commands else lang['settings_disabled_cmds_default']}```",
                inline=False)
            embed.add_field(name=f'**{lang["other_settings_string"]}**',
                            value=lang["settings_info_other_settings"],
                            inline=False)
            await ctx.send(embed=embed)
Ejemplo n.º 59
0
 def default_locale(self):
     """The default locale from the configuration as instance of a
     `babel.Locale` object.
     """
     return Locale.parse(self.app.config['BABEL_DEFAULT_LOCALE'])
Ejemplo n.º 60
0
 def test_url_for_with_locale_object(self):
     url = '/foo/de/dataset/my_dataset'
     generated_url = h.url_for('/dataset/my_dataset', locale=Locale('de'))
     eq_(generated_url, url)