feat: begin work on language updates

This commit is contained in:
tonya
2025-01-04 13:11:23 +00:00
parent 1268fd90ba
commit 646a80f494
4 changed files with 96 additions and 11 deletions

View 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)
}

View File

@@ -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{

View File

@@ -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
}

View File

@@ -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;
};