Beispiel #1
0
def get_target_lang_from_request(request, default_locale='en'):
    request_form = request.GET or request.POST

    if request_form.has_key('lang') and request_form['lang'] != '':
        return locale_to_lower_upper(request_form['lang'])

    try:
        if request.matchdict.has_key('target_lang'):
            target_lang = request.matchdict['target_lang']
        else:
            header_value = request.accept_language.header_value

            if ACCEPT_LANG_CACHE.has_key(header_value):
                return ACCEPT_LANG_CACHE[header_value]

            # clean the header into something we can use
            accept_lang = []
            for part in header_value.split(","):
                try:
                    locale, quality = map(str.strip, part.split(";"))
                    quality = float(quality)
                except ValueError:
                    locale = part.strip()
                    quality = 1
                accept_lang.append((locale_to_lower_upper(locale), quality))

            available = list(get_all_supported_languages())

            def run_matches(accept_lang, available):
                best = -1
                target_lang = None
                for locale, quality in accept_lang:
                    if quality > best and available.count(locale):
                        best = quality
                        target_lang = locale
                return target_lang

            # search for exact matches
            target_lang = run_matches(accept_lang, available)

            if not target_lang:
                # search for near matches
                reduced_accept = [(v.split("_")[0], q) for v, q in accept_lang]
                reduced_available = [i.split("_")[0] for i in available]
                target_lang = run_matches(reduced_accept, reduced_available)

    except AttributeError:
        # header was not defined
        header_value = default_locale
        target_lang = None

    if not target_lang:
        # screw it
        target_lang = default_locale

    try:
        ACCEPT_LANG_CACHE[header_value] = target_lang
    except NameError, e:
        # We didn't set the header_value, so don't try to use it as a key
        pass
Beispiel #2
0
def get_target_lang_from_request(request, default_locale='en'):
    request_form = request.GET or request.POST

    if request_form.has_key('lang') and request_form['lang'] != '':
        return locale_to_lower_upper(request_form['lang'])

    try:
        if request.matchdict.has_key('target_lang'):
            target_lang = request.matchdict['target_lang']
        else:
            header_value = request.accept_language.header_value

            if ACCEPT_LANG_CACHE.has_key(header_value):
                return ACCEPT_LANG_CACHE[header_value]

            # clean the header into something we can use
            accept_lang = []
            for part in header_value.split(","):
                try:
                    locale, quality = map(str.strip, part.split(";"))
                    quality = float(quality)
                except ValueError:
                    locale = part.strip()
                    quality = 1
                accept_lang.append((locale_to_lower_upper(locale), quality))
            
            available = list(get_all_supported_languages())

            def run_matches(accept_lang, available):
                best = -1
                target_lang = None
                for locale, quality in accept_lang:
                    if quality > best and available.count(locale):
                        best = quality
                        target_lang = locale
                return target_lang

            # search for exact matches
            target_lang = run_matches(accept_lang, available)
        
            if not target_lang:
                # search for near matches
                reduced_accept = [(v.split("_")[0], q) for v,q in accept_lang]
                reduced_available = [i.split("_")[0] for i in available]
                target_lang = run_matches(reduced_accept, reduced_available)

    except AttributeError:
        # header was not defined
        header_value = default_locale
        target_lang = None
                              
    if not target_lang:
        # screw it
        target_lang = default_locale

    try:
        ACCEPT_LANG_CACHE[header_value] = target_lang
    except NameError, e:
        # We didn't set the header_value, so don't try to use it as a key
        pass
Beispiel #3
0
def test_locale_to_lower_upper():
    """
    Test cc.i18n.util.locale_to_lower_upper()
    """
    assert util.locale_to_lower_upper("en") == "en"
    assert util.locale_to_lower_upper("en_US") == "en_US"
    assert util.locale_to_lower_upper("en-us") == "en_US"

    # crazy renditions.  Useful?
    assert util.locale_to_lower_upper("en-US") == "en_US"
    assert util.locale_to_lower_upper("en_us") == "en_US"
Beispiel #4
0
def test_locale_to_lower_upper():
    """
    Test cc.i18n.util.locale_to_lower_upper()
    """
    assert util.locale_to_lower_upper('en') == 'en'
    assert util.locale_to_lower_upper('en_US') == 'en_US'
    assert util.locale_to_lower_upper('en-us') == 'en_US'

    # crazy renditions.  Useful?
    assert util.locale_to_lower_upper('en-US') == 'en_US'
    assert util.locale_to_lower_upper('en_us') == 'en_US'
