Example #1
0
def scheming_language_text_or_empty(text, prefer_lang=None):
    """
    :param text: {lang: text} dict or text string
    :param prefer_lang: choose this language version if available

    Convert "language-text" to users' language by looking up
    language in dict or using gettext if not a dict
    """
    if not text:
        return u''

    if hasattr(text, 'get'):
        try:
            if prefer_lang is None:
                prefer_lang = lang()
        except:
            pass  # lang() call will fail when no user language available
        else:
            if prefer_lang in _LOCALE_ALIASES:
                prefer_lang = _LOCALE_ALIASES[prefer_lang]
            try:
                return text[prefer_lang]
            except KeyError:
                return ''

    t = gettext(text)
    if isinstance(t, str):
        return t.decode('utf-8')
    return t
Example #2
0
def dge_dataset_form_lang_and_value(text):
    """
    :param text: {lang: text} dict or text string

    Convert "language-text" to users' language by looking up
    languag in dict, if the text
    doesn't exit look for an available text
    """
    if not text:
        return {'': u''}

    if hasattr(text, 'get'):
        final_text = u''
        try:
            prefer_lang = lang()
        except:
            prefer_lang = config.get('ckan.locale_default', 'es')
        else:
            try:
                prefer_lang = str(prefer_lang)
                final_text = text[prefer_lang]
            except KeyError:
                pass

        if not final_text:
            locale_order = config.get('ckan.locale_order', '').split()
            for l in locale_order:
                if l in text and text[l]:
                    final_text = text[l]
                    prefer_lang = l
                    break

        return {prefer_lang: final_text}

    return {'': u''}
Example #3
0
def dge_dataset_field_value(text):
    """
    :param text: {lang: text} dict or text string

    Convert "language-text" to users' language by looking up
    language in dict or using gettext if not a dict but. If the text
    doesn't exist look for an available text
    """
    value = None
    language = None
    if not text:
        result = u''

    dict_text = dsh.dge_dataset_form_lang_and_value(text)
    if (dict_text):
        language = sh.lang()
        if (dict_text.has_key(language) and dict_text[language] and \
            dict_text[language].strip() != ''):
            value = dict_text[language]
            language = None
        else:
            for key in dict_text:
                if (dict_text[key] and dict_text[key].strip() != ''):
                    value = (dict_text[key])
                    language = key
                    break
    return language, value
Example #4
0
def dge_dataset_form_value(text):
    """
    :param text: {lang: text} dict or text string

    Convert "language-text" to users' language by looking up
    languag in dict or using gettext if not a dict but. If the text
    doesn't exist look for an available text
    """
    if not text:
        return u''

    if hasattr(text, 'get'):
        final_text = u''
        try:
            prefer_lang = lang()
        except:
            prefer_lang = config.get('ckan.locale_default', 'es')
        else:
            try:
                final_text = text[prefer_lang]
            except KeyError:
                pass

        if not final_text:
            locale_order = config.get('ckan.locale_order', '').split()
            for l in locale_order:
                if l in text and text[l]:
                    final_text = text[l]
                    break
        return final_text

    t = gettext(text)
    if isinstance(t, str):
        return t.decode('utf-8')
    return t
    def _get_facet_item_label_with_translation(self, dataset_facet_field, default_facet_label):
        '''
        Translate the default label of facet item. Return the default facet label if no translation available
        :param dataset_facet_field: the name of facet field in the dataset
        :param default_facet_label: the default label of the facet item
        '''
        from ckanext.scheming import helpers as scheming_helpers
        package_type = self._get_dataset_type_of_facet(dataset_facet_field)
        schema = scheming_helpers.scheming_get_dataset_schema(package_type)

        # if a facet has `facet_items` and `dataset_type`, wins `facet_items`
        if self._get_facet_items_of_facet(dataset_facet_field, self.additional_facets) is None:

            # if schema exists
            if schema is not None:
                schema_name = dataset_facet_field
                #remove prefix in facet name
                schema_name = schema_name.replace('extras_', '')
                schema_name = schema_name.replace('res_extras_', '')

                # switch for dataset or resource
                if schema_name.startswith( 'res_' ) and 'resource_fields' in schema:
                    fields_from_schema = schema['resource_fields']
                elif 'dataset_fields' in schema:
                    fields_from_schema = schema['dataset_fields']
                else:
                    return self._translate_facet_item_label(dataset_facet_field, default_facet_label)

                for field in fields_from_schema:
                    if field['field_name'] == schema_name:
                        #if item key is given - see facet_list.html
                        if default_facet_label is not None:
                            if 'choices' in field:
                                return scheming_helpers.scheming_choices_label(field['choices'], default_facet_label)
                            elif 'choices_helper' in field:
                                from ckantoolkit import h
                                choices_fn = getattr(h, field['choices_helper'])
                                return scheming_helpers.scheming_choices_label(choices_fn(field), default_facet_label)
                            else:
                                return default_facet_label;
                        else:
                            if len(field['label']) > 1 and type(field['label']) is dict:
                                label_array = field['label']
                                language = scheming_helpers.lang()
                                for key, value in label_array.iteritems():
                                    if key == language:
                                        if value is not None:
                                            return value
                                        else:
                                            return default_facet_label
                            if field['label'] is not None:
                                return field['label']
                            else:
                                return default_facet_label

        return self._translate_facet_item_label(dataset_facet_field, default_facet_label)
