Beispiel #1
0
def cached_i18n_name(dcids, locale, should_resolve_all):
    """Returns localization names for set of dcids.

    Args:
        dcids: ^ separated string of dcids. It must be a single string for the cache.
        locale: the desired localization language code.
        should_resolve_all: True if every dcid should be returned with a
                            name, False if only i18n names should be filled

    Returns:
        A dictionary of place names, keyed by dcid (potentially sparse if should_resolve_all=False)
    """
    if not dcids:
        return {}
    dcids = dcids.split('^')
    response = fetch_data('/node/property-values', {
        'dcids': dcids,
        'property': 'nameWithLanguage',
        'direction': 'out'
    },
                          compress=False,
                          post=True)
    result = {}
    dcids_default_name = []
    locales = i18n.locale_choices(locale)
    for dcid in dcids:
        values = response[dcid].get('out')
        # If there is no nameWithLanguage for this dcid, fall back to name.
        if not values:
            dcids_default_name.append(dcid)
            continue
        result[dcid] = ''
        for locale in locales:
            for entry in values:
                if has_locale_name(entry, locale):
                    result[dcid] = extract_locale_name(entry, locale)
                    break
            if result[dcid]:
                break
    if dcids_default_name:
        if should_resolve_all:
            default_names = cached_name('^'.join(sorted(dcids_default_name)))
        else:
            default_names = {}
        for dcid in dcids_default_name:
            result[dcid] = default_names.get(dcid, '')
    return result
Beispiel #2
0
 def before_request():
     requested_locale = request.args.get('hl', i18n.DEFAULT_LOCALE)
     g.locale_choices = i18n.locale_choices(requested_locale)
     g.locale = g.locale_choices[0]