diff --git a/backend/app/api/handlers/v1/v1_ctrl_locales.go b/backend/app/api/handlers/v1/v1_ctrl_locales.go new file mode 100644 index 00000000..e93e841e --- /dev/null +++ b/backend/app/api/handlers/v1/v1_ctrl_locales.go @@ -0,0 +1,51 @@ +package v1 + +import ( + "encoding/json" + "net/http" + + "github.com/google/uuid" + "github.com/hay-kot/httpkit/errchain" + "github.com/sysadminsmedia/homebox/backend/internal/core/services" + "github.com/sysadminsmedia/homebox/backend/internal/data/repo" + "github.com/sysadminsmedia/homebox/backend/internal/web/adapters" +) + +type Locales struct { + Locales []string `json:"locales"` +} + +// HandleLocalesGetAll godoc +// +// @Summary Get All Locales +// @Tags Locales +// @Produce json +// @Success 200 {object} []Locales +// @Router /v1/locales [GET] +// @Security Bearer +func (ctrl *V1Controller) HandleLocalesGetAll() errchain.HandlerFunc { + fn := func(r *http.Request) ([]Locales, error) { + // TODO: get a list of locales from files + return []Locales{}, nil + } + + return adapters.Command(fn, http.StatusOK) +} + +// HandleLocalesGet godoc +// +// @Summary Get Locale +// @Tags Locales +// @Produce json +// @Param id path string true "Locale ID" +// @Success 200 {object} interface{} +// @Router /v1/locales/{id} [GET] +// @Security Bearer +func (ctrl *V1Controller) HandleLocalesGet() errchain.HandlerFunc { + fn := func(r *http.Request, ID uuid.UUID) (interface{}, error) { + // TODO: get the current locale + return interface{}, nil + } + + return adapters.CommandID("id", fn, http.StatusOK) +} \ No newline at end of file diff --git a/backend/app/api/main.go b/backend/app/api/main.go index 6247fe6a..c7571ed4 100644 --- a/backend/app/api/main.go +++ b/backend/app/api/main.go @@ -254,6 +254,19 @@ func run(cfg *config.Config) error { } })) + // TODO: read from env var0 GOOS=linux go build + if true { + runner.AddPlugin(NewTask("locale-update", time.Duration(24)*time.Hour, func(ctx context.Context) { + log.Debug().Msg("running locale update") + err := app.services.BackgroundService.UpdateLocales(ctx) + if err != nil { + log.Error(). + Err(err). + Msg("failed to update locales") + } + })) + } + if cfg.Debug.Enabled { runner.AddFunc("debug", func(ctx context.Context) error { debugserver := http.Server{ diff --git a/backend/internal/core/services/service_background.go b/backend/internal/core/services/service_background.go index 1af8e1ba..ea2b9b12 100644 --- a/backend/internal/core/services/service_background.go +++ b/backend/internal/core/services/service_background.go @@ -79,3 +79,20 @@ func (svc *BackgroundService) SendNotifiersToday(ctx context.Context) error { return nil } + +func (svc *BackgroundService) UpdateLocales(ctx context.Context) error { + log.Debug().Msg("updating locales") + // fetch list of locales from github + // is it worth checking if any changes have been made? + // download locales overwriting files in static/public/locales + + // curl -H "Accept: application/vnd.github.v3+json" \ + // -H "If-Modified-Since: Thu, 31 Oct 2024 09:59:02 GMT" \ + // -o /dev/null -s -w "%{http_code}\n" \ + // https://api.github.com/repos/sysadminsmedia/homebox/contents/frontend/locales + // keep track of last modified date + + + + return nil +} \ No newline at end of file diff --git a/frontend/plugins/i18n.ts b/frontend/plugins/i18n.ts index f6994dcb..412914c2 100644 --- a/frontend/plugins/i18n.ts +++ b/frontend/plugins/i18n.ts @@ -2,10 +2,10 @@ import type { CompileError, MessageContext } from "vue-i18n"; import { createI18n } from "vue-i18n"; import { IntlMessageFormat } from "intl-messageformat"; -export default defineNuxtPlugin(({ vueApp }) => { - function checkDefaultLanguage() { +export default defineNuxtPlugin(async ({ vueApp }) => { + async function checkDefaultLanguage() { let matched = null; - const languages = Object.getOwnPropertyNames(messages()); + const languages = Object.getOwnPropertyNames(await messages()); const matching = navigator.languages.filter(lang => languages.some(l => l.toLowerCase() === lang.toLowerCase())); if (matching.length > 0) { matched = matching[0]; @@ -25,20 +25,24 @@ export default defineNuxtPlugin(({ vueApp }) => { fallbackLocale: "en", globalInjection: true, legacy: false, - locale: preferences.value.language || checkDefaultLanguage() || "en", + locale: preferences.value.language || await checkDefaultLanguage() || "en", messageCompiler, - messages: messages(), + messages: await messages(), }); vueApp.use(i18n); }); -export const messages = () => { +export const messages = async () => { const messages: Record = {}; - const modules = import.meta.glob("~//locales/**.json", { eager: true }); - for (const path in modules) { - const key = path.slice(9, -5); - messages[key] = modules[path]; - } + // const modules = import.meta.glob("~//locales/**.json", { eager: true }); + // for (const path in modules) { + // const key = path.slice(9, -5); + // messages[key] = modules[path]; + // } + console.log('Fetching translations...'); + const en = await (await fetch('https://raw.githubusercontent.com/sysadminsmedia/homebox/refs/heads/main/frontend/locales/en.json')).json(); + console.log('Fetched translations.'); + messages['en'] = en; return messages; };