mirror of
https://github.com/upscayl/upscayl.git
synced 2025-02-03 13:13:32 +01:00
36 lines
1.2 KiB
TypeScript
36 lines
1.2 KiB
TypeScript
import { atom } from "jotai";
|
|
import en from "../locales/en.json";
|
|
import ru from "../locales/ru.json";
|
|
import ja from "../locales/ja.json";
|
|
import { atomWithStorage } from "jotai/utils";
|
|
|
|
// Define the shape of the translations
|
|
type Translations = typeof en;
|
|
type Locales = "en" | "ru" | "ja";
|
|
|
|
// Utility function to access nested translation keys
|
|
const getNestedTranslation = (obj: Translations, key: string): string => {
|
|
return (
|
|
key.split(".").reduce((acc, part) => acc && (acc as any)[part], obj) || key
|
|
);
|
|
};
|
|
|
|
// Atom to store the current locale
|
|
export const localeAtom = atomWithStorage<Locales>("language", "en");
|
|
|
|
// Atom to get the translation function based on the current locale
|
|
export const translationAtom = atom((get) => {
|
|
const locale = get(localeAtom);
|
|
const translations: Record<Locales, Translations> = { en, ru, ja };
|
|
|
|
return (key: string, params: Record<string, string> = {}): string => {
|
|
const template = getNestedTranslation(translations[locale], key);
|
|
|
|
// Replace placeholders with parameters, e.g., {name} => John
|
|
return Object.keys(params).reduce(
|
|
(str, paramKey) => str.replace(`{${paramKey}}`, params[paramKey]),
|
|
template,
|
|
);
|
|
};
|
|
});
|