Skip to content

armgit / papra

Public Imported from GitHub
0

The minimalistic document archiving platform.

2.5 KB
Raw Blame
import type { Accessor, ParentComponent, Setter } from 'solid-js';
import type { TranslationsDictionary } from './locales.types';
import { createContext, createEffect, createResource, Show, useContext } from 'solid-js';
import { createPersistedSignal } from '@/modules/shared/signals/persistence/persistence.signals';
import { translations as defaultTranslations } from '../../locales/en.dictionary';
import { locales } from './i18n.constants';
import {
  createDateFormatter,
  createFragmentTranslator,
  createRelativeTimeFormatter,
  createTranslator,
  findMatchingLocale,
} from './i18n.models';

export type Locale = (typeof locales)[number]['key'];

const I18nContext = createContext<{
  t: ReturnType<typeof createTranslator<TranslationsDictionary>>;
  te: ReturnType<typeof createFragmentTranslator<TranslationsDictionary>>;
  getLocale: Accessor<Locale>;
  setLocale: Setter<Locale>;
  locales: typeof locales;
  formatDate: ReturnType<typeof createDateFormatter>;
  formatRelativeTime: ReturnType<typeof createRelativeTimeFormatter>;
}>();

export function useI18n() {
  const context = useContext(I18nContext);

  if (!context) {
    throw new Error('I18n context not found, useI18n must be used within I18nProvider');
  }

  return context;
}

async function fetchDictionary(locale: Locale): Promise<TranslationsDictionary> {
  const { translations } = await import(`../../locales/${locale}.dictionary.ts`);

  return {
    ...defaultTranslations,
    ...translations,
  };
}

export const I18nProvider: ParentComponent = (props) => {
  const browserLocale = findMatchingLocale({
    preferredLocales: navigator.languages.map((x) => new Intl.Locale(x)),
    supportedLocales: locales.map((x) => new Intl.Locale(x.key)),
  });
  const [getLocale, setLocale] = createPersistedSignal<Locale>(browserLocale, {
    key: 'papra_locale',
    storage: localStorage,
  });

  const [dict] = createResource(getLocale, fetchDictionary);

  createEffect(() => {
    document.documentElement.lang = getLocale();
  });

  return (
    <Show when={dict.latest}>
      {(getDictionary) => (
        <I18nContext.Provider
          value={{
            t: createTranslator({ getDictionary }),
            te: createFragmentTranslator({ getDictionary }),
            getLocale,
            setLocale,
            locales,
            formatDate: createDateFormatter({ getLocale }),
            formatRelativeTime: createRelativeTimeFormatter({ getLocale }),
          }}
        >
          {props.children}
        </I18nContext.Provider>
      )}
    </Show>
  );
};