Beispiel #5
0
def active_languages():
    """Return a sequence of dicts, where each element consists of the
    following keys:

    * code: the language code
    * name: the translated name of this language

    for each available language."""
    global _ACTIVE_LANGUAGES
    if _ACTIVE_LANGUAGES:
        return _ACTIVE_LANGUAGES

    # get a list of avaialable translations
    domain = base.queryUtility(ITranslationDomain,
                               ccorg_i18n_setup.I18N_DOMAIN)
    lang_codes = set(domain.getCatalogsInfo().keys())

    # determine the intersection of available translations and
    # launched jurisdiction locales
    launched_locales = set()
    jurisdictions = cclicense_functions.get_valid_jurisdictions()

    for jurisdiction in jurisdictions:
        query_string = ('PREFIX dc: <http://purl.org/dc/elements/1.1/> '
                        'SELECT ?lang WHERE {'
                        '  <%s> dc:language ?lang}') % jurisdiction

        query = RDF.Query(str(query_string), query_language='sparql')
        this_juri_locales = set([
            locale_to_lower_upper(str(result['lang']))
            for result in query.execute(rdf_helper.JURI_MODEL)
        ])

        # Append those locales that are applicable to this domain
        launched_locales.update(lang_codes.intersection(this_juri_locales))

    # XXX: Have to hack in Esperanto here because it's technically an
    # "Unported" language but there is no unported RDF jurisdiction in
    # jurisdictions.rdf..
    launched_locales.add('eo')

    # make our sequence have a predictable order
    launched_locales = list(launched_locales)

    # this loop is long hand for clarity; it's only done once, so
    # the additional performance cost should be negligible
    result = []
    for code in launched_locales:

        if code == 'test': continue

        gettext = ugettext_for_locale(negotiate_locale(code))
        name = gettext(mappers.LANG_MAP[code])
        result.append(dict(code=code, name=name))

    result = sorted(result, key=lambda lang: lang['name'].lower())

    _ACTIVE_LANGUAGES = result

    return result
Beispiel #6
0
    def legalcodes(self, language='en'):
        """
        Return a list of
        [(legalcode_uri, legalcode_lang, legalcode_lang_translated)]
        for this license.

        If this is a single-legalcode option, it'll probably return
        [(legalcode_uri, None, None)]

        """
        if self._legalcodes.has_key(language):
            return self._legalcodes[language]

        gettext = ugettext_for_locale(language)

        legalcodes = set()
        for legalcode, lang in rdf_helper.get_license_legalcodes(self.uri):
            if lang is None:
                translated_lang = None
            # <terrible_fixable_hacks>
            # We should probably add lang.sr_CYRL and lang.sr_LATN messages
            elif lang == 'sr-Cyrl':
                translated_lang = gettext('Serbian')
            elif lang == 'sr-Latn':
                translated_lang = 'srpski (latinica)'
            # </terrible_fixable_hacks>
            else:
                translated_lang = gettext(
                    mappers.LANG_MAP[locale_to_lower_upper(lang)])

            legalcodes.add((legalcode, lang, translated_lang))

        self._legalcodes[language] = legalcodes

        return legalcodes
Beispiel #7
0
def get_locale_file_from_locale(locale):
    """
    Returns the path to the locale file as a string or None if
    that file does not exist.
    """
    language = locale_to_lower_upper(locale)

    this_locale_filename = pkg_resources.resource_filename(
        u'zope.i18n.locales', u'data/%s.xml' % language)

    if os.path.exists(this_locale_filename):
        return this_locale_filename
    else:
        return None
Beispiel #8
0
def get_locale_file_from_locale(locale):
    """
    Returns the path to the locale file as a string or None if
    that file does not exist.
    """
    language = locale_to_lower_upper(locale)

    this_locale_filename = pkg_resources.resource_filename(
        u'zope.i18n.locales', u'data/%s.xml' % language)

    if os.path.exists(this_locale_filename):
        return this_locale_filename
    else:
        return None