Example #6
0
 def _pull_facet_title_from_schema(self, package_type, name, item_name,
                                   title):
     schema = scheming_helpers.scheming_get_dataset_schema(package_type)
     language = scheming_helpers.lang()
     schema_name = name
     schema_name = schema_name.replace('res_extras_', '')
     # switch for dataset or resource
     if schema_name.startswith('res_'):
         fields_from_schema = schema['resource_fields']
     else:
         fields_from_schema = schema['dataset_fields']
     for field in fields_from_schema:
         if field['field_name'] == schema_name:
             #if item key is given - see facet_list.html
             if item_name is not None:
                 if 'choices' in field:
                     for entry in field['choices']:
                         if entry['value'] == item_name:
                             if len(entry['label']) > 1 and type(
                                     entry['label']) is dict:
                                 label_array = entry['label']
                                 for key, value in label_array.iteritems():
                                     if key == language:
                                         if value is not None:
                                             return value
                                         else:
                                             return title
                                 if value is not None:
                                     return value
                                 else:
                                     return title
                 else:
                     return title
             else:
                 if len(field['label']) > 1 and type(
                         field['label']) is dict:
                     label_array = field['label']
                     for key, value in label_array.iteritems():
                         if key == language:
                             if value is not None:
                                 return value
                             else:
                                 return title
                     if value is not None:
                         return value
                     else:
                         return title
                 if field['label'] is not None:
                     return field['label']
                 else:
                     return title
     return title
Example #7
0
 def _pull_title_from_schema(self, package_type):
     language = scheming_helpers.lang()
     schema = scheming_helpers.scheming_get_dataset_schema(package_type)
     if 'dataset_type_label' in schema:
         if len(schema['dataset_type_label']) > 1 and type(
                 schema['dataset_type_label']) is dict:
             label_array = schema['dataset_type_label']
             for key, value in label_array.iteritems():
                 if key == language:
                     if value is not None:
                         return value
                     else:
                         return schema['dataset_type']
                     if value is not None:
                         return value
                     else:
                         return schema['dataset_type']
     else:
         return schema['dataset_type']
Example #8
0
def csc_scheming_language_text(text, prefer_lang=None):
    """
    :param text: {lang: text} dict or text string
    :param prefer_lang: choose this language version if available

    Convert "language-text" to users' language by looking up
    languag in dict or using gettext if not a dict
    """
    log.debug('[csc_scheming_language_text] text %s prefer_lang %s', text,
              prefer_lang)
    if not text:
        final_text = u''

    assert text != {}
    if hasattr(text, 'get'):
        try:
            if prefer_lang is None:
                prefer_lang = lang()
        except TypeError:
            prefer_lang = config.get('ckan.locale_default', 'es')
        else:
            try:
                final_text = text[prefer_lang]
            except KeyError:
                pass

        if not final_text:
            locale_order = config.get('ckan.locale_order', '').split()
            for l in locale_order:
                if l in text and text[l]:
                    final_text = text[l]
                    break
        log.debug('[csc_scheming_language_text] final_text %s', final_text)
        return final_text

    t = gettext(text)

    if isinstance(text, str):
        text = text.decode('utf-8')
    t = _(text)
    log.debug('[csc_scheming_language_text] t %s', t)
    return t