dsh-ai-courses 0.1.3 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/client.js +44 -1
- package/package.json +1 -1
package/lib/client.js
CHANGED
|
@@ -680,6 +680,27 @@ const AIC_COURSES_CORE = (function () {
|
|
|
680
680
|
return DEFAULT_LOCALE
|
|
681
681
|
}
|
|
682
682
|
|
|
683
|
+
/**
|
|
684
|
+
* 动态 `t(key, vars)`:**每次调用现读**宿主语言。
|
|
685
|
+
*
|
|
686
|
+
* 为什么不能在 apply 时 `createT(detectLocale(...))` 一次定死:
|
|
687
|
+
* DSH 的 locale 插件在启动后才把 `<html lang>` 从服务端默认的 `en` 改成用户设置
|
|
688
|
+
* (profile patch 里 `locale.preference: zh` 的情况)。apply 时读到的还是 `en`,
|
|
689
|
+
* 于是整个面板一直显示英文。这里按 locale 缓存翻译器,语言变了下一次渲染就切过来。
|
|
690
|
+
*/
|
|
691
|
+
function createLiveT (doc, nav) {
|
|
692
|
+
const cache = new Map()
|
|
693
|
+
return function t (key, vars) {
|
|
694
|
+
const locale = detectLocale(doc, nav)
|
|
695
|
+
let tr = cache.get(locale)
|
|
696
|
+
if (!tr) {
|
|
697
|
+
tr = createT(locale)
|
|
698
|
+
cache.set(locale, tr)
|
|
699
|
+
}
|
|
700
|
+
return tr(key, vars)
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
|
|
683
704
|
/* --------------------------------------------------- 编号(绝不解析 id) */
|
|
684
705
|
/** 章节的人类编号:由 `order` 派生(05 §4 / 06 §4.2)。 */
|
|
685
706
|
function chapterNo (ch) {
|
|
@@ -1172,6 +1193,7 @@ const AIC_COURSES_CORE = (function () {
|
|
|
1172
1193
|
LOCALES,
|
|
1173
1194
|
DEFAULT_LOCALE,
|
|
1174
1195
|
createT,
|
|
1196
|
+
createLiveT,
|
|
1175
1197
|
detectLocale,
|
|
1176
1198
|
genSpec,
|
|
1177
1199
|
learnSpec,
|
|
@@ -4458,9 +4480,28 @@ function PageState (props) {
|
|
|
4458
4480
|
)
|
|
4459
4481
|
}
|
|
4460
4482
|
|
|
4483
|
+
/**
|
|
4484
|
+
* `<html lang>` 变化时强制重渲染。
|
|
4485
|
+
*
|
|
4486
|
+
* DSH 的 locale 插件在启动后才把服务端默认的 `en` 改成用户设置;`t` 本身是
|
|
4487
|
+
* `createLiveT`(每次调用现读),但组件需要一次 re-render 才会重新取词。
|
|
4488
|
+
*/
|
|
4489
|
+
function useLocaleRefresh () {
|
|
4490
|
+
const [, setTick] = React.useState(0)
|
|
4491
|
+
React.useEffect(() => {
|
|
4492
|
+
if (typeof document === 'undefined' || typeof MutationObserver !== 'function') return undefined
|
|
4493
|
+
const el = document.documentElement
|
|
4494
|
+
if (!el || typeof el.getAttribute !== 'function') return undefined
|
|
4495
|
+
const obs = new MutationObserver(() => setTick((n) => n + 1))
|
|
4496
|
+
obs.observe(el, { attributes: true, attributeFilter: ['lang'] })
|
|
4497
|
+
return () => obs.disconnect()
|
|
4498
|
+
}, [])
|
|
4499
|
+
}
|
|
4500
|
+
|
|
4461
4501
|
function AicCoursesApp (props) {
|
|
4462
4502
|
const t = props.t
|
|
4463
4503
|
const core = AIC_COURSES_CORE
|
|
4504
|
+
useLocaleRefresh()
|
|
4464
4505
|
const [view, setView] = React.useState('library')
|
|
4465
4506
|
const [courseId, setCourseId] = React.useState(null)
|
|
4466
4507
|
const [kpId, setKpId] = React.useState(null)
|
|
@@ -4697,7 +4738,9 @@ function aicCoursesApply (ctx) {
|
|
|
4697
4738
|
|
|
4698
4739
|
const doc = typeof document === 'undefined' ? null : document
|
|
4699
4740
|
const nav = typeof navigator === 'undefined' ? null : navigator
|
|
4700
|
-
|
|
4741
|
+
/* 动态语言:`<html lang>` 由 DSH locale 插件在启动后改成用户设置,
|
|
4742
|
+
不能在这里一次性 detectLocale 后锁死(否则中文设置下一直显示英文)。 */
|
|
4743
|
+
const t = AIC_COURSES_CORE.createLiveT(doc, nav)
|
|
4701
4744
|
|
|
4702
4745
|
/* 样式:构建期内联(宿主不会自动加载包内 lib/styles.css —— 见 doc/03 §6 的定案),
|
|
4703
4746
|
挂一个 <style> 并在停用/卸载时移除;不用 innerHTML。 */
|