2

I need to get the iso currency symbol, without using the built-in python locale module.

The locale module needs to be the locale installed at the OS. I want to run my code on a serverless environment, so install any OS additional package is an obstacle.

What I need is equivalent to that:

import locale
locale.setlocale(locale.LC_ALL, ('en_US', 'utf-8'))
currency_symbol = locale.localeconv()['int_curr_symbol']
HHead26
  • 69
  • 1
  • 5
  • Why the restriction? – CristiFati Mar 25 '19 at 18:31
  • What additional package do you think is involved? – chepner Mar 25 '19 at 18:38
  • I need to get the currency code without installing any additional package in the OS. For example, if I try to set a not installed locale the error: `locale.Error: unsupported locale setting` is raised. I need to use some lib without dependency on the OS to get the currency symbol. – HHead26 Mar 25 '19 at 18:39
  • Assuming you have internet on the server, you could do something like finding your geographical coordinates by pinging a website. For example: https://stackoverflow.com/a/24907340/8150685 (Though you will have to find a different website). Or if you can install packages you can use the answer below that one: https://stackoverflow.com/a/43589918/8150685. Then use those coordinates to find the right currency symbol. – Error - Syntactical Remorse Mar 25 '19 at 21:03
  • You can find the right currency symbol either by using a lookup table or through another API call such as: http://usercountry.com/. (which this API can just use your IP to return the needed data). – Error - Syntactical Remorse Mar 25 '19 at 21:10

1 Answers1

0

I resolved this issue using the Babel library:

from babel import Locale
from babel.numbers import get_territory_currencies
from datetime import datetime


current_locale = Locale.parse('en_US')
current_date = datetime.utcnow().date
currency_symbol = get_territory_currencies(
    current_locale.territory,
    current_date,
    current_date)[0]

A limitation for this solution is that the locale code should have the territory.

HHead26
  • 69
  • 1
  • 5