Beispiel #9
0
    def legalcodes(self, language='en'):
        """
        Return a list of
        [(legalcode_uri, legalcode_lang, legalcode_lang_translated)]
        for this license.

        If this is a single-legalcode option, it'll probably return
        [(legalcode_uri, None, None)]

        """
        if self._legalcodes.has_key(language):
            return self._legalcodes[language]

        gettext = ugettext_for_locale(language)

        legalcodes = set()
        for legalcode, lang in rdf_helper.get_license_legalcodes(self.uri):
            if lang is None:
                translated_lang = None
            # <terrible_fixable_hacks>
            # We should probably add lang.sr_CYRL and lang.sr_LATN messages
            elif lang == 'sr-Cyrl':
                translated_lang = gettext('Serbian')
            elif lang == 'sr-Latn':
                translated_lang = 'srpski (latinica)'
            # </terrible_fixable_hacks>
            else:
                translated_lang = gettext(
                    mappers.LANG_MAP[locale_to_lower_upper(lang)])

            legalcodes.add(
                (legalcode, lang, translated_lang))

        self._legalcodes[language] = legalcodes

        return legalcodes
Beispiel #10
0
    except AttributeError:
        # header was not defined
        header_value = default_locale
        target_lang = None

    if not target_lang:
        # screw it
        target_lang = default_locale

    try:
        ACCEPT_LANG_CACHE[header_value] = target_lang
    except NameError, e:
        # We didn't set the header_value, so don't try to use it as a key
        pass

    return locale_to_lower_upper(target_lang)


def generate_404_response(request, routing, environ, staticdirector):
    """
    Create a 'nice looking' 404 response.
    """
    request.matchdict = {}
    request.urlgen = routes.URLGenerator(routing.mapping, environ)
    request.staticdirect = staticdirector

    target_lang = get_target_lang_from_request(request)

    context = {'page_style': 'bare'}
    context.update(rtl_context_stuff(target_lang))
Beispiel #11
0
 def title(self, language='en'):
     if self._titles is None:
         self._titles = rdf_helper.get_titles(self.uri)
     i18n_title = self._titles['x-i18n']
     return inverse_translate(i18n_title, locale_to_lower_upper(language))
Beispiel #12
0
def license_deed_view(request):
    """
    The main and major deed generating view.
    """
    ##########################
    # Try and get the license.
    ##########################
    license = by_code(request.matchdict['code'],
                      jurisdiction=request.matchdict.get('jurisdiction'),
                      version=request.matchdict.get('version'))
    if not license:
        license_versions = util.catch_license_versions_from_request(request)

        if license_versions:
            # If we can't get it, but others of that code exist, give
            # a special 404.
            return license_catcher(request)
        else:
            # Otherwise, give the normal 404.
            return exc.HTTPNotFound()

    ####################
    # Everything else ;)
    ####################
    # "color" of the license; the color reflects the relative amount
    # of freedom.
    if license.license_code in ('devnations', 'sampling'):
        color = 'red'
    elif license.license_code.find('sampling') > -1 or \
             license.license_code.find('nc') > -1 or \
             license.license_code.find('nd') > -1:
        color = 'yellow'
    else:
        color = 'green'

    # Get the language this view will be displayed in.
    #  - First checks to see if the routing matchdict specifies the language
    #  - Or, next gets the jurisdictions' default language if the jurisdiction
    #    specifies one
    #  - Otherwise it's english!
    if request.matchdict.has_key('target_lang'):
        target_lang = request.matchdict.get('target_lang')
    elif license.jurisdiction.default_language:
        target_lang = locale_to_lower_upper(
            license.jurisdiction.default_language)
    else:
        target_lang = 'en'

    # True if the legalcode for this license is available in
    # multiple languages (or a single language with a language code different
    # than that of the jurisdiction).
    #
    # Stored in the RDF, we'll just check license.legalcodes() :)
    legalcodes = license.legalcodes(target_lang)
    if len(legalcodes) > 1 \
            or list(legalcodes)[0][2] is not None:
        multi_language = True
        legalcodes = sorted(legalcodes, key=lambda lc: lc[2])
    else:
        multi_language = False

    # Use the lower-dash style for all RDF-related locale stuff
    rdf_style_target_lang = locale_to_lower_lower(target_lang)

    license_title = None
    try:
        license_title = license.title(rdf_style_target_lang)
    except KeyError:
        # don't have one for that language, use default
        license_title = license.title()

    conditions = {}
    for code in license.license_code.split('-'):
        conditions[code] = 1

    # Find out all the active languages
    active_languages = get_well_translated_langs()
    negotiated_locale = negotiate_locale(target_lang)

    # If negotiating the locale says that this isn't a valid language,
    # let's fall back to something that is.
    if target_lang != negotiated_locale:
        base_url = REMOVE_DEED_URL_RE.match(request.path_info).groups()[0]
        redirect_to = base_url + 'deed.' + negotiated_locale
        return exc.HTTPFound(location=redirect_to)

    if DEED_TEMPLATE_MAPPING.has_key(license.license_code):
        main_template = DEED_TEMPLATE_MAPPING[license.license_code]
    else:
        main_template = 'licenses/standard_deed.html'

    get_this = "/choose/results-one?license_code=%s&amp;jurisdiction=%s&amp;version=%s&amp;lang=%s" % (
        urllib.quote(license.license_code), license.jurisdiction.code,
        license.version, target_lang)

    context = {
        'request': request,
        'license_code': license.license_code,
        'license_code_quoted': urllib.quote(license.license_code),
        'license_title': license_title,
        'license': license,
        'multi_language': multi_language,
        'legalcodes': legalcodes,
        'color': color,
        'conditions': conditions,
        'active_languages': active_languages,
        'target_lang': target_lang,
        'jurisdiction': license.jurisdiction.code,
        'get_this': get_this,
    }
    context.update(util.rtl_context_stuff(target_lang))

    return Response(
        util.render_template(request, target_lang, main_template, context))
