@uniweb/core 1.6.11 → 1.6.12

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/website.js +28 -10
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uniweb/core",
3
- "version": "1.6.11",
3
+ "version": "1.6.12",
4
4
  "description": "Core classes for the Uniweb platform - Uniweb, Website, Page, Block",
5
5
  "type": "module",
6
6
  "exports": {
package/src/website.js CHANGED
@@ -42,6 +42,24 @@ const SERVICE_RESOLVERS = {
42
42
  search: (website) => website.isSearchEnabled(),
43
43
  }
44
44
 
45
+ /**
46
+ * A value's entry for a language: by its tag, then each shorter tag, ignoring case (RFC 4647 §3.4
47
+ * lookup). `zh-Hant-TW` tries `zh-Hant-TW`, `zh-Hant`, `zh`. Returns the first non-empty entry.
48
+ *
49
+ * @param {Object} obj - entries keyed by language tag
50
+ * @param {string} lang - a language tag
51
+ * @returns {*} the entry, or undefined
52
+ */
53
+ function entryForLanguage(obj, lang) {
54
+ if (!obj || !lang) return undefined
55
+ const keys = Object.keys(obj)
56
+ for (let tag = String(lang); tag; tag = tag.slice(0, Math.max(tag.lastIndexOf('-'), 0))) {
57
+ const key = Object.hasOwn(obj, tag) ? tag : keys.find((k) => k.toLowerCase() === tag.toLowerCase())
58
+ if (key !== undefined && obj[key]) return obj[key]
59
+ }
60
+ return undefined
61
+ }
62
+
45
63
  /**
46
64
  * Website — orchestration root for a single site instance.
47
65
  *
@@ -1151,6 +1169,12 @@ export default class Website {
1151
1169
 
1152
1170
  /**
1153
1171
  * Localize a value
1172
+ *
1173
+ * A language finds its entry by its own tag, then by each shorter one (`zh-Hant-TW`, `zh-Hant`,
1174
+ * `zh`), without regard to case: the lookup of RFC 4647 §3.4. So a value keyed `zh` answers a site
1175
+ * whose languages are `zh-CN` and `zh-TW`, where an exact key alone left it empty (measured
1176
+ * 2026-11-10 on a site's footer).
1177
+ *
1154
1178
  * @param {any} val - Value to localize (object with lang keys, or string)
1155
1179
  * @param {string} defaultVal - Default value if not found
1156
1180
  * @param {string} givenLang - Override language
@@ -1160,23 +1184,17 @@ export default class Website {
1160
1184
  localize(val, defaultVal = '', givenLang = '', fallbackDefaultLangVal = false) {
1161
1185
  const lang = givenLang || this.activeLang
1162
1186
  const defaultLang = this.langs[0]?.value || 'en'
1187
+ const pick = (obj) =>
1188
+ entryForLanguage(obj, lang) || (fallbackDefaultLangVal ? entryForLanguage(obj, defaultLang) : undefined) || defaultVal
1163
1189
 
1164
- if (typeof val === 'object' && !Array.isArray(val)) {
1165
- return fallbackDefaultLangVal
1166
- ? val?.[lang] || val?.[defaultLang] || defaultVal
1167
- : val?.[lang] || defaultVal
1168
- }
1190
+ if (typeof val === 'object' && !Array.isArray(val)) return pick(val)
1169
1191
 
1170
1192
  if (typeof val === 'string') {
1171
1193
  if (!val.startsWith('{') && !val.startsWith('"')) return val
1172
1194
 
1173
1195
  try {
1174
1196
  const obj = JSON.parse(val)
1175
- if (typeof obj === 'object') {
1176
- return fallbackDefaultLangVal
1177
- ? obj?.[lang] || obj?.[defaultLang] || defaultVal
1178
- : obj?.[lang] || defaultVal
1179
- }
1197
+ if (typeof obj === 'object') return pick(obj)
1180
1198
  return obj
1181
1199
  } catch {
1182
1200
  return val