1
0
mirror of https://github.com/hrfee/jfa-go.git synced 2024-12-22 09:00:10 +00:00

use embed.fs wrapper for langFS so lang/ is not needed in paths

[files]lang_files is now the path to the lang directory, not path to a
directory containing it.
This commit is contained in:
Harvey Tindall 2021-02-02 15:19:43 +00:00
parent 98a9e20cc0
commit e6775cd2d1
Signed by: hrfee
GPG Key ID: BBC65952848FB1A2
3 changed files with 23 additions and 11 deletions

View File

@ -867,7 +867,7 @@
"requires_restart": true, "requires_restart": true,
"type": "text", "type": "text",
"value": "", "value": "",
"description": "The path to a directory CONTAINING a 'lang/' directory, which follow the same form as the internal one. See GitHub for more info." "description": "The path to a directory which following the same form as the internal 'lang/' directory. See GitHub for more info."
} }
} }
} }

View File

@ -14,6 +14,7 @@ with open("embed.go", "w") as f:
f.write("""package main f.write("""package main
import ( import (
"embed" "embed"
"io/fs"
"log" "log"
) )
@ -21,9 +22,20 @@ import (
var localFS embed.FS var localFS embed.FS
//go:embed lang/common lang/admin lang/email lang/form lang/setup //go:embed lang/common lang/admin lang/email lang/form lang/setup
var langFS embed.FS var lFS embed.FS
var langFS LangFS
type LangFS struct {
fs embed.FS
}
func (l LangFS) Open(name string) (fs.File, error) { return l.fs.Open("lang/" + name) }
func (l LangFS) ReadDir(name string) ([]fs.DirEntry, error) { return l.fs.ReadDir("lang/" + name) }
func (l LangFS) ReadFile(name string) ([]byte, error) { return l.fs.ReadFile("lang/" + name) }
func loadLocalFS() { func loadLocalFS() {
langFS = LangFS{lFS}
log.Println("Using internal storage") log.Println("Using internal storage")
}""") }""")
elif EMBED in falses: elif EMBED in falses:
@ -42,5 +54,5 @@ func loadLocalFS() {
log.Println("Using external storage") log.Println("Using external storage")
executable, _ := os.Executable() executable, _ := os.Executable()
localFS = os.DirFS(filepath.Dir(executable)) localFS = os.DirFS(filepath.Dir(executable))
langFS = os.DirFS(filepath.Join(filepath.Dir(executable), "data")) langFS = os.DirFS(filepath.Join(filepath.Dir(executable), "data", "lang"))
}""") }""")

10
main.go
View File

@ -339,10 +339,10 @@ func start(asDaemon, firstCall bool) {
}() }()
} }
app.storage.lang.CommonPath = "lang/common" app.storage.lang.CommonPath = "common"
app.storage.lang.FormPath = "lang/form" app.storage.lang.FormPath = "form"
app.storage.lang.AdminPath = "lang/admin" app.storage.lang.AdminPath = "admin"
app.storage.lang.EmailPath = "lang/email" app.storage.lang.EmailPath = "email"
externalLang := app.config.Section("files").Key("lang_files").MustString("") externalLang := app.config.Section("files").Key("lang_files").MustString("")
var err error var err error
if externalLang == "" { if externalLang == "" {
@ -577,7 +577,7 @@ func start(asDaemon, firstCall bool) {
} else { } else {
debugMode = false debugMode = false
address = "0.0.0.0:8056" address = "0.0.0.0:8056"
app.storage.lang.SetupPath = "lang/setup" app.storage.lang.SetupPath = "setup"
err := app.storage.loadLangSetup(langFS) err := app.storage.loadLangSetup(langFS)
if err != nil { if err != nil {
app.info.Fatalf("Failed to load language files: %+v\n", err) app.info.Fatalf("Failed to load language files: %+v\n", err)