sysadminsmedia / homebox

A continuation of HomeBox the inventory and organization system built for the Home User
https://homebox.software
GNU Affero General Public License v3.0
1.08k stars 60 forks source link

Opt in auto updates for translations #310

Open tonyaellie opened 6 days ago

tonyaellie commented 6 days ago

What is the problem you are trying to solve with this feature?

Currently to update translations a full release has to be made, this means that it takes a relatively long time to get new translation changes.

What is the solution you are proposing?

An option to have homebox automatically check for updates to the translations and download them.

What alternatives have you considered?

Wait for releases.

Additional context

No response

Contributions

tonyaellie commented 5 days ago

Dynamically fetch translation files in messages

export default defineNuxtPlugin(async ({ vueApp }) => {
  async function checkDefaultLanguage() {
    let matched = null;
    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];
    }
    if (!matched) {
      languages.forEach(lang => {
        const languagePartials = navigator.language.split("-")[0];
        if (lang.toLowerCase() === languagePartials) {
          matched = lang;
        }
      });
    }
    return matched;
  }
  const preferences = useViewPreferences();
  const i18n = createI18n({
    fallbackLocale: "en",
    globalInjection: true,
    legacy: false,
    locale: preferences.value.language || await checkDefaultLanguage() || "en",
    messageCompiler,
    messages: await messages(),
  });
  vueApp.use(i18n);
});

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];
  // }
  console.log('Fetching translations...');
  const en = await (await fetch('http://localhost:3001/en.json')).json();
  console.log('Fetched translations.');
  messages['en'] = en;
  return messages;
};