create-middag-ui 0.27.0 → 0.29.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/lib/scaffold.js CHANGED
@@ -101,6 +101,9 @@ export function scaffoldPackageJson(targetDir, host, cwd, registryPath, hostKey,
101
101
  const deps = {
102
102
  "@middag-io/react": `^${getLibVersion()}`,
103
103
  "@fontsource-variable/figtree": "^5.0.0",
104
+ "i18next": "^26.3.1",
105
+ "i18next-browser-languagedetector": "^8.2.1",
106
+ "react-i18next": "^17.0.8",
104
107
  "react-router": "^7.0.0",
105
108
  };
106
109
  if (isPro) {
@@ -119,7 +122,7 @@ export function scaffoldPackageJson(targetDir, host, cwd, registryPath, hostKey,
119
122
  moodleDevDeps["tailwindcss"] = "^4.0.0";
120
123
  }
121
124
 
122
- // PRO: dev harness (host-sim shell, Inertia mocks, i18n) inherited at dev time
125
+ // PRO: dev harness (host-sim shell, Inertia mocks) inherited at dev time
123
126
  // from @middag-io/react-demo. Dev-only — production entry-*.tsx never imports it.
124
127
  const proDevDeps = {};
125
128
  if (isPro) {
@@ -6,14 +6,15 @@
6
6
  * mock/navigation.ts — sidebar structure
7
7
  * mock/data.ts — synthetic page props
8
8
  *
9
- * The dev harness (host-sim shell, Inertia mocks, i18n) is inherited from
9
+ * The dev harness (host-sim shell, Inertia mocks) is inherited from
10
10
  * @middag-io/react-demo — not generated locally. Vite aliases @inertiajs/*
11
11
  * to it (see vite.config.ts).
12
12
  */
13
13
  import {useEffect} from "react";
14
14
  import {BrowserRouter, Routes, useNavigate} from "react-router";
15
15
  import {I18nProvider} from "@middag-io/react";
16
- import {MockI18nProvider, MockPageProvider, setMockNavigate} from "@middag-io/react-demo";
16
+ import {MockPageProvider, setMockNavigate} from "@middag-io/react-demo";
17
+ import {moodleStringResolver} from "./lib/moodle/strings";
17
18
  import {buildNavigation} from "../mock/navigation";
18
19
  import {sharedProps} from "../mock/data";
19
20
  import {AppRoutes} from "../mock/routes";
@@ -35,20 +36,18 @@ if (typeof window !== "undefined") {
35
36
 
36
37
  export function App() {
37
38
  return (
38
- <MockI18nProvider>
39
- <MockPageProvider
40
- value={{
41
- props: { ...sharedProps, contract: null, navigation: buildNavigation("") },
42
- url: "/",
43
- }}
44
- >
45
- <I18nProvider>
46
- <BrowserRouter>
47
- <NavigateBridge />
48
- <Routes>{AppRoutes()}</Routes>
49
- </BrowserRouter>
50
- </I18nProvider>
51
- </MockPageProvider>
52
- </MockI18nProvider>
39
+ <MockPageProvider
40
+ value={{
41
+ props: { ...sharedProps, contract: null, navigation: buildNavigation("") },
42
+ url: "/",
43
+ }}
44
+ >
45
+ <I18nProvider asyncResolver={moodleStringResolver}>
46
+ <BrowserRouter>
47
+ <NavigateBridge />
48
+ <Routes>{AppRoutes()}</Routes>
49
+ </BrowserRouter>
50
+ </I18nProvider>
51
+ </MockPageProvider>
53
52
  );
54
53
  }
@@ -1,62 +1,27 @@
1
1
  /**
2
- * Typed adapter for Moodle's core/str AMD module.
2
+ * Moodle string resolver for I18nProvider.
3
3
  *
4
- * Wraps the native `get_strings` function with TypeScript types
5
- * and returns a key→value Record for convenient consumption.
4
+ * Implements the HostStringResolver contract: resolves one string at a time
5
+ * via Moodle's core/str AMD module (get_string). Used as the asyncResolver
6
+ * prop on I18nProvider so i18next fetches translations lazily on demand.
6
7
  *
7
- * Can be used as the asyncResolver for I18nProvider:
8
+ * Usage:
9
+ * import { moodleStringResolver } from "@/lib/moodle/strings";
10
+ * <I18nProvider asyncResolver={moodleStringResolver}>
8
11
  *
9
- * import { getString } from "@/lib/moodle/strings";
10
- * <I18nProvider asyncResolver={getString}>
11
- *
12
- * Change the default component below to match your plugin's frankenstyle.
12
+ * Change DEFAULT_COMPONENT below to match your plugin's frankenstyle name.
13
13
  */
14
14
 
15
- // @ts-expect-error AMD external — resolved by Moodle's RequireJS at runtime
16
- import {get_strings} from "core/str";
17
-
18
- export interface StringRequest {
19
- key: string;
20
- component?: string;
21
- param?: string | number;
22
- }
15
+ import type {HostStringResolver} from "@middag-io/react";
23
16
 
24
17
  // ── CHANGE THIS to your plugin's frankenstyle name ──
25
18
  const DEFAULT_COMPONENT = "__PLUGIN_PREFIX__";
26
19
 
27
- /**
28
- * Load translated strings from Moodle's string manager.
29
- *
30
- * @param requests - Array of string requests (key + optional component/param)
31
- * @returns Record mapping each key to its translated string
32
- */
33
- export async function getStrings(
34
- requests: StringRequest[],
35
- ): Promise<Record<string, string>> {
36
- const moodleRequests = requests.map((r) => ({
37
- key: r.key,
38
- component: r.component ?? DEFAULT_COMPONENT,
39
- param: r.param,
40
- }));
41
-
42
- const results: string[] = await get_strings(moodleRequests);
43
-
44
- const out: Record<string, string> = {};
45
- results.forEach((translated, index) => {
46
- out[requests[index].key] = translated;
20
+ /** Resolve one Moodle string lazily: get_string(key, component). */
21
+ export const moodleStringResolver: HostStringResolver = (key, component) =>
22
+ new Promise((resolve) => {
23
+ // @ts-expect-error Moodle AMD global
24
+ window.require(["core/str"], (str: {get_string: (k: string, c: string) => Promise<string>}) => {
25
+ str.get_string(key, component ?? DEFAULT_COMPONENT).then(resolve).catch(() => resolve(key));
26
+ });
47
27
  });
48
-
49
- return out;
50
- }
51
-
52
- /**
53
- * Load a single translated string.
54
- * Compatible with I18nProvider's AsyncStringResolver signature.
55
- */
56
- export async function getString(
57
- key: string,
58
- component = DEFAULT_COMPONENT,
59
- ): Promise<string> {
60
- const result = await getStrings([{key, component}]);
61
- return result[key] ?? key;
62
- }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-middag-ui",
3
- "version": "0.27.0",
3
+ "version": "0.29.0",
4
4
  "type": "module",
5
5
  "description": "Bootstrap a MIDDAG React UI layer in your Moodle or WordPress plugin",
6
6
  "scripts": {