From 9b2c63d92ff77ebce0f90a7be05437504422bf45 Mon Sep 17 00:00:00 2001 From: ivarlovlie Date: Sat, 11 Feb 2023 23:37:12 +0100 Subject: feat: Render localized content from sanity --- src/i18n/en/index.ts | 13 +++++++++ src/i18n/formatters.ts | 11 ++++++++ src/i18n/i18n-svelte.ts | 12 +++++++++ src/i18n/i18n-types.ts | 66 +++++++++++++++++++++++++++++++++++++++++++++ src/i18n/i18n-util.async.ts | 27 +++++++++++++++++++ src/i18n/i18n-util.sync.ts | 26 ++++++++++++++++++ src/i18n/i18n-util.ts | 38 ++++++++++++++++++++++++++ src/i18n/nb/index.ts | 13 +++++++++ 8 files changed, 206 insertions(+) create mode 100644 src/i18n/en/index.ts create mode 100644 src/i18n/formatters.ts create mode 100644 src/i18n/i18n-svelte.ts create mode 100644 src/i18n/i18n-types.ts create mode 100644 src/i18n/i18n-util.async.ts create mode 100644 src/i18n/i18n-util.sync.ts create mode 100644 src/i18n/i18n-util.ts create mode 100644 src/i18n/nb/index.ts (limited to 'src/i18n') diff --git a/src/i18n/en/index.ts b/src/i18n/en/index.ts new file mode 100644 index 0000000..796f4a5 --- /dev/null +++ b/src/i18n/en/index.ts @@ -0,0 +1,13 @@ +import type { BaseTranslation } from '../i18n-types' + +const en = { + contact: { + title: "Contact us", + addressTitle: "Address", + emailTitle: "Email", + phoneTitle: "Phone" + }, + homeTitle: "Home", +} satisfies BaseTranslation + +export default en diff --git a/src/i18n/formatters.ts b/src/i18n/formatters.ts new file mode 100644 index 0000000..78734f9 --- /dev/null +++ b/src/i18n/formatters.ts @@ -0,0 +1,11 @@ +import type { FormattersInitializer } from 'typesafe-i18n' +import type { Locales, Formatters } from './i18n-types' + +export const initFormatters: FormattersInitializer = (locale: Locales) => { + + const formatters: Formatters = { + // add your formatter functions here + } + + return formatters +} diff --git a/src/i18n/i18n-svelte.ts b/src/i18n/i18n-svelte.ts new file mode 100644 index 0000000..6cdffb3 --- /dev/null +++ b/src/i18n/i18n-svelte.ts @@ -0,0 +1,12 @@ +// This file was auto-generated by 'typesafe-i18n'. Any manual changes will be overwritten. +/* eslint-disable */ + +import { initI18nSvelte } from 'typesafe-i18n/svelte' +import type { Formatters, Locales, TranslationFunctions, Translations } from './i18n-types' +import { loadedFormatters, loadedLocales } from './i18n-util' + +const { locale, LL, setLocale } = initI18nSvelte(loadedLocales, loadedFormatters) + +export { locale, LL, setLocale } + +export default LL diff --git a/src/i18n/i18n-types.ts b/src/i18n/i18n-types.ts new file mode 100644 index 0000000..9a32b2f --- /dev/null +++ b/src/i18n/i18n-types.ts @@ -0,0 +1,66 @@ +// This file was auto-generated by 'typesafe-i18n'. Any manual changes will be overwritten. +/* eslint-disable */ +import type { BaseTranslation as BaseTranslationType, LocalizedString } from 'typesafe-i18n' + +export type BaseTranslation = BaseTranslationType +export type BaseLocale = 'en' + +export type Locales = + | 'en' + | 'nb' + +export type Translation = RootTranslation + +export type Translations = RootTranslation + +type RootTranslation = { + contact: { + /** + * C​o​n​t​a​c​t​ ​u​s + */ + title: string + /** + * A​d​d​r​e​s​s + */ + addressTitle: string + /** + * E​m​a​i​l + */ + emailTitle: string + /** + * P​h​o​n​e + */ + phoneTitle: string + } + /** + * H​o​m​e + */ + homeTitle: string +} + +export type TranslationFunctions = { + contact: { + /** + * Contact us + */ + title: () => LocalizedString + /** + * Address + */ + addressTitle: () => LocalizedString + /** + * Email + */ + emailTitle: () => LocalizedString + /** + * Phone + */ + phoneTitle: () => LocalizedString + } + /** + * Home + */ + homeTitle: () => LocalizedString +} + +export type Formatters = {} diff --git a/src/i18n/i18n-util.async.ts b/src/i18n/i18n-util.async.ts new file mode 100644 index 0000000..3f38e3e --- /dev/null +++ b/src/i18n/i18n-util.async.ts @@ -0,0 +1,27 @@ +// This file was auto-generated by 'typesafe-i18n'. Any manual changes will be overwritten. +/* eslint-disable */ + +import { initFormatters } from './formatters' +import type { Locales, Translations } from './i18n-types' +import { loadedFormatters, loadedLocales, locales } from './i18n-util' + +const localeTranslationLoaders = { + en: () => import('./en'), + nb: () => import('./nb'), +} + +const updateDictionary = (locale: Locales, dictionary: Partial): Translations => + loadedLocales[locale] = { ...loadedLocales[locale], ...dictionary } + +export const importLocaleAsync = async (locale: Locales): Promise => + (await localeTranslationLoaders[locale]()).default as unknown as Translations + +export const loadLocaleAsync = async (locale: Locales): Promise => { + updateDictionary(locale, await importLocaleAsync(locale)) + loadFormatters(locale) +} + +export const loadAllLocalesAsync = (): Promise => Promise.all(locales.map(loadLocaleAsync)) + +export const loadFormatters = (locale: Locales): void => + void (loadedFormatters[locale] = initFormatters(locale)) diff --git a/src/i18n/i18n-util.sync.ts b/src/i18n/i18n-util.sync.ts new file mode 100644 index 0000000..f1a8e9e --- /dev/null +++ b/src/i18n/i18n-util.sync.ts @@ -0,0 +1,26 @@ +// This file was auto-generated by 'typesafe-i18n'. Any manual changes will be overwritten. +/* eslint-disable */ + +import { initFormatters } from './formatters' +import type { Locales, Translations } from './i18n-types' +import { loadedFormatters, loadedLocales, locales } from './i18n-util' + +import en from './en' +import nb from './nb' + +const localeTranslations = { + en, + nb, +} + +export const loadLocale = (locale: Locales): void => { + if (loadedLocales[locale]) return + + loadedLocales[locale] = localeTranslations[locale] as unknown as Translations + loadFormatters(locale) +} + +export const loadAllLocales = (): void => locales.forEach(loadLocale) + +export const loadFormatters = (locale: Locales): void => + void (loadedFormatters[locale] = initFormatters(locale)) diff --git a/src/i18n/i18n-util.ts b/src/i18n/i18n-util.ts new file mode 100644 index 0000000..0d1cef0 --- /dev/null +++ b/src/i18n/i18n-util.ts @@ -0,0 +1,38 @@ +// This file was auto-generated by 'typesafe-i18n'. Any manual changes will be overwritten. +/* eslint-disable */ + +import { i18n as initI18n, i18nObject as initI18nObject, i18nString as initI18nString } from 'typesafe-i18n' +import type { LocaleDetector } from 'typesafe-i18n/detectors' +import type { LocaleTranslationFunctions, TranslateByString } from 'typesafe-i18n' +import { detectLocale as detectLocaleFn } from 'typesafe-i18n/detectors' +import { initExtendDictionary } from 'typesafe-i18n/utils' +import type { Formatters, Locales, Translations, TranslationFunctions } from './i18n-types' + +export const baseLocale: Locales = 'en' + +export const locales: Locales[] = [ + 'en', + 'nb' +] + +export const isLocale = (locale: string): locale is Locales => locales.includes(locale as Locales) + +export const loadedLocales: Record = {} as Record + +export const loadedFormatters: Record = {} as Record + +export const extendDictionary = initExtendDictionary() + +export const i18nString = (locale: Locales): TranslateByString => initI18nString(locale, loadedFormatters[locale]) + +export const i18nObject = (locale: Locales): TranslationFunctions => + initI18nObject( + locale, + loadedLocales[locale], + loadedFormatters[locale] + ) + +export const i18n = (): LocaleTranslationFunctions => + initI18n(loadedLocales, loadedFormatters) + +export const detectLocale = (...detectors: LocaleDetector[]): Locales => detectLocaleFn(baseLocale, locales, ...detectors) diff --git a/src/i18n/nb/index.ts b/src/i18n/nb/index.ts new file mode 100644 index 0000000..9e13fba --- /dev/null +++ b/src/i18n/nb/index.ts @@ -0,0 +1,13 @@ +import type { Translation } from '../i18n-types' + +const nb = { + contact: { + title: "Kontakt oss", + addressTitle: "Adresse", + emailTitle: "E-postadresse", + phoneTitle: "Telefon" + }, + homeTitle: "Hjem", +} satisfies Translation + +export default nb -- cgit v1.3