Example #1
0
 def test_description(self):
     description = tags.description('nl-BE')
     self.assertIn('Dutch', description)
     self.assertIn('Flemish', description)
     self.assertIn('Belgium', description)
     description = tags.description('az-Arab')
     self.assertIn('Azerbaijani in Arabic script', description)
     description = tags.description('123')
     self.assertEqual(0, len(description))
     description = tags.description('vls')
     self.assertIn('Vlaams', description)
 def test_description(self):
     description = tags.description('nl-BE')
     self.assertIn('Dutch', description)
     self.assertIn('Flemish', description)
     self.assertIn('Belgium', description)
     description = tags.description('az-Arab')
     self.assertIn('Azerbaijani in Arabic script', description)
     self.assertEqual(len(description), len(tags.description('az-Arab')))
     description = tags.description('123')
     self.assertEqual(0, len(description))
     description = tags.description('vls')
     self.assertIn('Vlaams', description)
Example #3
0
def standardize_language(code):
    """Match `code` to a standard RFC5646 or RFC3066 language. The following
    approaches are tried in order:
    * Match a RFC5646 language string.
    * Match a RFC3066 language string.
    * Use a ISO-6639/2 bibliographic synonym, and match a RFC3066 language
    string for the ISO-6639/2 terminological code.
    If no results are found, `None` is returned.

    http://www.idpf.org/epub/30/spec/epub30-publications.html#sec-opf-dclanguage
    http://www.idpf.org/epub/20/spec/OPF_2.0.1_draft.htm#Section2.2.12

    :param code: string with a language code ('en-GB', ...)
    :returns: `LanguageTuple` with the RFC5646 code and the list of description
    tags, or `None` if the language could not be identified.
    """
    if not code:
        return None

    # Try RFC5646 (for EPUB 3).
    if tags.check(code):
        return LanguageTuple(code=code.lower(),
                             description=tags.description(code))

    # Try RFC3066 (for EPUB 2).
    # Try to get the ISO639-1 code for the language.
    try:
        lang = languages.get(iso639_2T_code=code)
        new_code = lang.iso639_1_code
    except KeyError:
        # Try synonym.
        if code in ISO_6639_2_B.keys():
            try:
                lang = languages.get(iso639_2T_code=ISO_6639_2_B[code])
                new_code = lang.iso639_1_code
            except KeyError:
                return None
        else:
            return None

    # Try RFC5646 for the ISO639-1 code.
    if tags.check(new_code):
        return LanguageTuple(code=new_code.lower(),
                             description=tags.description(new_code))
    return None
Example #4
0
    def get(self):
        lang = flask.request.args.get('language', default='en')
        page = flask.request.args.get('page', default=1, type=int)
        page_size = flask.request.args.get('page-size', default=10, type=int)

        formatted_lang = tags.tag(lang).format
        lang_name = tags.description(lang)[0] if tags.check(lang) else None

        return game_repository.all_v2(formatted_lang, lang_name, page,
                                      page_size)
Example #5
0
def label_lang_rule(errors, node, request, labels):
    for label in labels:
        language_tag = label['language']
        if not tags.check(language_tag):
            errors.append(colander.Invalid(
                node['labels'],
                'Invalid language tag: %s' % ", ".join([err.message for err in tags.tag(language_tag).errors])
            ))
        else:
            languages_present = request.db.query(Language).filter_by(id=language_tag).count()
            if not languages_present:
                descriptions = ', '.join(tags.description(language_tag))
                language_item = Language(id=language_tag, name=descriptions)
                request.db.add(language_item)
Example #6
0
def label_lang_rule(errors, node, request, labels):
    for label in labels:
        language_tag = label['language']
        if not tags.check(language_tag):
            errors.append(
                colander.Invalid(
                    node['labels'], 'Invalid language tag: %s' % ", ".join(
                        [err.message
                         for err in tags.tag(language_tag).errors])))
        else:
            languages_present = request.db.query(Language).filter_by(
                id=language_tag).count()
            if not languages_present:
                descriptions = ', '.join(tags.description(language_tag))
                language_item = Language(id=language_tag, name=descriptions)
                request.db.add(language_item)
def _check_language(language_tag, session):
    '''
    Checks if a certain language is already present, if not import.

    :param string language_tag: IANA language tag
    :param session: Database session to use
    :rtype: :class:`skosprovider_sqlalchemy.models.Language`
    '''
    if not language_tag:
        language_tag = 'und'
    l = session.query(LanguageModel).get(language_tag)
    if not l:
        if not tags.check(language_tag):
            raise ValueError('Unable to import provider. Invalid language tag: %s' % language_tag)
        descriptions = ', '.join(tags.description(language_tag))
        l = LanguageModel(id=language_tag, name=descriptions)
        session.add(l)
    return l
def label_lang_rule(errors, node, languages_manager, labels):
    """
    Checks that languages of a label are valid.

    Checks that they are valid IANA language tags. If the language tag was not
    already present in the database, it adds them.
    """
    for label in labels:
        language_tag = label['language']
        if not tags.check(language_tag):
            errors.append(colander.Invalid(
                node['labels'],
                'Invalid language tag: %s' % ", ".join([err.message for err in tags.tag(language_tag).errors])
            ))
        else:
            languages_present = languages_manager.count_languages(language_tag)
            if not languages_present:
                descriptions = ', '.join(tags.description(language_tag))
                language_item = Language(id=language_tag, name=descriptions)
                languages_manager.save(language_item)
Example #9
0
def _check_language(language_tag, session):
    '''
    Checks if a certain language is already present, if not import.

    :param string language_tag: IANA language tag
    :param session: Database session to use
    :rtype: :class:`skosprovider_sqlalchemy.models.Language`
    '''
    if not language_tag:
        language_tag = 'und'
    l = session.query(LanguageModel).get(language_tag)
    if not l:
        if not tags.check(language_tag):
            raise ValueError(
                'Unable to import provider. Invalid language tag: %s' %
                language_tag)
        descriptions = ', '.join(tags.description(language_tag))
        l = LanguageModel(id=language_tag, name=descriptions)
        session.add(l)
    return l
Example #10
0
def label_lang_rule(errors, node, languages_manager, labels):
    """
    Checks that languages of a label are valid.

    Checks that they are valid IANA language tags. If the language tag was not
    already present in the database, it adds them.
    """
    for label in labels:
        language_tag = label['language']
        if not tags.check(language_tag):
            errors.append(
                colander.Invalid(
                    node['labels'], 'Invalid language tag: %s' % ", ".join(
                        [err.message
                         for err in tags.tag(language_tag).errors])))
        else:
            languages_present = languages_manager.count_languages(language_tag)
            if not languages_present:
                descriptions = ', '.join(tags.description(language_tag))
                language_item = Language(id=language_tag, name=descriptions)
                languages_manager.save(language_item)
Example #11
0
 def get(self):
     languages = [item['language'] for item in GDLConfig.GAMES_TABLE.scan()['Items']]
     unique = list(dict.fromkeys(languages))
     return [{'code': x, 'description': ', '.join(tags.description(x))} for x in unique]