@tagadapay/plugin-sdk 2.3.6 → 2.3.8

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.
@@ -179,6 +179,10 @@ export function useCheckout(options = {}) {
179
179
  }
180
180
  }, [apiService, currentCurrency, isSessionInitialized]);
181
181
  const refresh = useCallback(async () => {
182
+ console.log('🔄 [useCheckout] Refreshing checkout data...', {
183
+ checkoutToken: currentCheckoutTokenRef.current?.substring(0, 8) + '...',
184
+ timestamp: new Date().toISOString(),
185
+ });
182
186
  if (!currentCheckoutTokenRef.current) {
183
187
  throw new Error('No checkout session to refresh');
184
188
  }
@@ -0,0 +1,18 @@
1
+ type Primitive = string | number | boolean | null | undefined;
2
+ export interface TranslateOptions {
3
+ defaultValue?: string;
4
+ }
5
+ export interface UseTranslationsResult {
6
+ t: (key: string, vars?: Record<string, Primitive>, options?: TranslateOptions) => string;
7
+ messages: Record<string, string>;
8
+ locale: string;
9
+ language: string;
10
+ region: string;
11
+ }
12
+ /**
13
+ * useTranslations - Lightweight translator for SDK plugins
14
+ * - Reads `locale.messages` populated by TagadaProvider session init
15
+ * - Provides `t(key, vars, options)` with simple `{var}` interpolation
16
+ */
17
+ export declare function useTranslations(targetLanguageCode?: string): UseTranslationsResult;
18
+ export {};
@@ -0,0 +1,77 @@
1
+ 'use client';
2
+ import { useEffect, useMemo, useState } from 'react';
3
+ import { useTagadaContext } from '../providers/TagadaProvider';
4
+ function interpolate(template, vars) {
5
+ if (!vars)
6
+ return template;
7
+ return template.replace(/\{(\w+)\}/g, (_, name) => {
8
+ const value = vars[name];
9
+ return value === null || value === undefined ? '' : String(value);
10
+ });
11
+ }
12
+ /**
13
+ * useTranslations - Lightweight translator for SDK plugins
14
+ * - Reads `locale.messages` populated by TagadaProvider session init
15
+ * - Provides `t(key, vars, options)` with simple `{var}` interpolation
16
+ */
17
+ export function useTranslations(targetLanguageCode) {
18
+ const { locale, apiService, pluginConfig } = useTagadaContext();
19
+ const selectedLanguage = targetLanguageCode || locale.language;
20
+ // Prefer backend messages for the selected language; fall back to provider
21
+ const [fetchedMessages, setFetchedMessages] = useState(null);
22
+ useEffect(() => {
23
+ let cancelled = false;
24
+ async function fetchMessages() {
25
+ const region = locale.region || 'US';
26
+ const desiredLanguage = selectedLanguage;
27
+ const targetLocale = `${desiredLanguage}-${region}`;
28
+ try {
29
+ const storeId = pluginConfig?.storeId;
30
+ if (!storeId) {
31
+ setFetchedMessages(null);
32
+ return;
33
+ }
34
+ const data = await apiService.fetch('/api/v1/translation-messages', {
35
+ method: 'GET',
36
+ params: { locale: targetLocale, storeId },
37
+ });
38
+ if (!cancelled) {
39
+ setFetchedMessages(data || {});
40
+ }
41
+ }
42
+ catch (_err) {
43
+ if (!cancelled) {
44
+ setFetchedMessages(null);
45
+ }
46
+ }
47
+ }
48
+ void fetchMessages();
49
+ return () => {
50
+ cancelled = true;
51
+ };
52
+ }, [selectedLanguage, locale.region, pluginConfig?.storeId, apiService]);
53
+ const messages = useMemo(() => {
54
+ if (fetchedMessages)
55
+ return fetchedMessages;
56
+ return locale.messages || {};
57
+ }, [fetchedMessages, locale.messages]);
58
+ const t = useMemo(() => {
59
+ return (key, vars, opts) => {
60
+ const raw = messages[key] ?? opts?.defaultValue ?? key;
61
+ return interpolate(raw, vars);
62
+ };
63
+ }, [messages]);
64
+ const computedLocale = useMemo(() => {
65
+ if (selectedLanguage === locale.language)
66
+ return locale.locale;
67
+ const region = locale.region || 'US';
68
+ return `${selectedLanguage}-${region}`;
69
+ }, [selectedLanguage, locale.locale, locale.language, locale.region]);
70
+ return {
71
+ t,
72
+ messages,
73
+ locale: computedLocale,
74
+ language: selectedLanguage,
75
+ region: computedLocale.split('-')[1] || 'US',
76
+ };
77
+ }
@@ -16,6 +16,7 @@ export { useOrderBump } from './hooks/useOrderBump';
16
16
  export { usePostPurchases } from './hooks/usePostPurchases';
17
17
  export { useProducts } from './hooks/useProducts';
18
18
  export { useSession } from './hooks/useSession';
19
+ export { useTranslations } from './hooks/useTranslations';
19
20
  export { useTagadaContext } from './providers/TagadaProvider';
20
21
  export { clearPluginConfigCache, debugPluginConfig, getPluginConfig, useBasePath, usePluginConfig } from './hooks/usePluginConfig';
21
22
  export type { PluginConfig } from './hooks/usePluginConfig';
@@ -19,6 +19,7 @@ export { useOrderBump } from './hooks/useOrderBump';
19
19
  export { usePostPurchases } from './hooks/usePostPurchases';
20
20
  export { useProducts } from './hooks/useProducts';
21
21
  export { useSession } from './hooks/useSession';
22
+ export { useTranslations } from './hooks/useTranslations';
22
23
  export { useTagadaContext } from './providers/TagadaProvider';
23
24
  // Plugin configuration hooks
24
25
  export { clearPluginConfigCache, debugPluginConfig, getPluginConfig, useBasePath, usePluginConfig } from './hooks/usePluginConfig';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tagadapay/plugin-sdk",
3
- "version": "2.3.6",
3
+ "version": "2.3.8",
4
4
  "description": "Modern React SDK for building Tagada Pay plugins",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",