Beispiel #13
0
def license_deed_view(request):
    """
    The main and major deed generating view.
    """
    ##########################
    # Try and get the license.
    ##########################
    license = by_code(
        request.matchdict['code'],
        jurisdiction=request.matchdict.get('jurisdiction'),
        version=request.matchdict.get('version'))
    if not license:
        license_versions = util.catch_license_versions_from_request(request)

        if license_versions:
            # If we can't get it, but others of that code exist, give
            # a special 404.
            return license_catcher(request)
        else:
            # Otherwise, give the normal 404.
            return exc.HTTPNotFound()

    ####################
    # Everything else ;)
    ####################
    # "color" of the license; the color reflects the relative amount
    # of freedom.
    if license.license_code in ('devnations', 'sampling'):
       color = 'red'
    elif license.license_code.find('sampling') > -1 or \
             license.license_code.find('nc') > -1 or \
             license.license_code.find('nd') > -1:
       color = 'yellow'
    else:
       color = 'green'

    # Get the language this view will be displayed in.
    #  - First checks to see if the routing matchdict specifies the language
    #  - Or, next gets the jurisdictions' default language if the jurisdiction
    #    specifies one
    #  - Otherwise it's english!
    if request.matchdict.has_key('target_lang'):
        target_lang = request.matchdict.get('target_lang')
    elif license.jurisdiction.default_language:
        target_lang = locale_to_lower_upper(
            license.jurisdiction.default_language)
    else:
        target_lang = 'en'

    # True if the legalcode for this license is available in
    # multiple languages (or a single language with a language code different
    # than that of the jurisdiction).
    #
    # Stored in the RDF, we'll just check license.legalcodes() :)
    legalcodes = license.legalcodes(target_lang)
    if len(legalcodes) > 1 \
            or list(legalcodes)[0][2] is not None:
        multi_language = True
        legalcodes = sorted(legalcodes, key=lambda lc: lc[2])
    else:
        multi_language = False

    # Use the lower-dash style for all RDF-related locale stuff
    rdf_style_target_lang = locale_to_lower_lower(target_lang)

    license_title = None
    try:
        license_title = license.title(rdf_style_target_lang)
    except KeyError:
        # don't have one for that language, use default
        license_title = license.title()

    conditions = {}
    for code in license.license_code.split('-'):
        conditions[code] = 1

    # Find out all the active languages
    active_languages = get_well_translated_langs()
    negotiated_locale = negotiate_locale(target_lang)

    # If negotiating the locale says that this isn't a valid language,
    # let's fall back to something that is.
    if target_lang != negotiated_locale:
        base_url = REMOVE_DEED_URL_RE.match(request.path_info).groups()[0]
        redirect_to = base_url + 'deed.' + negotiated_locale
        return exc.HTTPFound(location=redirect_to)

    if DEED_TEMPLATE_MAPPING.has_key(license.license_code):
        main_template = DEED_TEMPLATE_MAPPING[license.license_code]
    else:
        main_template = 'licenses/standard_deed.html'

    get_this = "/choose/results-one?license_code=%s&amp;jurisdiction=%s&amp;version=%s&amp;lang=%s" % (urllib.quote(license.license_code), license.jurisdiction.code, license.version, target_lang)

    context = {
        'request': request,
        'license_code': license.license_code,
        'license_code_quoted': urllib.quote(license.license_code),
        'license_title': license_title,
        'license': license,
        'multi_language': multi_language,
        'legalcodes': legalcodes,
        'color': color,
        'conditions': conditions,
        'active_languages': active_languages,
        'target_lang': target_lang,
        'jurisdiction':license.jurisdiction.code,
        'get_this': get_this,
        }
    context.update(util.rtl_context_stuff(target_lang))

    return Response(
        util.render_template(
            request, target_lang,
            main_template, context))
