Files
yfinance-fork/tests/test_ticker_locale.py
Alessandro Colace 97bdabe9dc Allow lang and region scoping for Ticker via YfConfig
Yahoo's v7 (`/v7/quote`) and v10 (`/quoteSummary`) endpoints accept
`lang` and `region` parameters and return localized fields (`longName`,
`shortName`, ...) when the ticker has been translated for that locale —
e.g. 1810.HK returns '小米集團-W' under `lang=zh-Hant-HK`, GAZP.ME
returns 'Публичное акционерное общество Газпром' under `lang=ru-RU`.
Previously these params were not exposed and `Ticker.info["longName"]`
always came back in U.S. English.

Surfaced as global config (`YfConfig.locale.lang` / `YfConfig.locale.region`)
rather than per-Ticker constructor params: a typical user wants the
same locale for every call in their session, and the config approach
also covers the v10 calendar/earnings endpoint in `_fetch_history_metadata`
without threading kwargs through three layers.

- Add `YfConfig.locale.lang` (default `"en-US"`) and `YfConfig.locale.region`
  (default `"US"`).
- `Quote._fetch` (v10/quoteSummary) and `Quote._fetch_additional_info`
  (v7/quote) read the locale from `YfConfig` on every call.
- `_fetch_history_metadata` (v1 calendar) reads the same locale, replacing
  a hardcoded `lang=en-US, region=US`.
- The `v8/chart` endpoint ignores `lang` server-side, so we don't forward
  there — would only generate noise.
- Verified live for Latin/diacritics (it-IT), CJK (zh-Hant-HK, ja-JP),
  Cyrillic (ru-RU); Apple under ja-JP correctly stays "Apple Inc." since
  it's a U.S. listing.

Closes #2582
2026-05-15 17:13:22 +02:00

55 lines
2.0 KiB
Python

"""Verify YfConfig.locale switches the lang/region forwarded to Yahoo."""
from tests.context import yfinance as yf
import unittest
class TestTickerLocale(unittest.TestCase):
def setUp(self):
self._backup_lang = yf.config.locale.lang
self._backup_region = yf.config.locale.region
def tearDown(self):
yf.config.locale.lang = self._backup_lang
yf.config.locale.region = self._backup_region
def test_default_locale_is_en_us(self):
self.assertEqual(yf.config.locale.lang, "en-US")
self.assertEqual(yf.config.locale.region, "US")
def test_default_returns_english(self):
# Default locale (en-US/US) returns the U.S. English long name.
self.assertEqual(yf.Ticker("1810.HK").info["longName"], "Xiaomi Corporation")
def test_locale_zh_hant_hk(self):
yf.config.locale.lang = "zh-Hant-HK"
yf.config.locale.region = "HK"
# 小米集團-W
self.assertEqual(yf.Ticker("1810.HK").info["longName"], "小米集團-W")
def test_locale_ja_jp(self):
yf.config.locale.lang = "ja-JP"
yf.config.locale.region = "JP"
# トヨタ自動車
self.assertEqual(yf.Ticker("7203.T").info["longName"], "トヨタ自動車")
def test_locale_ru_ru(self):
yf.config.locale.lang = "ru-RU"
yf.config.locale.region = "RU"
# Публичное акционерное общество Газпром
name = yf.Ticker("GAZP.ME").info.get("longName")
if name is None:
self.skipTest("GAZP.ME longName unavailable from Yahoo")
self.assertIn("Газпром", name) # contains "Газпром"
def test_locale_us_listing_not_translated(self):
# Yahoo only translates fields for natively-listed companies.
# Apple's U.S. listing has no Japanese translation.
yf.config.locale.lang = "ja-JP"
yf.config.locale.region = "JP"
self.assertEqual(yf.Ticker("AAPL").info["longName"], "Apple Inc.")
if __name__ == "__main__":
unittest.main()