All files / islands / GlobalLanguageProvider.tsx

100.00% Branches 0/0
4.51% Lines 6/133
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
 
 
 
 
 
x2
 
x2
x2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x2
 
 
x2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x2
















































































































































































































































/**
 * 全局语言提供者 Island 组件
 * 管理全局语言状态,实现无刷新语言切换
 */

import { DEFAULT_LANGUAGE, isSupportedLanguage } from "@i18n/config.ts";
import type { SupportedLanguage, TranslationResource } from "@i18n/types.ts";
import { ComponentChildren, createContext } from "preact";
import { useContext, useEffect, useState } from "preact/hooks";

// 语言上下文类型
interface LanguageContextType {
  currentLanguage: SupportedLanguage;
  changeLanguage: (language: SupportedLanguage) => void;
  translations: Record<string, TranslationResource>;
  isLoading: boolean;
  t: (
    key: string,
    params?: Record<string, string | number>,
    namespace?: string,
  ) => string;
}

// 创建语言上下文
const LanguageContext = createContext<LanguageContextType | null>(null);

// 翻译缓存
const translationCache = new Map<string, TranslationResource>();

/**
 * Retrieves a localized string for the given key from the specified namespace, performing parameter interpolation if needed.
 *
 * If the key or namespace is missing, or the resolved value is not a string, returns the key itself.
 *
 * @param key - The translation key, supporting dot notation for nested lookups
 * @param translations - The loaded translation resources, organized by namespace
 * @param params - Optional parameters for interpolation within the translation string
 * @param namespace - The namespace to search within, defaults to "common"
 * @returns The localized string with parameters interpolated, or the key if not found
 */
function simpleTranslate(
  key: string,
  translations: Record<string, TranslationResource>,
  params?: Record<string, string | number>,
  namespace = "common",
): string {
  const namespaceKey = `${namespace}`;
  const resource = translations[namespaceKey];

  if (!resource) {
    return key;
  }

  const keys = key.split(".");
  let value: unknown = resource;

  for (const k of keys) {
    if (value && typeof value === "object" && k in value) {
      value = (value as Record<string, unknown>)[k];
    } else {
      return key;
    }
  }

  if (typeof value !== "string") {
    return key;
  }

  // 简单的参数替换
  if (params) {
    return value.replace(/\{\{(\w+)\}\}/g, (match, paramKey) => {
      return params[paramKey] !== undefined ? String(params[paramKey]) : match;
    });
  }

  return value;
}

/**
 * Loads translation resources for a given language and namespace.
 *
 * Fetches the translation JSON file from the server, caches the result to avoid redundant requests, and returns the translation object. Returns an empty object if loading fails.
 *
 * @param language - The language code for which to load translations
 * @param namespace - The namespace of the translation resource
 * @returns The loaded translation resource object, or an empty object if loading fails
 */
async function loadTranslation(
  language: SupportedLanguage,
  namespace: string,
): Promise<TranslationResource> {
  const cacheKey = `${language}_${namespace}`;

  if (translationCache.has(cacheKey)) {
    return translationCache.get(cacheKey)!;
  }

  try {
    const response = await fetch(`/i18n/locales/${language}/${namespace}.json`);
    if (!response.ok) {
      throw new Error(`Failed to load translations: ${response.status}`);
    }

    const data = await response.json();
    translationCache.set(cacheKey, data);
    return data;
  } catch (err) {
    console.error(
      `Failed to load translations for ${language}/${namespace}:`,
      err,
    );
    return {};
  }
}

/**
 * Provides global language state and translation utilities to the application.
 *
 * Initializes and manages the current language, loads translation resources for fixed namespaces, and exposes a translation function and language switching capability to all descendant components via context.
 *
 * @param children - The components that will have access to the language context.
 */
export function GlobalLanguageProvider(
  { children }: { children: ComponentChildren },
) {
  const [currentLanguage, setCurrentLanguage] = useState<SupportedLanguage>(
    DEFAULT_LANGUAGE,
  );
  const [translations, setTranslations] = useState<
    Record<string, TranslationResource>
  >({});
  const [isLoading, setIsLoading] = useState(false);

  // 初始化语言
  useEffect(() => {
    if (typeof window !== "undefined") {
      const stored = localStorage.getItem("athena_language");
      if (stored && isSupportedLanguage(stored)) {
        setCurrentLanguage(stored);
      }
    }
  }, []);

  // 加载翻译资源
  useEffect(() => {
    const loadTranslations = async () => {
      setIsLoading(true);

      try {
        // 加载常用的命名空间
        const namespaces = ["common", "components"];
        const newTranslations: Record<string, TranslationResource> = {};

        for (const namespace of namespaces) {
          const resource = await loadTranslation(currentLanguage, namespace);
          newTranslations[namespace] = resource;
        }

        setTranslations(newTranslations);
      } catch (error) {
        console.error("Failed to load translations:", error);
      } finally {
        setIsLoading(false);
      }
    };

    loadTranslations();
  }, [currentLanguage]);

  // 切换语言
  const changeLanguage = (language: SupportedLanguage) => {
    if (language === currentLanguage) return;

    setCurrentLanguage(language);

    if (typeof window !== "undefined") {
      localStorage.setItem("athena_language", language);
    }
  };

  // 翻译函数
  const t = (
    key: string,
    params?: Record<string, string | number>,
    namespace = "common",
  ) => {
    return simpleTranslate(key, translations, params, namespace);
  };

  const contextValue: LanguageContextType = {
    currentLanguage,
    changeLanguage,
    translations,
    isLoading,
    t,
  };

  return (
    <LanguageContext.Provider value={contextValue}>
      {children}
    </LanguageContext.Provider>
  );
}

/**
 * Provides access to the global language context.
 *
 * Throws an error if used outside of a GlobalLanguageProvider.
 * @returns The current language context, including language state, translation resources, and utilities.
 */
export function useGlobalLanguage(): LanguageContextType {
  const context = useContext(LanguageContext);
  if (!context) {
    throw new Error(
      "useGlobalLanguage must be used within a GlobalLanguageProvider",
    );
  }
  return context;
}

/**
 * Provides translation utilities scoped to a specific namespace, along with global language state.
 *
 * Returns an object containing a translation function for the given namespace, the current language, a method to change the language, and a loading status flag.
 *
 * @param namespace - The translation namespace to use. Defaults to "common".
 * @returns An object with a namespaced translation function, current language, language change method, and loading status.
 */
export function useGlobalTranslation(namespace = "common") {
  const { t, currentLanguage, changeLanguage, isLoading } = useGlobalLanguage();

  return {
    t: (key: string, params?: Record<string, string | number>) =>
      t(key, params, namespace),
    currentLanguage,
    changeLanguage,
    isLoading,
  };
}

// 默认导出
export default GlobalLanguageProvider;