@samitouri / QOSami-HFS / commits / 25fa84c5

dev: better API for i18n (fallback key is now last)

Massimo Melina committed Aug 15, 2023 at 09:51 UTC 25fa84c579208efa01bcf42fc8e0a23a67f271dd
3 files changed +8 -7
frontend/src/i18n.ts
+6 -5
@@ -20,12 +20,13 @@ export function I18Nprovider({ embedded='en', ...props }) {
20 return h(Fragment, props)
21 }
22
23 -export function t(keyOrTpl: string | string[] | TemplateStringsArray, params?: any, def?: string) {
23 +// If one of the keys is an "id", that should be the first. If one of the keys should work as a fallback, that should be the last. Use 'fallback' parameter if you don't want the fallback to work as a key.
24 +export function t(keyOrTpl: string | string[] | TemplateStringsArray, params?: any, fallback?: string) {
25 // memoize?
25 - const keys = isTemplateStringsArray(keyOrTpl) ? [(def ??= keyOrTpl[0] as string)]
26 + const keys = isTemplateStringsArray(keyOrTpl) ? [(fallback ??= keyOrTpl[0] as string)]
27 : Array.isArray(keyOrTpl) ? keyOrTpl : [keyOrTpl]
27 - if (typeof params === 'string' && !def) {
28 - def = params
28 + if (typeof params === 'string' && !fallback) {
29 + fallback = params
30 params = null
31 }
32 let found
@@ -40,7 +41,7 @@ export function t(keyOrTpl: string | string[] | TemplateStringsArray, params?: a
41 }
42 }
43 if (!found) {
43 - found = def || keys[0]
44 + found = fallback || keys[keys.length - 1]
45 selectedLang = embedded
46 }
47 return Array.from(tokenizer(found)).map(([s,inside]) => {
frontend/src/options.ts
+1 -1
@@ -68,7 +68,7 @@ export function showOptions (){
68 ),
69
70 h(Select, {
71 - options: ['', 'light', 'dark'].map(s => ({ label: t(["Theme:", "theme:"]) + ' ' + t(s || "auto"), value: s })),
71 + options: ['', 'light', 'dark'].map(s => ({ label: t(["theme:", "Theme:", ]) + ' ' + t(s || "auto"), value: s })),
72 value: snap.theme,
73 onChange(v) {
74 state.theme = v
plugins/download-counter/public/main.js
+1 -1
@@ -1,7 +1,7 @@
1 { // this wrapper avoids name clashing of outer variables and functions
2 const config = HFS.getPluginConfig()
3
4 - const label = HFS.t(["Download counter", "download counter"])
4 + const label = HFS.t(["download counter", "Download counter"])
5 const inMenu = config.where === 'menu'
6 HFS.onEvent('additionalEntryDetails', ({ entry: { hits } }) =>
7 hits && !inMenu && `<span class="download-counter" title="${label}">${hits}</span>`)