Skip to content

Commit 2764bf2

Browse files
authored
Localize signal snapshot source in notifications (#51)
1 parent 38c3249 commit 2764bf2

1 file changed

Lines changed: 56 additions & 7 deletions

File tree

notifications/telegram.py

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,57 @@
99
from quant_platform_kit.common.notification_localization import (
1010
localize_notification_text as _base_localize_notification_text,
1111
)
12+
13+
try:
14+
from quant_platform_kit.common.notification_localization import (
15+
localize_price_source_label as _localize_price_source_label,
16+
localize_quote_overlay_state as _localize_quote_overlay_state,
17+
)
18+
except ImportError: # pragma: no cover - compatibility with older pinned shared wheels
19+
_PRICE_SOURCE_LABELS = {
20+
"longbridge_candlesticks": ("LongBridge 日线K线", "LongBridge daily candlesticks"),
21+
"schwab_daily_history_with_live_quote_overlay": (
22+
"Schwab 日线历史 + 实时报价覆盖",
23+
"Schwab daily history + live quote overlay",
24+
),
25+
"firstrade_ohlc_with_live_quote_overlay": (
26+
"Firstrade OHLC + 实时报价覆盖",
27+
"Firstrade OHLC + live quote overlay",
28+
),
29+
"market_quote": ("实时行情报价", "market quote"),
30+
"mixed_market_quote_snapshot_close": (
31+
"实时行情报价 + 快照收盘价回补",
32+
"market quote + snapshot close fallback",
33+
),
34+
"mixed_market_quote_historical_close": (
35+
"实时行情报价 + 历史收盘价回补",
36+
"market quote + historical close fallback",
37+
),
38+
"snapshot_close": ("快照收盘价", "snapshot close"),
39+
"historical_close": ("历史收盘价", "historical close"),
40+
"market_data": ("市场数据", "market data"),
41+
}
42+
43+
def _locale_uses_zh(locale: str | None) -> bool:
44+
return str(locale or "").strip().lower().startswith("zh")
45+
46+
def _localize_price_source_label(value, *, translator=None, locale=None):
47+
source = str(value or "").strip()
48+
use_zh = _locale_uses_zh(locale)
49+
if not source:
50+
return "未知" if use_zh else "unknown"
51+
label = _PRICE_SOURCE_LABELS.get(source)
52+
if label is not None:
53+
return label[0] if use_zh else label[1]
54+
return source.replace("_", " ")
55+
56+
def _localize_quote_overlay_state(value, *, translator=None, locale=None):
57+
use_zh = _locale_uses_zh(locale)
58+
if value is True:
59+
return "是" if use_zh else "yes"
60+
if value is False:
61+
return "否" if use_zh else "no"
62+
return "未知" if use_zh else "unknown"
1263
try:
1364
from quant_platform_kit.common.small_account_compatibility import (
1465
format_small_account_cash_substitution_notes,
@@ -528,20 +579,18 @@ def _format_signal_snapshot_line(snapshot: Any, *, lang: str) -> str:
528579
return ""
529580
use_zh = str(lang or "").lower().startswith("zh")
530581
if use_zh:
531-
overlay_text = "是" if overlay is True else "否" if overlay is False else "未知"
532582
parts = [
533583
f"日期 {market_date or '未知'}",
534-
f"数据源 {source or '未知'}",
535-
f"报价覆盖 {overlay_text}",
584+
f"数据源 {_localize_price_source_label(source, locale=lang)}",
585+
f"报价覆盖 {_localize_quote_overlay_state(overlay, locale=lang)}",
536586
]
537587
if warning not in (None, "", False):
538-
parts.append(f"提示 {warning}")
588+
parts.append(f"提示 {_base_localize_notification_text(warning, translator=build_translator(lang))}")
539589
return "🧾 信号快照: " + " | ".join(parts)
540-
overlay_text = "yes" if overlay is True else "no" if overlay is False else "unknown"
541590
parts = [
542591
f"date {market_date or 'unknown'}",
543-
f"source {source or 'unknown'}",
544-
f"quote overlay {overlay_text}",
592+
f"source {_localize_price_source_label(source, locale=lang)}",
593+
f"quote overlay {_localize_quote_overlay_state(overlay, locale=lang)}",
545594
]
546595
if warning not in (None, "", False):
547596
parts.append(f"warning {warning}")

0 commit comments

Comments
 (0)