diff --git a/packages/i18n/package.json b/packages/i18n/package.json index e9a7905e..1ca979f0 100644 --- a/packages/i18n/package.json +++ b/packages/i18n/package.json @@ -36,11 +36,13 @@ } }, "exports": { - "./*/locale": "./src/server/*/locale.js", - "./*": "./src/server/*/index.js", + "./core/*": { + "@primate/lt": "./src/public/*.js", + "default": "./src/public/*.js" + }, ".": { - "runtime": "./src/runtime.js", - "default": "./src/default.js" + "runtime": "./src/public/runtime.js", + "default": "./src/public/default.js" } } } diff --git a/packages/i18n/src/private/header.js b/packages/i18n/src/private/header.js new file mode 100644 index 00000000..90d6ad31 --- /dev/null +++ b/packages/i18n/src/private/header.js @@ -0,0 +1 @@ +export default "Primate-I18N-Locale"; diff --git a/packages/i18n/src/private/hook/handle.js b/packages/i18n/src/private/hook/handle.js index 6f5c7e5b..32ba4aa9 100644 --- a/packages/i18n/src/private/hook/handle.js +++ b/packages/i18n/src/private/hook/handle.js @@ -1,3 +1,4 @@ +import header from "#header"; import name from "#name"; import { OK } from "@rcompat/http/status"; @@ -14,7 +15,7 @@ export default ({ env }) => (request, next) => { return next(request); } - const set_locale = request.headers.get("primate-i18n-locale"); + const set_locale = request.headers.get(header); if (set_locale === undefined) { return next(request); diff --git a/packages/i18n/src/private/save.js b/packages/i18n/src/private/save.js index 3acc0f4c..af59759e 100644 --- a/packages/i18n/src/private/save.js +++ b/packages/i18n/src/private/save.js @@ -1,7 +1,7 @@ +import header from "#header"; + export default locale => fetch("/", { - headers: { - "Primate-I18N-Locale": locale, - }, + headers: { [header]: locale }, method: "post", body: null, }); diff --git a/packages/i18n/src/default.js b/packages/i18n/src/public/default.js similarity index 100% rename from packages/i18n/src/default.js rename to packages/i18n/src/public/default.js diff --git a/packages/i18n/src/public/resolve.js b/packages/i18n/src/public/resolve.js new file mode 100644 index 00000000..56099cde --- /dev/null +++ b/packages/i18n/src/public/resolve.js @@ -0,0 +1 @@ +export { default } from "#resolve"; diff --git a/packages/i18n/src/runtime.js b/packages/i18n/src/public/runtime.js similarity index 100% rename from packages/i18n/src/runtime.js rename to packages/i18n/src/public/runtime.js diff --git a/packages/i18n/src/public/save.js b/packages/i18n/src/public/save.js new file mode 100644 index 00000000..2c97cf52 --- /dev/null +++ b/packages/i18n/src/public/save.js @@ -0,0 +1 @@ +export { default } from "#save"; diff --git a/packages/react/package.json b/packages/react/package.json index e0dad284..6206cccf 100644 --- a/packages/react/package.json +++ b/packages/react/package.json @@ -21,10 +21,16 @@ "@rcompat/platform": "^0.3.0" }, "peerDependencies": { + "@primate/i18n": "workspace:^", "primate": "workspace:^", "react": "18", "react-dom": "18" }, + "peerDependenciesMeta": { + "@primate/i18n": { + "optional": true + } + }, "type": "module", "imports": { "#*": { @@ -50,6 +56,10 @@ "#client/*": { "@primate/lt": "./src/private/client/*.js", "default": "./src/private/client/*.js" + }, + "#i18n/*": { + "@primate/lt": "./src/private/i18n/*.js", + "default": "./src/private/i18n/*.js" } }, "exports": { @@ -57,7 +67,7 @@ "runtime": "./src/public/runtime.js", "default": "./src/public/default.js" }, - "./head": "./src/public/head.js", + "./*": "./src/public/*.js", "./context/*": "./src/public/context/*.js" } } diff --git a/packages/i18n/src/server/react/index.js b/packages/react/src/private/i18n/index.js similarity index 63% rename from packages/i18n/src/server/react/index.js rename to packages/react/src/private/i18n/index.js index a46324b8..2af95a3f 100644 --- a/packages/i18n/src/server/react/index.js +++ b/packages/react/src/private/i18n/index.js @@ -1,7 +1,7 @@ -import resolve from "#resolve"; -import AppContext from "@primate/react/context/app"; +import AppContext from "#context/app"; +import locale_store from "#i18n/locale"; +import resolve from "@primate/i18n/core/resolve"; import { useContext } from "react"; -import locale_store from "./locale.js"; export default (key, placeholders) => { locale_store.init(); diff --git a/packages/i18n/src/server/react/locale.js b/packages/react/src/private/i18n/locale.js similarity index 89% rename from packages/i18n/src/server/react/locale.js rename to packages/react/src/private/i18n/locale.js index e8c40643..23a82a91 100644 --- a/packages/i18n/src/server/react/locale.js +++ b/packages/react/src/private/i18n/locale.js @@ -1,5 +1,5 @@ -import save from "#save"; -import AppContext from "@primate/react/context/app"; +import AppContext from "#context/app"; +import save from "@primate/i18n/core/save"; import { useContext } from "react"; const $ = { diff --git a/packages/react/src/public/head.js b/packages/react/src/public/head.js index adfae60e..c3f71008 100644 --- a/packages/react/src/public/head.js +++ b/packages/react/src/public/head.js @@ -1,4 +1,4 @@ -import HeadContext from "@primate/react/context/head"; +import HeadContext from "#context/head"; import platform from "@rcompat/platform"; import { Component } from "react"; diff --git a/packages/react/src/public/i18n.js b/packages/react/src/public/i18n.js new file mode 100644 index 00000000..8e764b1c --- /dev/null +++ b/packages/react/src/public/i18n.js @@ -0,0 +1 @@ +export { default } from "#i18n/index"; diff --git a/packages/react/src/public/locale.js b/packages/react/src/public/locale.js new file mode 100644 index 00000000..cc969e42 --- /dev/null +++ b/packages/react/src/public/locale.js @@ -0,0 +1 @@ +export { default } from "#i18n/locale"; diff --git a/packages/solid/package.json b/packages/solid/package.json index 33910268..61804a96 100644 --- a/packages/solid/package.json +++ b/packages/solid/package.json @@ -22,10 +22,16 @@ }, "peerDependencies": { "@babel/core": "7", + "@primate/i18n": "workspace:^", "babel-preset-solid": "1", "primate": "workspace:^", "solid-js": "1" }, + "peerDependenciesMeta": { + "@primate/i18n": { + "optional": true + } + }, "type": "module", "imports": { "#*": { @@ -51,6 +57,10 @@ "#client/*": { "@primate/lt": "./src/private/client/*.js", "default": "./src/private/client/*.js" + }, + "#i18n/*": { + "@primate/lt": "./src/private/i18n/*.js", + "default": "./src/private/i18n/*.js" } }, "exports": { @@ -58,7 +68,7 @@ "runtime": "./src/public/runtime.js", "default": "./src/public/default.js" }, - "./head": "./src/public/head.js", + "./*": "./src/public/*.js", "./context/*": "./src/public/context/*.js" } } diff --git a/packages/i18n/src/server/solid/index.js b/packages/solid/src/private/i18n/index.js similarity index 75% rename from packages/i18n/src/server/solid/index.js rename to packages/solid/src/private/i18n/index.js index 9cd4482f..9f8c8c00 100644 --- a/packages/i18n/src/server/solid/index.js +++ b/packages/solid/src/private/i18n/index.js @@ -1,5 +1,5 @@ -import resolve from "#resolve"; -import AppContext from "@primate/solid/context/app"; +import resolve from "@primate/i18n/core/resolve"; +import AppContext from "#context/app"; import { useContext } from "solid-js"; import locale_store from "./locale.js"; diff --git a/packages/i18n/src/server/solid/locale.js b/packages/solid/src/private/i18n/locale.js similarity index 89% rename from packages/i18n/src/server/solid/locale.js rename to packages/solid/src/private/i18n/locale.js index 3e44b983..dad34f98 100644 --- a/packages/i18n/src/server/solid/locale.js +++ b/packages/solid/src/private/i18n/locale.js @@ -1,5 +1,5 @@ -import save from "#save"; -import AppContext from "@primate/solid/context/app"; +import AppContext from "#context/app"; +import save from "@primate/i18n/core/save"; import { useContext } from "solid-js"; const $ = { diff --git a/packages/solid/src/public/i18n.js b/packages/solid/src/public/i18n.js new file mode 100644 index 00000000..8e764b1c --- /dev/null +++ b/packages/solid/src/public/i18n.js @@ -0,0 +1 @@ +export { default } from "#i18n/index"; diff --git a/packages/solid/src/public/locale.js b/packages/solid/src/public/locale.js new file mode 100644 index 00000000..cc969e42 --- /dev/null +++ b/packages/solid/src/public/locale.js @@ -0,0 +1 @@ +export { default } from "#i18n/locale"; diff --git a/packages/svelte/package.json b/packages/svelte/package.json index 3c52095a..89ccaa8f 100644 --- a/packages/svelte/package.json +++ b/packages/svelte/package.json @@ -19,9 +19,15 @@ "@rcompat/fs": "^0.4.0" }, "peerDependencies": { + "@primate/i18n": "workspace:^", "primate": "workspace:^", "svelte": "4" }, + "peerDependenciesMeta": { + "@primate/i18n": { + "optional": true + } + }, "type": "module", "imports": { "#*": { @@ -43,12 +49,17 @@ "#client/*": { "@primate/lt": "./src/private/client/*.js", "default": "./src/private/client/*.js" + }, + "#i18n/*": { + "@primate/lt": "./src/private/i18n/*.js", + "default": "./src/private/i18n/*.js" } }, "exports": { ".": { "runtime": "./src/public/runtime.js", "default": "./src/public/default.js" - } + }, + "./*": "./src/public/*.js" } } diff --git a/packages/svelte/src/private/client/create-root.js b/packages/svelte/src/private/client/create-root.js index 76a42ddf..a20f473b 100644 --- a/packages/svelte/src/private/client/create-root.js +++ b/packages/svelte/src/private/client/create-root.js @@ -14,6 +14,7 @@ export default length => { return ` diff --git a/packages/svelte/src/private/context-name.js b/packages/svelte/src/private/context-name.js new file mode 100644 index 00000000..25e736a4 --- /dev/null +++ b/packages/svelte/src/private/context-name.js @@ -0,0 +1 @@ +export default "__PRMT__"; diff --git a/packages/i18n/src/server/svelte/index.js b/packages/svelte/src/private/i18n/index.js similarity index 52% rename from packages/i18n/src/server/svelte/index.js rename to packages/svelte/src/private/i18n/index.js index 15827346..89dc1688 100644 --- a/packages/i18n/src/server/svelte/index.js +++ b/packages/svelte/src/private/i18n/index.js @@ -1,9 +1,10 @@ -import resolve from "#resolve"; +import context_name from "#context-name"; +import locale_store from "#i18n/locale"; +import resolve from "@primate/i18n/core/resolve"; import { getContext } from "svelte"; import { derived } from "svelte/store"; -import locale_store from "./locale.js"; export default derived(locale_store, locale => (key, placeholders) => { - const { locales } = getContext("__primate__").i18n; + const { locales } = getContext(context_name).i18n; return resolve(locales[locale], key, placeholders); }); diff --git a/packages/i18n/src/server/svelte/locale.js b/packages/svelte/src/private/i18n/locale.js similarity index 66% rename from packages/i18n/src/server/svelte/locale.js rename to packages/svelte/src/private/i18n/locale.js index 3b4ead21..ae501918 100644 --- a/packages/i18n/src/server/svelte/locale.js +++ b/packages/svelte/src/private/i18n/locale.js @@ -1,9 +1,10 @@ -import save from "#save"; +import context_name from "#context-name"; +import save from "@primate/i18n/core/save"; import { getContext } from "svelte"; import { writable } from "svelte/store"; const store = writable("", set => { - const { locale } = getContext("__primate__").i18n; + const { locale } = getContext(context_name).i18n; set(locale); return () => undefined; diff --git a/packages/svelte/src/public/context-name.js b/packages/svelte/src/public/context-name.js new file mode 100644 index 00000000..0d1b2aeb --- /dev/null +++ b/packages/svelte/src/public/context-name.js @@ -0,0 +1 @@ +export { default } from "#context-name"; diff --git a/packages/svelte/src/public/i18n.js b/packages/svelte/src/public/i18n.js new file mode 100644 index 00000000..8e764b1c --- /dev/null +++ b/packages/svelte/src/public/i18n.js @@ -0,0 +1 @@ +export { default } from "#i18n/index"; diff --git a/packages/svelte/src/public/locale.js b/packages/svelte/src/public/locale.js new file mode 100644 index 00000000..cc969e42 --- /dev/null +++ b/packages/svelte/src/public/locale.js @@ -0,0 +1 @@ +export { default } from "#i18n/locale";