mirror of
https://github.com/sysadminsmedia/homebox.git
synced 2025-12-21 13:23:14 +01:00
feat: begin work on language updates
This commit is contained in:
51
backend/app/api/handlers/v1/v1_ctrl_locales.go
Normal file
51
backend/app/api/handlers/v1/v1_ctrl_locales.go
Normal file
@@ -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)
|
||||
}
|
||||
@@ -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{
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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<string, any> = {};
|
||||
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;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user