zikade 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1 @@
1
+ # Zikade
@@ -0,0 +1,16 @@
1
+ import React, { ReactNode } from "react";
2
+ import { createZikade } from "./createZikade";
3
+ import type { LocaleTranslations } from "./types";
4
+ type ZikadeContextType<T extends LocaleTranslations> = {
5
+ zikade: ReturnType<typeof createZikade<T>>;
6
+ currentLocale: keyof T;
7
+ setLanguage: (locale: keyof T) => void;
8
+ };
9
+ declare const ZikadeContext: React.Context<ZikadeContextType<any> | undefined>;
10
+ type ZikadeProviderProps<T extends LocaleTranslations> = {
11
+ translations: T;
12
+ defaultLocale: keyof T;
13
+ children: ReactNode;
14
+ };
15
+ export declare function ZikadeProvider<T extends LocaleTranslations>({ translations, defaultLocale, children }: ZikadeProviderProps<T>): import("react/jsx-runtime").JSX.Element;
16
+ export { ZikadeContext };
@@ -0,0 +1,19 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { createContext, useState, useMemo } from "react";
3
+ import { createZikade } from "./createZikade"; // <-- corrected import
4
+ const ZikadeContext = createContext(undefined);
5
+ export function ZikadeProvider({ translations, defaultLocale, children }) {
6
+ const [currentLocale, setCurrentLocale] = useState(defaultLocale);
7
+ const zikade = useMemo(() => {
8
+ const instance = createZikade(translations, defaultLocale);
9
+ instance.setLanguage(currentLocale);
10
+ return instance;
11
+ }, [translations, currentLocale, defaultLocale]);
12
+ const contextValue = useMemo(() => ({
13
+ zikade,
14
+ currentLocale,
15
+ setLanguage: setCurrentLocale // <-- cast here
16
+ }), [zikade, currentLocale]);
17
+ return _jsx(ZikadeContext.Provider, { value: contextValue, children: children });
18
+ }
19
+ export { ZikadeContext };
@@ -0,0 +1,7 @@
1
+ import type { LocaleTranslations } from "./types.ts";
2
+ /**
3
+ * Creates a dot-access, multi-locale translation object with setLanguage()
4
+ */
5
+ export declare function createZikade<T extends LocaleTranslations>(translations: T, defaultLocale: keyof T): T[keyof T] & {
6
+ setLanguage: (locale: keyof T) => void;
7
+ };
@@ -0,0 +1,69 @@
1
+ /**
2
+ * Runtime validation: ensures all locales have the same keys
3
+ */
4
+ function validateTranslations(translations, baseLocale) {
5
+ const locales = Object.keys(translations);
6
+ const base = baseLocale ? translations[baseLocale] : translations[locales[0]];
7
+ function compareKeys(baseObj, otherObj, path = [], localeName = "") {
8
+ for (const key of Object.keys(baseObj)) {
9
+ const fullPath = [...path, key].join(".");
10
+ if (!(key in otherObj)) {
11
+ console.error(`[zikade] Missing translation key '${fullPath}' in locale '${localeName}'`);
12
+ }
13
+ else if (typeof baseObj[key] === "object" &&
14
+ typeof otherObj[key] === "object") {
15
+ compareKeys(baseObj[key], otherObj[key], [...path, key], localeName);
16
+ }
17
+ }
18
+ }
19
+ for (const locale of locales) {
20
+ if (translations[locale] !== base) {
21
+ compareKeys(base, translations[locale], [], locale);
22
+ }
23
+ }
24
+ }
25
+ /**
26
+ * Creates a dot-access, multi-locale translation object with setLanguage()
27
+ */
28
+ export function createZikade(translations, defaultLocale) {
29
+ // 1️⃣ Validate translations
30
+ validateTranslations(translations);
31
+ // 2️⃣ Current locale
32
+ let currentLocale = defaultLocale;
33
+ function setLanguage(locale) {
34
+ if (!(locale in translations)) {
35
+ console.warn(`[zikade] Locale '${String(locale)}' not found. Falling back to '${String(defaultLocale)}'.`);
36
+ currentLocale = defaultLocale;
37
+ }
38
+ else {
39
+ currentLocale = locale;
40
+ }
41
+ }
42
+ // 3️⃣ Recursive proxy that dynamically reads from currentLocale
43
+ function createProxyForPath(path = []) {
44
+ return new Proxy({}, {
45
+ get(_, prop) {
46
+ const key = typeof prop === "string" ? prop : String(prop);
47
+ const fullPath = [...path, key];
48
+ let val = translations[currentLocale];
49
+ for (const p of fullPath) {
50
+ if (val == null)
51
+ return key; // fallback if missing
52
+ val = val[p];
53
+ }
54
+ if (typeof val === "object")
55
+ return createProxyForPath(fullPath);
56
+ return val;
57
+ }
58
+ });
59
+ }
60
+ const proxy = createProxyForPath();
61
+ // 4️⃣ Return a proxy that merges setLanguage and top-level translation keys
62
+ return new Proxy({ setLanguage }, {
63
+ get(_, prop) {
64
+ if (prop === "setLanguage")
65
+ return setLanguage;
66
+ return proxy[prop];
67
+ }
68
+ });
69
+ }
@@ -0,0 +1,4 @@
1
+ export * from "./createZikade";
2
+ export * from "./types";
3
+ export * from "./ZikadeProvider";
4
+ export * from "./useZikade";
package/dist/index.js ADDED
@@ -0,0 +1,4 @@
1
+ export * from "./createZikade";
2
+ export * from "./types";
3
+ export * from "./ZikadeProvider";
4
+ export * from "./useZikade";
@@ -0,0 +1,5 @@
1
+ export type TranslationLeaf = string;
2
+ export type TranslationObject = {
3
+ [key: string]: TranslationLeaf | TranslationObject;
4
+ };
5
+ export type LocaleTranslations = Record<string, TranslationObject>;
package/dist/types.js ADDED
@@ -0,0 +1,2 @@
1
+ // src/types.ts
2
+ export {};
@@ -0,0 +1,4 @@
1
+ import type { LocaleTranslations } from "./types.ts";
2
+ export declare function useZikade<T extends LocaleTranslations>(): ReturnType<any> & {
3
+ setLanguage: (locale: keyof T) => void;
4
+ };
@@ -0,0 +1,8 @@
1
+ import { useContext } from "react";
2
+ import { ZikadeContext } from "../src/ZikadeProvider.js";
3
+ export function useZikade() {
4
+ const context = useContext(ZikadeContext);
5
+ if (!context)
6
+ throw new Error("useZikade must be used within a ZikadeProvider");
7
+ return context.zikade;
8
+ }
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "zikade",
3
+ "version": "0.1.0",
4
+ "author": "Sven_Guettner",
5
+ "description": "Type-safe, React-compatible internationalization library",
6
+ "main": "dist/index.js",
7
+ "module": "dist/index.js",
8
+ "types": "dist/index.d.ts",
9
+ "scripts": {
10
+ "build": "rm -rf dist && tsc",
11
+ "prepare": "npm run build"
12
+ },
13
+ "peerDependencies": {
14
+ "react": "^18.0.0"
15
+ },
16
+ "dependencies": {},
17
+ "devDependencies": {
18
+ "typescript": "^5.0.0",
19
+ "@types/react": "^19.2.7"
20
+ },
21
+ "files": [
22
+ "dist"
23
+ ],
24
+ "keywords": [
25
+ "i18n",
26
+ "typescript",
27
+ "react",
28
+ "internationalization",
29
+ "translations",
30
+ "type-safe"
31
+ ],
32
+ "license": "MIT"
33
+ }