Beispiel #14
0
def active_languages():
    """Return a sequence of dicts, where each element consists of the
    following keys:

    * code: the language code
    * name: the translated name of this language

    for each available language."""
    global _ACTIVE_LANGUAGES
    if _ACTIVE_LANGUAGES:
        return _ACTIVE_LANGUAGES

    # get a list of avaialable translations
    domain = base.queryUtility(ITranslationDomain, ccorg_i18n_setup.I18N_DOMAIN)
    lang_codes = set(domain.getCatalogsInfo().keys())

    # determine the intersection of available translations and
    # launched jurisdiction locales
    launched_locales = set()
    jurisdictions = cclicense_functions.get_valid_jurisdictions()

    for jurisdiction in jurisdictions:
        query_string = (
            'PREFIX dc: <http://purl.org/dc/elements/1.1/> '
            'SELECT ?lang WHERE {'
            '  <%s> dc:language ?lang}') % jurisdiction

        query = RDF.Query(
            str(query_string),
            query_language='sparql')
        this_juri_locales = set(
            [locale_to_lower_upper(str(result['lang']))
             for result in query.execute(rdf_helper.JURI_MODEL)])

        # Append those locales that are applicable to this domain
        launched_locales.update(lang_codes.intersection(this_juri_locales))

    # XXX: Have to hack in Esperanto here because it's technically an
    # "Unported" language but there is no unported RDF jurisdiction in
    # jurisdictions.rdf..
    launched_locales.add('eo')

    # make our sequence have a predictable order
    launched_locales = list(launched_locales)

    # this loop is long hand for clarity; it's only done once, so
    # the additional performance cost should be negligible
    result = []
    for code in launched_locales:

        if code == 'test': continue

        gettext = ugettext_for_locale(negotiate_locale(code))
        name = gettext(mappers.LANG_MAP[code])
        result.append(dict(code=code, name=name))

    result = sorted(result, key=lambda lang: lang['name'].lower())

    _ACTIVE_LANGUAGES = result

    return result
Beispiel #15
0
    except AttributeError:
        # header was not defined
        header_value = default_locale
        target_lang = None
                              
    if not target_lang:
        # screw it
        target_lang = default_locale

    try:
        ACCEPT_LANG_CACHE[header_value] = target_lang
    except NameError, e:
        # We didn't set the header_value, so don't try to use it as a key
        pass

    return locale_to_lower_upper(target_lang)


def generate_404_response(request, routing, environ, staticdirector):
    """
    Create a 'nice looking' 404 response.
    """
    request.matchdict = {}
    request.urlgen = routes.URLGenerator(routing.mapping, environ)
    request.staticdirect = staticdirector

    target_lang = get_target_lang_from_request(request)

    context = {'page_style': 'bare'}
    context.update(rtl_context_stuff(target_lang))
Beispiel #16
0
 def title(self, language='en'):
     if self._titles is None:
         self._titles = rdf_helper.get_titles(self.uri)
     i18n_title = self._titles['i18n']
     return inverse_translate(i18n_title, locale_to_lower_upper(language))