Files
syops/backend/app/services/shipping_notice/i18n.py
T

220 lines
13 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
services/shipping_notice/i18n.py
用途:维护发货通知链路的多语言文案与语言解析工具。
依赖:Flask request 上下文、模块内 `_MESSAGES` 文案字典。
逻辑:由 parse_summary、field_alignment_summary、workflow 提示文案等上游模块调用本文件输出用户可读消息。
"""
from __future__ import annotations
from typing import Any
from flask import has_request_context, request
SUPPORTED_LANGS = ("zh", "en", "th")
_MESSAGES: dict[str, dict[str, str]] = {
"zh": {
"parse.title": "【发货通知解析确认】任务编号:{task_no}",
"parse.no_lines": "未解析到订单明细行,请重新上传或重新解析。",
"parse.low_conf": "解析置信度较低,请重点核对料号、预订单号、数量、单价与金额。",
"parse.confirm": "请确认以上信息是否正确?",
"parse.hint": "回复「确认」进入库存检查;「重新解析」重试;「驳回」终止本任务。",
"parse.hint_btn": "",
"align.title": "【库存字段对齐确认 — 确认后再匹配】任务编号:{task_no}",
"align.match_rule": "匹配规则:{rule}{note}",
"align.table.sys_field": "系统字段",
"align.table.inv_col": "库存 Excel 列",
"align.table.notice_field": "订单字段",
"align.table.display_only": "unit_price / amount(展示用,不参与匹配)",
"align.sample.notice": "订单样例:",
"align.sample.inventory": "库存样例:",
"align.stats": "统计:{stats}",
"align.source.llm": "(LLM 推断)",
"align.source.fallback": "(后端动态兜底)",
"align.source.other": "({source})",
"align.confirm": "请确认以上字段对齐是否正确。",
"align.hint": "回复「确认」进入匹配;「更正映射」重选列;「重新上传」换库存文件;「驳回」终止本任务。",
"align.hint_btn": "",
"common.tips": "提示:",
"table.field": "字段",
"table.value": "值",
"field.po": "采购单号",
"field.sku": "料号",
"field.reservation_id": "预订单号",
"field.qty": "数量",
"field.unit_price": "单价",
"field.amount": "金额",
"field.required_date": "需求日期",
"field.ship_to": "收货方",
"field.vendor_part_number": "供应商料号",
"field.line_no": "项次",
"field.available_qty": "可用数量",
"field.aws_sku": "料号",
"match.title": "【库存匹配结果】任务编号:{task_no}",
"match.subtitle": "匹配明细(订单数量 vs 库存可用量)",
"match.col.order_qty": "订单需求",
"match.col.inv_qty": "库存可用",
"match.col.gap": "缺口",
"match.col.status": "状态",
"match.status.ok": "✅ 充足",
"match.status.partial": "⚠️ 部分满足",
"match.status.shortage": "❌ 不足",
"match.status.unknown": "未知",
"match.conclusion.fulfillable": "**结论**:库存充足,可生成发货 Excel 并上传货代系统。",
"match.conclusion.partial": "**结论**:存在库存缺口,无法发货。请补库存或返回修订后重新匹配,直至全部行满足。",
"match.conclusion.unfulfillable": "**结论**:库存均不足,无法发货。请补库存或返回修订后重新匹配。",
"match.conclusion.other": "**结论**:match_status={status}",
"match.no_lines": "无订单明细,无法比对数量。",
},
"en": {
"parse.title": "[Shipping notice parsed] Task: {task_no}",
"parse.no_lines": "No order lines found. Please re-upload or re-parse.",
"parse.low_conf": "Low confidence. Please double-check SKU, Qty, Reservation ID, Unit Price and Amount.",
"parse.confirm": "Please confirm the above information.",
"parse.hint": 'Reply "Confirm" to proceed; "Re-parse" to retry; "Reject" to stop this task.',
"parse.hint_btn": "",
"align.title": "[Inventory mapping confirmation] Task: {task_no}",
"align.match_rule": "Match rule: {rule}{note}",
"align.table.sys_field": "System field",
"align.table.inv_col": "Inventory column",
"align.table.notice_field": "Order field",
"align.table.display_only": "unit_price / amount (display only)",
"align.sample.notice": "Order samples:",
"align.sample.inventory": "Inventory samples:",
"align.stats": "Stats: {stats}",
"align.source.llm": " (LLM)",
"align.source.fallback": " (backend fallback)",
"align.source.other": " ({source})",
"align.confirm": "Please confirm the field alignment above.",
"align.hint": 'Reply "Confirm" to match; "Fix mapping" to reselect columns; "Re-upload" to change the inventory file; "Reject" to stop.',
"align.hint_btn": "",
"common.tips": "Notes:",
"table.field": "Field",
"table.value": "Value",
"field.po": "PO",
"field.sku": "SKU",
"field.reservation_id": "Reservation ID",
"field.qty": "Qty",
"field.unit_price": "Unit Price",
"field.amount": "Amount",
"field.required_date": "Required date",
"field.ship_to": "Ship to",
"field.vendor_part_number": "Vendor PN",
"field.line_no": "Line",
"field.available_qty": "Available qty",
"field.aws_sku": "AWS Sku",
"match.title": "[Inventory match result] Task: {task_no}",
"match.subtitle": "Match details (order qty vs inventory available)",
"match.col.order_qty": "Order qty",
"match.col.inv_qty": "Available",
"match.col.gap": "Gap",
"match.col.status": "Status",
"match.status.ok": "✅ OK",
"match.status.partial": "⚠️ Partial",
"match.status.shortage": "❌ Short",
"match.status.unknown": "Unknown",
"match.conclusion.fulfillable": "**Result**: Inventory is sufficient. You may generate the shipment Excel and upload.",
"match.conclusion.partial": "**Result**: Inventory gap detected. Shipment blocked until all lines are fulfilled. Re-upload inventory or revise mapping, then rematch.",
"match.conclusion.unfulfillable": "**Result**: Insufficient inventory for all lines. Shipment blocked until inventory is replenished.",
"match.conclusion.other": "**Result**: match_status={status}",
"match.no_lines": "No order lines to compare.",
},
"th": {
"parse.title": "【ยืนยันการแยกใบแจ้งส่งสินค้า】งาน: {task_no}",
"parse.no_lines": "ไม่พบบรรทัดรายการสั่งซื้อ กรุณาอัปโหลดใหม่หรือแยกใหม่อีกครั้ง",
"parse.low_conf": "ความมั่นใจต่ำ กรุณาตรวจสอบ SKU, จำนวน, Reservation ID, ราคาต่อหน่วย และจำนวนเงิน",
"parse.confirm": "กรุณายืนยันว่าข้อมูลด้านบนถูกต้องหรือไม่",
"parse.hint": 'พิมพ์ "ยืนยัน" เพื่อไปต่อ; "แยกใหม่" เพื่อลองใหม่; "ปฏิเสธ" เพื่อยุติงานนี้',
"parse.hint_btn": "",
"align.title": "【ยืนยันการจับคู่คอลัมน์สต็อก】งาน: {task_no}",
"align.match_rule": "กฎการจับคู่: {rule}{note}",
"align.table.sys_field": "ฟิลด์ระบบ",
"align.table.inv_col": "คอลัมน์สต็อก",
"align.table.notice_field": "ฟิลด์คำสั่งซื้อ",
"align.table.display_only": "unit_price / amount (แสดงผลเท่านั้น)",
"align.sample.notice": "ตัวอย่างคำสั่งซื้อ:",
"align.sample.inventory": "ตัวอย่างสต็อก:",
"align.stats": "สถิติ: {stats}",
"align.source.llm": " (LLM)",
"align.source.fallback": " (สำรองโดยระบบ)",
"align.source.other": " ({source})",
"align.confirm": "กรุณายืนยันว่าการจับคู่ฟิลด์ด้านบนถูกต้อง",
"align.hint": 'พิมพ์ "ยืนยัน" เพื่อจับคู่; "แก้ไขการจับคู่" เพื่อเลือกคอลัมน์ใหม่; "อัปโหลดใหม่" เพื่อเปลี่ยนไฟล์สต็อก; "ปฏิเสธ" เพื่อยุติ',
"align.hint_btn": "",
"common.tips": "หมายเหตุ:",
"table.field": "ฟิลด์",
"table.value": "ค่า",
"field.po": "PO",
"field.sku": "SKU",
"field.reservation_id": "Reservation ID",
"field.qty": "จำนวน",
"field.unit_price": "ราคาต่อหน่วย",
"field.amount": "จำนวนเงิน",
"field.required_date": "วันที่ต้องการ",
"field.ship_to": "ผู้รับ",
"field.vendor_part_number": "หมายเลขชิ้นส่วนผู้ขาย",
"field.line_no": "รายการ",
"field.available_qty": "จำนวนคงเหลือ",
"field.aws_sku": "AWS Sku",
"match.title": "【ผลการจับคู่สต็อก】งาน: {task_no}",
"match.subtitle": "รายละเอียดการจับคู่ (จำนวนสั่งซื้อ vs สต็อกคงเหลือ)",
"match.col.order_qty": "จำนวนสั่ง",
"match.col.inv_qty": "สต็อกคงเหลือ",
"match.col.gap": "ส่วนต่าง",
"match.col.status": "สถานะ",
"match.status.ok": "✅ เพียงพอ",
"match.status.partial": "⚠️ บางส่วน",
"match.status.shortage": "❌ ไม่พอ",
"match.status.unknown": "ไม่ทราบ",
"match.conclusion.fulfillable": "**สรุป**: สต็อกเพียงพอ สามารถสร้าง Excel และอัปโหลดได้",
"match.conclusion.partial": "**สรุป**: มีส่วนต่างสต็อก ไม่สามารถจัดส่งได้ กรุณาเติมสต็อกหรือแก้ไขแล้วจับคู่ใหม่จนกว่าทุกรายการจะครบ",
"match.conclusion.unfulfillable": "**สรุป**: สต็อกไม่เพียงพอทั้งหมด ไม่สามารถจัดส่งได้ กรุณาเติมสต็อกหรือแก้ไขแล้วจับคู่ใหม่",
"match.conclusion.other": "**สรุป**: match_status={status}",
"match.no_lines": "ไม่มีรายการสั่งซื้อสำหรับเปรียบเทียบ",
},
}
def normalize_lang(raw: Any) -> str:
"""将 lang/locale 别名规范化为 zh / en / th。"""
text = str(raw or "").strip().lower()
if not text:
return "zh"
if text in {"zh-cn", "zh_hans", "zh-hans", "cn", "zh"}:
return "zh"
if text in {"en-us", "en_us", "en"}:
return "en"
if text in {"th-th", "th_th", "th"}:
return "th"
for prefix in SUPPORTED_LANGS:
if text.startswith(prefix):
return prefix
return "zh"
def resolve_lang(payload: dict[str, Any] | None = None) -> str:
"""从 payload 或 HTTP 请求头(X-Lang / Accept-Language)解析语言,默认 zh。"""
if isinstance(payload, dict):
for key in ("lang", "locale", "language"):
if key in payload:
val = str(payload.get(key) or "").strip()
if val:
return normalize_lang(val)
if has_request_context():
hdr = str(request.headers.get("X-Lang") or "").strip()
if hdr:
return normalize_lang(hdr)
accept = str(request.headers.get("Accept-Language") or "").strip()
if accept:
return normalize_lang(accept.split(",")[0].strip())
return "zh"
def t(key: str, *, lang: str | None = None) -> str:
"""按语言键查找 i18n 文案;未命中时回退 zh 或原 key。"""
resolved = normalize_lang(lang) if lang else "zh"
return _MESSAGES.get(resolved, {}).get(key) or _MESSAGES["zh"].get(key) or key