plugins: i18n support

Massimo Melina committed Dec 6, 2023 at 22:32 UTC ccd7374716cc6ea9ae1180a8627b46f77075a214
3 files changed +14 -8
dev-plugins.md
+2
@@ -363,6 +363,8 @@ Where `h` is just `import { createElement as h } from 'react'`.
363
364 ## API version history
365
366 +- 8.6 (v0.51.0)
367 + - plugin's own hfs-lang files
368 - 8.5 (v0.49.0)
369 - new event: entry
370 - exports.onDirEntry: entry.icon
src/lang.ts
+11 -2
@@ -1,9 +1,12 @@
1 import Koa from 'koa'
2 -import { hasProp, tryJson, wantArray } from './misc'
2 +import { hasProp, onlyTruthy, tryJson, wantArray } from './misc'
3 import { readFile } from 'fs/promises'
4 import { defineConfig } from './config'
5 import { watchLoad } from './watchLoad'
6 import EMBEDDED_TRANSLATIONS from './langs/embedded'
7 +import { mapPlugins } from './plugins'
8 +import { join } from 'path'
9 +import _ from 'lodash'
10
11 const PREFIX = 'hfs-lang-'
12 const SUFFIX = '.json'
@@ -28,7 +31,8 @@ export async function getLangData(ctx: Koa.Context) {
31 while (i < langs.length) {
32 let k = langs[i] || '' // shut up ts
33 if (!k || k === EMBEDDED_LANGUAGE) break
31 - try { ret[k!] = JSON.parse(await readFile(`hfs-lang-${k}.json`, 'utf8')) }
34 + const fn = code2file(k)
35 + try { ret[k] = JSON.parse(await readFile(fn, 'utf8')) }
36 catch {
37 if (hasProp(EMBEDDED_TRANSLATIONS, k))
38 ret[k] = EMBEDDED_TRANSLATIONS[k]
@@ -41,6 +45,11 @@ export async function getLangData(ctx: Koa.Context) {
45 }
46 }
47 }
48 + const fromPlugins = onlyTruthy(await Promise.all(mapPlugins(async pl =>
49 + tryJson(await readFile(join(pl.folder, fn), 'utf8').catch(() => ''))?.translate )))
50 + if (fromPlugins.length)
51 + _.defaults((ret[k] ||= {}).translate ||= {}, ...fromPlugins) // be sure we have an entry for k
52 +
53 i++
54 }
55 return ret
src/langs/hfs-lang-it.json
+1 -6
@@ -140,11 +140,6 @@
140 "Cancel clipboard": "Annulla appunti",
141 "to_clipboard_source": "Torna all'origine'",
142 "Paste": "Incolla",
143 - "clipboard_list": "Elementi negli appunti",
144 -
145 - "_PLUGINS SECTION": "",
146 - "_PLUGIN thumbnails": "",
147 - "Enable tiles mode": "Modalità griglia",
148 - "thumbnails_switchBack": "Puoi tornare alla lista com'era prima entrando in Opzioni"
143 + "clipboard_list": "Elementi negli appunti"
144 }
145 }