mirror of
https://github.com/sysadminsmedia/homebox.git
synced 2025-12-21 21:33:02 +01:00
Compare commits
1 Commits
copilot/fi
...
tonya/lang
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
646a80f494 |
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 {
|
if cfg.Debug.Enabled {
|
||||||
runner.AddFunc("debug", func(ctx context.Context) error {
|
runner.AddFunc("debug", func(ctx context.Context) error {
|
||||||
debugserver := http.Server{
|
debugserver := http.Server{
|
||||||
|
|||||||
@@ -77,5 +77,22 @@ 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
|
return nil
|
||||||
}
|
}
|
||||||
@@ -2,10 +2,10 @@ import type { CompileError, MessageContext } from "vue-i18n";
|
|||||||
import { createI18n } from "vue-i18n";
|
import { createI18n } from "vue-i18n";
|
||||||
import { IntlMessageFormat } from "intl-messageformat";
|
import { IntlMessageFormat } from "intl-messageformat";
|
||||||
|
|
||||||
export default defineNuxtPlugin(({ vueApp }) => {
|
export default defineNuxtPlugin(async ({ vueApp }) => {
|
||||||
function checkDefaultLanguage() {
|
async function checkDefaultLanguage() {
|
||||||
let matched = null;
|
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()));
|
const matching = navigator.languages.filter(lang => languages.some(l => l.toLowerCase() === lang.toLowerCase()));
|
||||||
if (matching.length > 0) {
|
if (matching.length > 0) {
|
||||||
matched = matching[0];
|
matched = matching[0];
|
||||||
@@ -25,20 +25,24 @@ export default defineNuxtPlugin(({ vueApp }) => {
|
|||||||
fallbackLocale: "en",
|
fallbackLocale: "en",
|
||||||
globalInjection: true,
|
globalInjection: true,
|
||||||
legacy: false,
|
legacy: false,
|
||||||
locale: preferences.value.language || checkDefaultLanguage() || "en",
|
locale: preferences.value.language || await checkDefaultLanguage() || "en",
|
||||||
messageCompiler,
|
messageCompiler,
|
||||||
messages: messages(),
|
messages: await messages(),
|
||||||
});
|
});
|
||||||
vueApp.use(i18n);
|
vueApp.use(i18n);
|
||||||
});
|
});
|
||||||
|
|
||||||
export const messages = () => {
|
export const messages = async () => {
|
||||||
const messages: Record<string, any> = {};
|
const messages: Record<string, any> = {};
|
||||||
const modules = import.meta.glob("~//locales/**.json", { eager: true });
|
// const modules = import.meta.glob("~//locales/**.json", { eager: true });
|
||||||
for (const path in modules) {
|
// for (const path in modules) {
|
||||||
const key = path.slice(9, -5);
|
// const key = path.slice(9, -5);
|
||||||
messages[key] = modules[path];
|
// 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;
|
return messages;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user