mirror of
https://github.com/danbulant/Portfolio
synced 2026-06-09 17:52:14 +00:00
feat: init typesafe-i18n
This commit is contained in:
parent
339921a131
commit
58b2a797b4
11 changed files with 159 additions and 2 deletions
7
frontend/.typesafe-i18n.json
Normal file
7
frontend/.typesafe-i18n.json
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
{
|
||||||
|
"baseLocale": "cs",
|
||||||
|
"adapter": "svelte",
|
||||||
|
"esmImports": true,
|
||||||
|
"outputPath": "./src/translations",
|
||||||
|
"$schema": "https://unpkg.com/typesafe-i18n@5.20.0/schema/typesafe-i18n.json"
|
||||||
|
}
|
||||||
|
|
@ -10,7 +10,8 @@
|
||||||
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
|
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
|
||||||
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
|
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
|
||||||
"lint": "prettier --plugin-search-dir . --check . && eslint .",
|
"lint": "prettier --plugin-search-dir . --check . && eslint .",
|
||||||
"format": "prettier --plugin-search-dir . --write ."
|
"format": "prettier --plugin-search-dir . --write .",
|
||||||
|
"typesafe-i18n": "typesafe-i18n"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@playwright/test": "^1.29.2",
|
"@playwright/test": "^1.29.2",
|
||||||
|
|
@ -46,6 +47,7 @@
|
||||||
"svelte-tippy": "^1.3.2",
|
"svelte-tippy": "^1.3.2",
|
||||||
"swiper": "^8.4.6",
|
"swiper": "^8.4.6",
|
||||||
"tippy.js": "^6.3.7",
|
"tippy.js": "^6.3.7",
|
||||||
|
"typesafe-i18n": "^5.20.0",
|
||||||
"yup": "^0.32.11"
|
"yup": "^0.32.11"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -34,6 +34,9 @@ dependencies:
|
||||||
tippy.js:
|
tippy.js:
|
||||||
specifier: ^6.3.7
|
specifier: ^6.3.7
|
||||||
version: 6.3.7
|
version: 6.3.7
|
||||||
|
typesafe-i18n:
|
||||||
|
specifier: ^5.20.0
|
||||||
|
version: 5.20.0(typescript@4.9.4)
|
||||||
yup:
|
yup:
|
||||||
specifier: ^0.32.11
|
specifier: ^0.32.11
|
||||||
version: 0.32.11
|
version: 0.32.11
|
||||||
|
|
@ -2385,11 +2388,19 @@ packages:
|
||||||
engines: {node: '>=10'}
|
engines: {node: '>=10'}
|
||||||
dev: true
|
dev: true
|
||||||
|
|
||||||
|
/typesafe-i18n@5.20.0(typescript@4.9.4):
|
||||||
|
resolution: {integrity: sha512-uOvKnVkp1tXRDNBz9Aom54qs0LP2xWrtDliMPdKm9Scsnvn0DC7ZqjSGdOWxVplpbFbqYWNZuzx5Q5jWOjnBTA==}
|
||||||
|
hasBin: true
|
||||||
|
peerDependencies:
|
||||||
|
typescript: '>=3.5.1'
|
||||||
|
dependencies:
|
||||||
|
typescript: 4.9.4
|
||||||
|
dev: false
|
||||||
|
|
||||||
/typescript@4.9.4:
|
/typescript@4.9.4:
|
||||||
resolution: {integrity: sha512-Uz+dTXYzxXXbsFpM86Wh3dKCxrQqUcVMxwU54orwlJjOpO3ao8L7j5lH+dWfTwgCwIuM9GQ2kvVotzYJMXTBZg==}
|
resolution: {integrity: sha512-Uz+dTXYzxXXbsFpM86Wh3dKCxrQqUcVMxwU54orwlJjOpO3ao8L7j5lH+dWfTwgCwIuM9GQ2kvVotzYJMXTBZg==}
|
||||||
engines: {node: '>=4.2.0'}
|
engines: {node: '>=4.2.0'}
|
||||||
hasBin: true
|
hasBin: true
|
||||||
dev: true
|
|
||||||
|
|
||||||
/unconfig@0.2.2:
|
/unconfig@0.2.2:
|
||||||
resolution: {integrity: sha512-JN1MeYJ/POnjBj7NgOJJxPp6+NcD6Nd0hEuK0D89kjm9GvQQUq8HeE2Eb7PZgtu+64mWkDiqeJn1IZoLH7htPg==}
|
resolution: {integrity: sha512-JN1MeYJ/POnjBj7NgOJJxPp6+NcD6Nd0hEuK0D89kjm9GvQQUq8HeE2Eb7PZgtu+64mWkDiqeJn1IZoLH7htPg==}
|
||||||
|
|
|
||||||
8
frontend/src/translations/cs/index.ts
Normal file
8
frontend/src/translations/cs/index.ts
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
import type { BaseTranslation } from '../i18n-types.js'
|
||||||
|
|
||||||
|
const cs: BaseTranslation = {
|
||||||
|
// TODO: your translations go here
|
||||||
|
HI: 'Hi {name:string}! Please leave a star if you like this project: https://github.com/ivanhofer/typesafe-i18n',
|
||||||
|
}
|
||||||
|
|
||||||
|
export default cs
|
||||||
8
frontend/src/translations/de/index.ts
Normal file
8
frontend/src/translations/de/index.ts
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
import type { Translation } from '../i18n-types.js'
|
||||||
|
|
||||||
|
const de: Translation = {
|
||||||
|
// this is an example Translation, just rename or delete this folder if you want
|
||||||
|
HI: 'Hallo {name}! Bitte hinterlasse einen Stern, wenn dir das Projekt gefällt: https://github.com/ivanhofer/typesafe-i18n',
|
||||||
|
}
|
||||||
|
|
||||||
|
export default de
|
||||||
11
frontend/src/translations/formatters.ts
Normal file
11
frontend/src/translations/formatters.ts
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
import type { FormattersInitializer } from 'typesafe-i18n'
|
||||||
|
import type { Locales, Formatters } from './i18n-types.js'
|
||||||
|
|
||||||
|
export const initFormatters: FormattersInitializer<Locales, Formatters> = (locale: Locales) => {
|
||||||
|
|
||||||
|
const formatters: Formatters = {
|
||||||
|
// add your formatter functions here
|
||||||
|
}
|
||||||
|
|
||||||
|
return formatters
|
||||||
|
}
|
||||||
12
frontend/src/translations/i18n-svelte.ts
Normal file
12
frontend/src/translations/i18n-svelte.ts
Normal file
|
|
@ -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.js'
|
||||||
|
import { loadedFormatters, loadedLocales } from './i18n-util.js'
|
||||||
|
|
||||||
|
const { locale, LL, setLocale } = initI18nSvelte<Locales, Translations, TranslationFunctions, Formatters>(loadedLocales, loadedFormatters)
|
||||||
|
|
||||||
|
export { locale, LL, setLocale }
|
||||||
|
|
||||||
|
export default LL
|
||||||
19
frontend/src/translations/i18n-types.ts
Normal file
19
frontend/src/translations/i18n-types.ts
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
// This file was auto-generated by 'typesafe-i18n'. Any manual changes will be overwritten.
|
||||||
|
/* eslint-disable */
|
||||||
|
import type { BaseTranslation as BaseTranslationType } from 'typesafe-i18n'
|
||||||
|
|
||||||
|
export type BaseTranslation = BaseTranslationType
|
||||||
|
export type BaseLocale = 'cs'
|
||||||
|
|
||||||
|
export type Locales =
|
||||||
|
| 'cs'
|
||||||
|
|
||||||
|
export type Translation = RootTranslation
|
||||||
|
|
||||||
|
export type Translations = RootTranslation
|
||||||
|
|
||||||
|
type RootTranslation = {}
|
||||||
|
|
||||||
|
export type TranslationFunctions = {}
|
||||||
|
|
||||||
|
export type Formatters = {}
|
||||||
25
frontend/src/translations/i18n-util.async.ts
Normal file
25
frontend/src/translations/i18n-util.async.ts
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
// This file was auto-generated by 'typesafe-i18n'. Any manual changes will be overwritten.
|
||||||
|
/* eslint-disable */
|
||||||
|
|
||||||
|
import { initFormatters } from './formatters.js'
|
||||||
|
import type { Locales, Translations } from './i18n-types.js'
|
||||||
|
import { loadedFormatters, loadedLocales, locales } from './i18n-util.js'
|
||||||
|
|
||||||
|
const localeTranslationLoaders = {
|
||||||
|
}
|
||||||
|
|
||||||
|
const updateDictionary = (locale: Locales, dictionary: Partial<Translations>): Translations =>
|
||||||
|
loadedLocales[locale] = { ...loadedLocales[locale], ...dictionary }
|
||||||
|
|
||||||
|
export const importLocaleAsync = async (locale: Locales): Promise<Translations> =>
|
||||||
|
(await localeTranslationLoaders[locale]()).default as unknown as Translations
|
||||||
|
|
||||||
|
export const loadLocaleAsync = async (locale: Locales): Promise<void> => {
|
||||||
|
updateDictionary(locale, await importLocaleAsync(locale))
|
||||||
|
loadFormatters(locale)
|
||||||
|
}
|
||||||
|
|
||||||
|
export const loadAllLocalesAsync = (): Promise<void[]> => Promise.all(locales.map(loadLocaleAsync))
|
||||||
|
|
||||||
|
export const loadFormatters = (locale: Locales): void =>
|
||||||
|
void (loadedFormatters[locale] = initFormatters(locale))
|
||||||
21
frontend/src/translations/i18n-util.sync.ts
Normal file
21
frontend/src/translations/i18n-util.sync.ts
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
// This file was auto-generated by 'typesafe-i18n'. Any manual changes will be overwritten.
|
||||||
|
/* eslint-disable */
|
||||||
|
|
||||||
|
import { initFormatters } from './formatters.js'
|
||||||
|
import type { Locales, Translations } from './i18n-types.js'
|
||||||
|
import { loadedFormatters, loadedLocales, locales } from './i18n-util.js'
|
||||||
|
|
||||||
|
const localeTranslations = {
|
||||||
|
}
|
||||||
|
|
||||||
|
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))
|
||||||
33
frontend/src/translations/i18n-util.ts
Normal file
33
frontend/src/translations/i18n-util.ts
Normal file
|
|
@ -0,0 +1,33 @@
|
||||||
|
// 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 type { Formatters, Locales, Translations, TranslationFunctions } from './i18n-types.js'
|
||||||
|
|
||||||
|
export const baseLocale: Locales = 'cs'
|
||||||
|
|
||||||
|
export const locales: Locales[] = [
|
||||||
|
]
|
||||||
|
|
||||||
|
export const isLocale = (locale: string): locale is Locales => locales.includes(locale as Locales)
|
||||||
|
|
||||||
|
export const loadedLocales: Record<Locales, Translations> = {} as Record<Locales, Translations>
|
||||||
|
|
||||||
|
export const loadedFormatters: Record<Locales, Formatters> = {} as Record<Locales, Formatters>
|
||||||
|
|
||||||
|
export const i18nString = (locale: Locales): TranslateByString => initI18nString<Locales, Formatters>(locale, loadedFormatters[locale])
|
||||||
|
|
||||||
|
export const i18nObject = (locale: Locales): TranslationFunctions =>
|
||||||
|
initI18nObject<Locales, Translations, TranslationFunctions, Formatters>(
|
||||||
|
locale,
|
||||||
|
loadedLocales[locale],
|
||||||
|
loadedFormatters[locale]
|
||||||
|
)
|
||||||
|
|
||||||
|
export const i18n = (): LocaleTranslationFunctions<Locales, Translations, TranslationFunctions> =>
|
||||||
|
initI18n<Locales, Translations, TranslationFunctions, Formatters>(loadedLocales, loadedFormatters)
|
||||||
|
|
||||||
|
export const detectLocale = (...detectors: LocaleDetector[]): Locales => detectLocaleFn<Locales>(baseLocale, locales, ...detectors)
|
||||||
Loading…
Reference in a new issue