Skip to content

Commit 616bbc2

Browse files
committed
Use shared notification localization
1 parent e1100cb commit 616bbc2

3 files changed

Lines changed: 23 additions & 164 deletions

File tree

application/rebalance_service.py

Lines changed: 11 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -16,84 +16,17 @@
1616
from notifications.events import NotificationPublisher
1717
from notifications import renderers as notification_renderers
1818
from quant_platform_kit.common.models import PortfolioSnapshot, Position
19+
from quant_platform_kit.common.notification_localization import (
20+
localize_notification_text as _base_localize_notification_text,
21+
translator_uses_zh as _base_translator_uses_zh,
22+
)
1923
from quant_platform_kit.common.port_adapters import CallableNotificationPort, CallablePortfolioPort
2024

2125

22-
_ZH_REASON_REPLACEMENTS = (
23-
("feature snapshot guard blocked execution", "特征快照校验阻止执行"),
24-
("feature snapshot required", "需要特征快照"),
25-
("feature snapshot compute failed", "特征快照计算失败"),
26-
("feature_snapshot_download_failed", "特征快照下载失败"),
27-
("feature_snapshot_compute_failed", "特征快照计算失败"),
28-
("feature_snapshot_path_missing", "缺少特征快照路径"),
29-
("feature_snapshot_missing", "特征快照不存在"),
30-
("feature_snapshot_stale", "特征快照过旧"),
31-
("feature_snapshot_manifest_missing", "缺少快照清单"),
32-
("feature_snapshot_profile_mismatch", "快照策略名不匹配"),
33-
("feature_snapshot_config_name_mismatch", "快照配置名不匹配"),
34-
("feature_snapshot_config_path_mismatch", "快照配置路径不匹配"),
35-
("feature_snapshot_contract_version_mismatch", "快照契约版本不匹配"),
36-
("soxl_soxx_trend_income", "SOXL/SOXX 半导体趋势收益"),
37-
("tqqq_growth_income", "TQQQ 增长收益"),
38-
("global_etf_rotation", "全球 ETF 轮动"),
39-
("russell_1000_multi_factor_defensive", "罗素1000多因子"),
40-
("tech_communication_pullback_enhancement", "科技通信回调增强"),
41-
("qqq_tech_enhancement", "科技通信回调增强"),
42-
("mega_cap_leader_rotation_aggressive", "Mega Cap 激进龙头轮动"),
43-
("mega_cap_leader_rotation_dynamic_top20", "Mega Cap 动态 Top20 龙头轮动"),
44-
("mega_cap_leader_rotation_top50_balanced", "Mega Cap Top50 平衡龙头轮动"),
45-
("dynamic_mega_leveraged_pullback", "Mega Cap 2x 回调策略"),
46-
("outside_monthly_execution_window", "当前不在月度执行窗口"),
47-
("no_execution_window_after_snapshot", "快照后没有可用执行窗口"),
48-
("no-op", "不执行"),
49-
("monthly snapshot cadence", "月度快照节奏"),
50-
("waiting inside execution window", "等待进入执行窗口"),
51-
("small_account_warning=true", "小账户提示=是"),
52-
("portfolio_equity=", "净值="),
53-
("min_recommended_equity=", "建议最低净值="),
54-
(
55-
"integer_shares_min_position_value_may_prevent_backtest_replication",
56-
"整数股和最小仓位限制可能导致实盘无法完全复现回测",
57-
),
58-
(
59-
"integer-share minimum position sizing may prevent backtest replication",
60-
"整数股和最小仓位限制可能导致实盘无法完全复现回测",
61-
),
62-
("small account warning: portfolio equity", "小账户提示:净值"),
63-
("small account warning", "小账户提示"),
64-
("is below the recommended", "低于建议"),
65-
("is below recommended", "低于建议"),
66-
("snapshot_as_of=", "快照日期="),
67-
("snapshot=", "快照日期="),
68-
("allowed=", "允许日期="),
69-
("<unknown>", "未知"),
70-
("<none>", "无"),
71-
("RISK-ON", "风险开启"),
72-
("DE-LEVER", "降杠杆"),
73-
("regime=hard_defense", "市场阶段=强防御"),
74-
("regime=soft_defense", "市场阶段=软防御"),
75-
("regime=risk_on", "市场阶段=进攻"),
76-
("benchmark_trend=down", "基准趋势=向下"),
77-
("benchmark_trend=up", "基准趋势=向上"),
78-
("benchmark=down", "基准趋势=向下"),
79-
("benchmark=up", "基准趋势=向上"),
80-
("breadth=", "市场宽度="),
81-
("target_stock=", "目标股票仓位="),
82-
("realized_stock=", "实际股票仓位="),
83-
("stock_exposure=", "股票目标仓位="),
84-
("safe_haven=", "避险仓位="),
85-
("selected=", "入选标的数="),
86-
("top=", "前排标的="),
87-
("no_selection", "无入选标的"),
88-
("outside_execution_window", "当前不在执行窗口"),
26+
_EXTRA_ZH_REASON_REPLACEMENTS = (
8927
("pending_orders_detected", "检测到未完成订单"),
9028
("same_day_execution_locked", "当日执行锁已存在"),
9129
("same_day_fills_detected", "检测到当日成交"),
92-
("insufficient_buying_power", "购买力不足"),
93-
("missing_price", "缺少报价"),
94-
("no_equity", "无净值"),
95-
("fail_closed", "关闭执行"),
96-
("reason=", "原因="),
9730
("fail_reason=", "失败原因="),
9831
("decision=", "决策="),
9932
)
@@ -117,18 +50,15 @@ def _format_symbol_preview(symbols, *, limit: int = 3) -> str:
11750

11851

11952
def _translator_uses_zh(translator) -> bool:
120-
sample = str(translator("no_trades"))
121-
return any("\u4e00" <= ch <= "\u9fff" for ch in sample)
53+
return _base_translator_uses_zh(translator)
12254

12355

12456
def _localize_notification_text(text: str, *, translator) -> str:
125-
value = str(text or "").strip()
126-
if not value or not _translator_uses_zh(translator):
127-
return value
128-
localized = value
129-
for source, target in _ZH_REASON_REPLACEMENTS:
130-
localized = localized.replace(source, target)
131-
return localized
57+
return _base_localize_notification_text(
58+
text,
59+
translator=translator,
60+
extra_replacements=_EXTRA_ZH_REASON_REPLACEMENTS,
61+
)
13262

13363

13464
def _render_notification_context_text(

notifications/renderers.py

Lines changed: 11 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -6,83 +6,15 @@
66
import re
77

88
from notifications.events import RenderedNotification
9+
from quant_platform_kit.common.notification_localization import (
10+
localize_notification_text as _base_localize_notification_text,
11+
translator_uses_zh as _base_translator_uses_zh,
12+
)
913

10-
11-
_ZH_REASON_REPLACEMENTS = (
12-
("feature snapshot guard blocked execution", "特征快照校验阻止执行"),
13-
("feature snapshot required", "需要特征快照"),
14-
("feature snapshot compute failed", "特征快照计算失败"),
15-
("feature_snapshot_download_failed", "特征快照下载失败"),
16-
("feature_snapshot_compute_failed", "特征快照计算失败"),
17-
("feature_snapshot_path_missing", "缺少特征快照路径"),
18-
("feature_snapshot_missing", "特征快照不存在"),
19-
("feature_snapshot_stale", "特征快照过旧"),
20-
("feature_snapshot_manifest_missing", "缺少快照清单"),
21-
("feature_snapshot_profile_mismatch", "快照策略名不匹配"),
22-
("feature_snapshot_config_name_mismatch", "快照配置名不匹配"),
23-
("feature_snapshot_config_path_mismatch", "快照配置路径不匹配"),
24-
("feature_snapshot_contract_version_mismatch", "快照契约版本不匹配"),
25-
("soxl_soxx_trend_income", "SOXL/SOXX 半导体趋势收益"),
26-
("tqqq_growth_income", "TQQQ 增长收益"),
27-
("global_etf_rotation", "全球 ETF 轮动"),
28-
("russell_1000_multi_factor_defensive", "罗素1000多因子"),
29-
("tech_communication_pullback_enhancement", "科技通信回调增强"),
30-
("qqq_tech_enhancement", "科技通信回调增强"),
31-
("mega_cap_leader_rotation_aggressive", "Mega Cap 激进龙头轮动"),
32-
("mega_cap_leader_rotation_dynamic_top20", "Mega Cap 动态 Top20 龙头轮动"),
33-
("mega_cap_leader_rotation_top50_balanced", "Mega Cap Top50 平衡龙头轮动"),
34-
("dynamic_mega_leveraged_pullback", "Mega Cap 2x 回调策略"),
35-
("outside_monthly_execution_window", "当前不在月度执行窗口"),
36-
("no_execution_window_after_snapshot", "快照后没有可用执行窗口"),
37-
("no-op", "不执行"),
38-
("monthly snapshot cadence", "月度快照节奏"),
39-
("waiting inside execution window", "等待进入执行窗口"),
40-
("small_account_warning=true", "小账户提示=是"),
41-
("portfolio_equity=", "净值="),
42-
("min_recommended_equity=", "建议最低净值="),
43-
(
44-
"integer_shares_min_position_value_may_prevent_backtest_replication",
45-
"整数股和最小仓位限制可能导致实盘无法完全复现回测",
46-
),
47-
(
48-
"integer-share minimum position sizing may prevent backtest replication",
49-
"整数股和最小仓位限制可能导致实盘无法完全复现回测",
50-
),
51-
("small account warning: portfolio equity", "小账户提示:净值"),
52-
("small account warning", "小账户提示"),
53-
("is below the recommended", "低于建议"),
54-
("is below recommended", "低于建议"),
55-
("snapshot_as_of=", "快照日期="),
56-
("snapshot=", "快照日期="),
57-
("allowed=", "允许日期="),
58-
("<unknown>", "未知"),
59-
("<none>", "无"),
60-
("RISK-ON", "风险开启"),
61-
("DE-LEVER", "降杠杆"),
62-
("regime=hard_defense", "市场阶段=强防御"),
63-
("regime=soft_defense", "市场阶段=软防御"),
64-
("regime=risk_on", "市场阶段=进攻"),
65-
("benchmark_trend=down", "基准趋势=向下"),
66-
("benchmark_trend=up", "基准趋势=向上"),
67-
("benchmark=down", "基准趋势=向下"),
68-
("benchmark=up", "基准趋势=向上"),
69-
("breadth=", "市场宽度="),
70-
("target_stock=", "目标股票仓位="),
71-
("realized_stock=", "实际股票仓位="),
72-
("stock_exposure=", "股票目标仓位="),
73-
("safe_haven=", "避险仓位="),
74-
("selected=", "入选标的数="),
75-
("top=", "前排标的="),
76-
("no_selection", "无入选标的"),
77-
("outside_execution_window", "当前不在执行窗口"),
14+
_EXTRA_ZH_REASON_REPLACEMENTS = (
7815
("pending_orders_detected", "检测到未完成订单"),
7916
("same_day_execution_locked", "当日执行锁已存在"),
8017
("same_day_fills_detected", "检测到当日成交"),
81-
("insufficient_buying_power", "购买力不足"),
82-
("missing_price", "缺少报价"),
83-
("no_equity", "无净值"),
84-
("fail_closed", "关闭执行"),
85-
("reason=", "原因="),
8618
("fail_reason=", "失败原因="),
8719
("decision=", "决策="),
8820
)
@@ -106,18 +38,15 @@ def _format_symbol_preview(symbols, *, limit: int = 3) -> str:
10638

10739

10840
def _translator_uses_zh(translator) -> bool:
109-
sample = str(translator("no_trades"))
110-
return any("\u4e00" <= ch <= "\u9fff" for ch in sample)
41+
return _base_translator_uses_zh(translator)
11142

11243

11344
def _localize_notification_text(text: str, *, translator) -> str:
114-
value = str(text or "").strip()
115-
if not value or not _translator_uses_zh(translator):
116-
return value
117-
localized = value
118-
for source, target in _ZH_REASON_REPLACEMENTS:
119-
localized = localized.replace(source, target)
120-
return localized
45+
return _base_localize_notification_text(
46+
text,
47+
translator=translator,
48+
extra_replacements=_EXTRA_ZH_REASON_REPLACEMENTS,
49+
)
12150

12251

12352
def _split_detail_segment(text: str) -> list[str]:

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
flask
22
gunicorn
3-
quant-platform-kit @ git+https://github.com/QuantStrategyLab/QuantPlatformKit.git@ac550fee3ca33bbf28c8a886848dede4e4e09a1b
3+
quant-platform-kit @ git+https://github.com/QuantStrategyLab/QuantPlatformKit.git@c24d4c52e84c8c696006532590b15e9be92c8d89
44
us-equity-strategies @ git+https://github.com/QuantStrategyLab/UsEquityStrategies.git@9f55c58b6854ae700c47a684262eec10e763306a
55
pandas
66
numpy

0 commit comments

Comments
 (0)