Skip to content

format_currency only ever retrieves the default locale currency format #201

Closed

Description

@craigloftus

format_currency always passes None to locale.currency_formats, which means that it only supports the default currency format for the given locale.

def format_currency(number, currency, format=None, locale=LC_NUMERIC):
    locale = Locale.parse(locale)
    if not format:
        format = locale.currency_formats.get(format)
    pattern = parse_pattern(format)
    return pattern.apply(number, locale, currency=currency)

Shouldn't this be:

if not format:
    locale_format = locale.currency_formats.get(currency)
    format = locale_format or locale.currency_formats.get(None)

Which would allow me to do something simple like:

Locale('en', 'US').currency_formats['KES'] = kes_format

Activity

  1. etanol commented on Aug 14, 2015

    @etanol
    Contributor

    I think the CLDR importing script needs to be also modified, as I haven't found any data file (in pickle format) that defines multiple currency formats, other than None.

    @sils1297 I can take a look at this, if I may.

  2. sils commented on Aug 14, 2015

    @sils
    Member

    @etanol sure, go ahead. I can't assign you because of github having a bad access management. Many thanks for your support.

  3. etanol commented on Aug 15, 2015

    @etanol
    Contributor

    After diggin up a bit, it indeed looks that the CLDR import script is not using the type attribute from the correct element. It is searching for it at <currencyFormatLength> elements where it should be at <currencyFormat>, child element of the former.

    However, there is now alias resolution involved (added in CLDR >23), so it is going to be a bit more complex and take a little longer to implement. I'm still dedicating some time when I can.

  4. craigloftus commented on Aug 15, 2015

    @craigloftus
    ContributorAuthor

    @etanol If you focus on the import script I could work on a separate PR for format_currency function itself, would probably be able to do it on Monday.

  5. etanol commented on Aug 16, 2015

    @etanol
    Contributor

    @craigloftus Sure, no problem. Just keep in mind that in CLDR 26, currency formats are always typed (i.e. they have a type attribute), so a format with key None will not exist anymore. Basically, in CLDR 26 there are two types: standard and accounting.

    You can verify it, by executing the following from the root of the repository, after executing the actual import script:

    grep -h '<currencyFormat[> ]' cldr/common/main/*.xml | sort -u
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions