python - 获取提供的区域设置的货币名称 (Python)

标签 python locale

我正在寻找一种方法来获取所提供的区域设置的货币名称。 例如:

currency_name = get_currency_name(locale = "en_US")
print(currency_name) --> "US Dollar" 

一直在互联网上寻找答案,但找不到任何真正有用的东西。 这是我目前为止的情况:

import pycountry

country = pycountry.countries.get(name='Norway')
currency = pycountry.currencies.get(numeric=country.numeric)
currency_name = currency.name
print(currency_name) --> "Norwegian Krone"

谢谢!

最佳答案

import locale
import babel
from babel import numbers

def get_currency_name(loc):
  l = locale.setlocale(locale.LC_ALL, '')
  locale.setlocale(locale.LC_ALL, loc)
  sym = locale.localeconv()['int_curr_symbol']
  print(babel.numbers.get_currency_name(sym, locale='en_US'))
  locale.setlocale(locale.LC_ALL, l)

get_currency_name('en_US')
get_currency_name('zh')
get_currency_name('de')

输出为

US Dollar
Chinese Yuan
Euro

关于python - 获取提供的区域设置的货币名称 (Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52793512/

相关文章:

python - 在 PyQt Designer 中实现 LED

Python:如何在 POST 请求中发送 html 代码

未加载 Android 语言环境文件

c++ - 如何获取 C/C++ 中的系统语言?

python - Brecknell 量表的多个可重现错误

python - Flaskr 语法错误

python - 在索引和值 "for i, v in enumerate(listVar):"上是否有 C++/C++11 类似于 python 迭代?

java - 在android中动态创建 "values-en"文件夹

jQuery Timeago 时间戳 : How to choose language?

macos - 无法在vim中显示所有字符