def get_translation(translation_json_string, lang=None): ''' Returns the given JSON translation string in correct language. :param translation_json_string: a json string containing translations, i.e. title :param lang: language of the translation :return: ''' try: json_data = json.loads(translation_json_string) except (ValueError, TypeError): return translation_json_string # if no language is given as a parameter, fetch the currently used if not lang: lang = h.lang() # convert ISO639-1 to ISO639-2 (fi -> fin, en -> eng) lang = convert_language_code(lang, 'alpha3') # return the given language if it is found, # otherwise return the next one from the defaults list defaults = [lang, 'eng', 'fin'] for lang in defaults: translation = json_data.get(lang) if translation: return translation if not translation: try: return json_data.itervalues().next() except StopIteration: return
def get_label_for_uri(uri, ontology=None, lang=None): ''' Return a label for an uri :param uri: single uri to get a label for :param ontology: ontology to use or none :param lang: language of the label. If not provided, uses the language of environment :return: resolved label by given language or original string if uri can not be resolved ''' if not isinstance(uri, basestring) or not uri.startswith("http://www.yso.fi"): return uri try: if not lang: lang = h.lang() except TypeError: lang = config.get('ckan.locale_default', 'en') try: labels = get_labels_for_uri(uri, ontology) except TypeError: labels = get_labels_for_uri_nocache(uri, ontology) if labels: for label in labels: if label.get('lang') == lang: return label.get('value') return uri
def template(self, dataset_type, lang, owner_org): if lang != h.lang(): abort(404, _('Not found')) lc = ckanapi.LocalCKAN(username=c.user) try: dataset = lc.action.recombinant_show( dataset_type=dataset_type, owner_org=owner_org) org = lc.action.organization_show( id=owner_org, include_datasets=False) except NotFound: abort(404, _('Not found')) book = excel_template(dataset_type, org) blob = StringIO() book.save(blob) response.headers['Content-Type'] = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' response.headers['Content-Disposition'] = ( 'inline; filename="{0}_{1}_{2}.xlsx"'.format( dataset['owner_org'], lang, dataset['dataset_type'])) return blob.getvalue()
def get_label_for_uri(uri, ontology=None, lang=None): ''' Return a label for an uri :param uri: single uri to get a label for :param ontology: ontology to use or none :param lang: language of the label. If not provided, uses the language of environment :return: resolved label by given language or original string if uri can not be resolved ''' if not isinstance(uri, basestring) or not uri.startswith("http://www.yso.fi"): return uri try: if not lang: lang = h.lang() except TypeError: lang = config.get('ckan.locale_default', 'en') try: labels = get_labels_for_uri(uri, ontology) except TypeError: labels = get_labels_for_uri_nocache(uri, ontology) if labels: for label in labels: if label.get('lang') == lang: return label.get('value') # If no label for the language is found, use the first one available return labels[0].get('value') return uri
def get_translation_from_extras(package): ''' Fetch the translation string from the extras, in case the title is of the old type. This function ensures that the legacy title is shown in right language even though the package hasn't gone through the converter in show_package_schema or create_package_schema yet (i.e. in package_item.html). :param package: package dict :param default: default value to return, if there is no matching value to the language :return: translated value ''' # Try to translate the valid package title, if it doesn't work, # we need to fetch the title from extras try: t = package.get("title", "") json.loads(t) return get_translation(t) except (ValueError, TypeError): pass ret = "" lang = convert_language_code(h.lang(), 'alpha3') # fi -> fin langlist = list() # an ordered list of title languages valuelist = list() # an ordered list of titles if package.get('extras') and lang: for extra in package.get('extras'): for key, value in extra.iteritems(): if value.startswith("lang_title"): # fetch the language of the given title langlist.insert(int(value.split('_')[2]), extra['value']) if value.startswith("title"): # fetch the title valuelist.insert(int(value.split('_')[1]), extra['value']) try: ret = valuelist[langlist.index(lang)] except: log.debug('List index was probably out of range') if valuelist: # use the first title given, if any are given at all ret = valuelist[0] return ret