refactor: support i18n
This commit is contained in:
@@ -3,11 +3,11 @@ export type CSAgentConfig = {
|
||||
baseUrl?: string
|
||||
apiBaseUrl?: string
|
||||
widgetBaseUrl?: string
|
||||
/** 外部访客稳定标识;未传时使用浏览器本地访客 ID */
|
||||
/** Stable external visitor ID. Uses the browser-local visitor ID when omitted. */
|
||||
externalId?: string
|
||||
/** 访客展示名,仅用于首次换取客服会话 token */
|
||||
/** Visitor display name, only used when first exchanging for a chat token. */
|
||||
externalName?: string
|
||||
/** 打开客服前按需获取业务系统签发的前台用户 JWT */
|
||||
/** Gets the user JWT issued by the host system before opening support. */
|
||||
getUserToken?: () => string | Promise<string>
|
||||
title?: string
|
||||
subtitle?: string
|
||||
@@ -17,7 +17,7 @@ export type CSAgentConfig = {
|
||||
}
|
||||
|
||||
export type KefuChatRuntimeConfig = Omit<CSAgentConfig, "getUserToken"> & {
|
||||
/** 仅用于 /kefu/chat 运行时换取客服会话 token,不属于 CSAgentConfig 接入参数 */
|
||||
/** Used only by /kefu/chat to exchange for a chat token; not part of CSAgentConfig. */
|
||||
userToken?: string
|
||||
}
|
||||
|
||||
|
||||
@@ -54,7 +54,7 @@ async function loadSdk(config) {
|
||||
json: async () => ({
|
||||
success: true,
|
||||
data: {
|
||||
title: "在线客服",
|
||||
title: "\u5728\u7ebf\u5ba2\u670d",
|
||||
themeColor: "#2563eb",
|
||||
},
|
||||
}),
|
||||
|
||||
@@ -38,6 +38,24 @@ type WidgetConfigResponse = {
|
||||
>>
|
||||
}
|
||||
|
||||
function getWidgetLocale() {
|
||||
try {
|
||||
const stored = window.localStorage?.getItem("cs_ai_agent_locale")
|
||||
const language = stored || document.documentElement.lang || window.navigator?.language || ""
|
||||
return language.toLowerCase().startsWith("en") ? "en-US" : "zh-CN"
|
||||
} catch {
|
||||
return "zh-CN"
|
||||
}
|
||||
}
|
||||
|
||||
function getDefaultWidgetTitle() {
|
||||
return getWidgetLocale() === "en-US" ? "Support" : "\u5728\u7ebf\u5ba2\u670d"
|
||||
}
|
||||
|
||||
function getLauncherText() {
|
||||
return getWidgetLocale() === "en-US" ? "Support" : "\u5ba2\u670d"
|
||||
}
|
||||
|
||||
type FrameMessage =
|
||||
| { type: "cs-agent:init"; payload: KefuChatRuntimeConfig }
|
||||
| { type: "cs-agent:open" }
|
||||
@@ -351,7 +369,7 @@ type FrameMessage =
|
||||
|
||||
state.frame = document.createElement("iframe")
|
||||
state.frame.dataset.csAgentWidget = "frame"
|
||||
state.frame.title = state.config.title || "在线客服"
|
||||
state.frame.title = state.config.title || getDefaultWidgetTitle()
|
||||
state.frame.src = state.frameUrl.toString()
|
||||
applyFrameLayout()
|
||||
state.frame.style.display = "block"
|
||||
@@ -417,7 +435,7 @@ type FrameMessage =
|
||||
const text = document.createElement("span")
|
||||
button.type = "button"
|
||||
button.dataset.csAgentWidget = "launcher"
|
||||
button.setAttribute("aria-label", config.title || "在线客服")
|
||||
button.setAttribute("aria-label", config.title || getDefaultWidgetTitle())
|
||||
icon.setAttribute("viewBox", "0 0 24 24")
|
||||
icon.setAttribute("fill", "none")
|
||||
icon.setAttribute("stroke", "currentColor")
|
||||
@@ -433,7 +451,7 @@ type FrameMessage =
|
||||
path.setAttribute("d", pathData)
|
||||
icon.appendChild(path)
|
||||
})
|
||||
text.textContent = "客服"
|
||||
text.textContent = getLauncherText()
|
||||
text.style.display = "block"
|
||||
button.style.position = "fixed"
|
||||
button.style.bottom = "24px"
|
||||
|
||||
Reference in New Issue
Block a user