def format_currency(value, meta, currency_locale=None):
    """
    Formats currency value into properly decorated currency string based on either locale (preferred)
    or if that is not available then currency metadata. Until locale is provided from the server
    a crude mapping between `currency.dxCode` and a locale string is used instead (eg 0: 'en_US')

    :param value: amount
    :param meta: server metadata (`currency`)
    :return: formatted currency string
    """
    try:
        if currency_locale is None:
            currency_locale = locale_from_currency_code(meta['dxCode'])
        if currency_locale is None:
            return format_currency_from_meta(value, meta)
        else:
            locale.setlocale(locale.LC_ALL, currency_locale)
            return locale.currency(value, grouping=True)
    except locale.Error:
        # .. locale is probably not available -> fallback to format manually
        return format_currency_from_meta(value, meta)