@hua-labs/i18n-core 1.0.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/LICENSE +21 -0
- package/README.md +636 -0
- package/dist/components/MissingKeyOverlay.d.ts +33 -0
- package/dist/components/MissingKeyOverlay.d.ts.map +1 -0
- package/dist/components/MissingKeyOverlay.js +138 -0
- package/dist/components/MissingKeyOverlay.js.map +1 -0
- package/dist/core/debug-tools.d.ts +37 -0
- package/dist/core/debug-tools.d.ts.map +1 -0
- package/dist/core/debug-tools.js +241 -0
- package/dist/core/debug-tools.js.map +1 -0
- package/dist/core/i18n-resource.d.ts +59 -0
- package/dist/core/i18n-resource.d.ts.map +1 -0
- package/dist/core/i18n-resource.js +153 -0
- package/dist/core/i18n-resource.js.map +1 -0
- package/dist/core/lazy-loader.d.ts +82 -0
- package/dist/core/lazy-loader.d.ts.map +1 -0
- package/dist/core/lazy-loader.js +193 -0
- package/dist/core/lazy-loader.js.map +1 -0
- package/dist/core/translator-factory.d.ts +50 -0
- package/dist/core/translator-factory.d.ts.map +1 -0
- package/dist/core/translator-factory.js +117 -0
- package/dist/core/translator-factory.js.map +1 -0
- package/dist/core/translator.d.ts +202 -0
- package/dist/core/translator.d.ts.map +1 -0
- package/dist/core/translator.js +912 -0
- package/dist/core/translator.js.map +1 -0
- package/dist/hooks/useI18n.d.ts +39 -0
- package/dist/hooks/useI18n.d.ts.map +1 -0
- package/dist/hooks/useI18n.js +531 -0
- package/dist/hooks/useI18n.js.map +1 -0
- package/dist/hooks/useTranslation.d.ts +55 -0
- package/dist/hooks/useTranslation.d.ts.map +1 -0
- package/dist/hooks/useTranslation.js +58 -0
- package/dist/hooks/useTranslation.js.map +1 -0
- package/dist/index.d.ts +162 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +191 -0
- package/dist/index.js.map +1 -0
- package/dist/types/index.d.ts +162 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +191 -0
- package/dist/types/index.js.map +1 -0
- package/dist/utils/default-translations.d.ts +20 -0
- package/dist/utils/default-translations.d.ts.map +1 -0
- package/dist/utils/default-translations.js +123 -0
- package/dist/utils/default-translations.js.map +1 -0
- package/package.json +60 -0
- package/src/components/MissingKeyOverlay.tsx +223 -0
- package/src/core/debug-tools.ts +298 -0
- package/src/core/i18n-resource.ts +180 -0
- package/src/core/lazy-loader.ts +255 -0
- package/src/core/translator-factory.ts +137 -0
- package/src/core/translator.tsx +1194 -0
- package/src/hooks/useI18n.tsx +595 -0
- package/src/hooks/useTranslation.tsx +62 -0
- package/src/index.ts +298 -0
- package/src/types/index.ts +443 -0
- package/src/utils/default-translations.ts +129 -0
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
import { TranslationNamespace } from '../types';
|
|
2
|
+
import { i18nResourceManager } from './i18n-resource';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 네임스페이스별 지연 로딩 전략
|
|
6
|
+
* 대규모 앱에서 번역 단위 최적화를 위한 loadOnDemand 함수
|
|
7
|
+
*/
|
|
8
|
+
export class LazyLoader {
|
|
9
|
+
private static instance: LazyLoader;
|
|
10
|
+
private loadingQueue = new Map<string, Promise<TranslationNamespace>>();
|
|
11
|
+
private preloadCache = new Set<string>();
|
|
12
|
+
private loadHistory = new Map<string, number>();
|
|
13
|
+
|
|
14
|
+
private constructor() {}
|
|
15
|
+
|
|
16
|
+
static getInstance(): LazyLoader {
|
|
17
|
+
if (!LazyLoader.instance) {
|
|
18
|
+
LazyLoader.instance = new LazyLoader();
|
|
19
|
+
}
|
|
20
|
+
return LazyLoader.instance;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 필요할 때 네임스페이스 로딩
|
|
25
|
+
*/
|
|
26
|
+
async loadOnDemand(
|
|
27
|
+
language: string,
|
|
28
|
+
namespace: string,
|
|
29
|
+
loader: (lang: string, ns: string) => Promise<TranslationNamespace>
|
|
30
|
+
): Promise<TranslationNamespace> {
|
|
31
|
+
const cacheKey = `${language}:${namespace}`;
|
|
32
|
+
|
|
33
|
+
// 이미 로딩 중이면 기존 Promise 반환
|
|
34
|
+
if (this.loadingQueue.has(cacheKey)) {
|
|
35
|
+
return this.loadingQueue.get(cacheKey)!;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// 캐시에 있으면 반환
|
|
39
|
+
const cached = i18nResourceManager.getCachedTranslationsSync(language, namespace);
|
|
40
|
+
if (cached) {
|
|
41
|
+
this.updateLoadHistory(cacheKey);
|
|
42
|
+
return cached;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// 새로 로딩
|
|
46
|
+
const loadPromise = this.performLoad(language, namespace, loader);
|
|
47
|
+
this.loadingQueue.set(cacheKey, loadPromise);
|
|
48
|
+
|
|
49
|
+
try {
|
|
50
|
+
const result = await loadPromise;
|
|
51
|
+
this.updateLoadHistory(cacheKey);
|
|
52
|
+
return result;
|
|
53
|
+
} finally {
|
|
54
|
+
this.loadingQueue.delete(cacheKey);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* 실제 로딩 수행
|
|
60
|
+
*/
|
|
61
|
+
private async performLoad(
|
|
62
|
+
language: string,
|
|
63
|
+
namespace: string,
|
|
64
|
+
loader: (lang: string, ns: string) => Promise<TranslationNamespace>
|
|
65
|
+
): Promise<TranslationNamespace> {
|
|
66
|
+
const result = await i18nResourceManager.getCachedTranslations(language, namespace, loader);
|
|
67
|
+
return result;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* 로딩 히스토리 업데이트
|
|
72
|
+
*/
|
|
73
|
+
private updateLoadHistory(cacheKey: string): void {
|
|
74
|
+
this.loadHistory.set(cacheKey, Date.now());
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* 네임스페이스 사전 로딩
|
|
79
|
+
*/
|
|
80
|
+
async preloadNamespace(
|
|
81
|
+
language: string,
|
|
82
|
+
namespace: string,
|
|
83
|
+
loader: (lang: string, ns: string) => Promise<TranslationNamespace>
|
|
84
|
+
): Promise<void> {
|
|
85
|
+
const cacheKey = `${language}:${namespace}`;
|
|
86
|
+
|
|
87
|
+
if (this.preloadCache.has(cacheKey)) {
|
|
88
|
+
return; // 이미 사전 로딩됨
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
try {
|
|
92
|
+
await this.loadOnDemand(language, namespace, loader);
|
|
93
|
+
this.preloadCache.add(cacheKey);
|
|
94
|
+
} catch (error) {
|
|
95
|
+
// Silent fail for preloading
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* 여러 네임스페이스 동시 사전 로딩
|
|
101
|
+
*/
|
|
102
|
+
async preloadMultipleNamespaces(
|
|
103
|
+
language: string,
|
|
104
|
+
namespaces: string[],
|
|
105
|
+
loader: (lang: string, ns: string) => Promise<TranslationNamespace>
|
|
106
|
+
): Promise<void> {
|
|
107
|
+
const promises = namespaces.map(namespace =>
|
|
108
|
+
this.preloadNamespace(language, namespace, loader)
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
await Promise.allSettled(promises);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* 사용 패턴 기반 자동 사전 로딩
|
|
116
|
+
*/
|
|
117
|
+
async autoPreload(
|
|
118
|
+
language: string,
|
|
119
|
+
currentNamespace: string,
|
|
120
|
+
loader: (lang: string, ns: string) => Promise<TranslationNamespace>
|
|
121
|
+
): Promise<void> {
|
|
122
|
+
// 현재 네임스페이스와 관련된 네임스페이스들 자동 사전 로딩
|
|
123
|
+
const relatedNamespaces = this.getRelatedNamespaces(currentNamespace);
|
|
124
|
+
|
|
125
|
+
for (const namespace of relatedNamespaces) {
|
|
126
|
+
await this.preloadNamespace(language, namespace, loader);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* 관련 네임스페이스 추정
|
|
132
|
+
*/
|
|
133
|
+
private getRelatedNamespaces(currentNamespace: string): string[] {
|
|
134
|
+
// 실제 구현에서는 사용 패턴 분석을 통해 관련 네임스페이스 추정
|
|
135
|
+
const namespacePatterns: Record<string, string[]> = {
|
|
136
|
+
'auth': ['common', 'validation', 'errors'],
|
|
137
|
+
'dashboard': ['common', 'navigation', 'stats'],
|
|
138
|
+
'profile': ['common', 'auth', 'validation'],
|
|
139
|
+
'settings': ['common', 'navigation', 'forms'],
|
|
140
|
+
'common': ['errors', 'validation']
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
return namespacePatterns[currentNamespace] || [];
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* 로딩 우선순위 설정
|
|
148
|
+
*/
|
|
149
|
+
setLoadPriority(namespaces: string[]): void {
|
|
150
|
+
// 우선순위가 높은 네임스페이스들을 먼저 로딩
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* 로딩 통계 가져오기
|
|
155
|
+
*/
|
|
156
|
+
getLoadStats() {
|
|
157
|
+
return {
|
|
158
|
+
loadingQueueSize: this.loadingQueue.size,
|
|
159
|
+
preloadedCount: this.preloadCache.size,
|
|
160
|
+
loadHistorySize: this.loadHistory.size
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* 메모리 최적화
|
|
166
|
+
*/
|
|
167
|
+
optimizeMemory(): void {
|
|
168
|
+
// 오래된 로딩 히스토리 정리
|
|
169
|
+
const now = Date.now();
|
|
170
|
+
const oneHour = 60 * 60 * 1000;
|
|
171
|
+
|
|
172
|
+
for (const [key, timestamp] of this.loadHistory.entries()) {
|
|
173
|
+
if (now - timestamp > oneHour) {
|
|
174
|
+
this.loadHistory.delete(key);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* 네임스페이스 사용 빈도 분석
|
|
181
|
+
*/
|
|
182
|
+
analyzeUsagePatterns(): Record<string, number> {
|
|
183
|
+
const usage: Record<string, number> = {};
|
|
184
|
+
|
|
185
|
+
for (const [key, timestamp] of this.loadHistory.entries()) {
|
|
186
|
+
const namespace = key.split(':')[1];
|
|
187
|
+
usage[namespace] = (usage[namespace] || 0) + 1;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
return usage;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* 캐시 무효화
|
|
195
|
+
*/
|
|
196
|
+
invalidateCache(language?: string, namespace?: string): void {
|
|
197
|
+
i18nResourceManager.invalidateCache(language, namespace);
|
|
198
|
+
|
|
199
|
+
if (language && namespace) {
|
|
200
|
+
const cacheKey = `${language}:${namespace}`;
|
|
201
|
+
this.preloadCache.delete(cacheKey);
|
|
202
|
+
this.loadHistory.delete(cacheKey);
|
|
203
|
+
} else if (language) {
|
|
204
|
+
// 특정 언어의 모든 캐시 무효화
|
|
205
|
+
for (const key of this.preloadCache) {
|
|
206
|
+
if (key.startsWith(`${language}:`)) {
|
|
207
|
+
this.preloadCache.delete(key);
|
|
208
|
+
this.loadHistory.delete(key);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
} else {
|
|
212
|
+
// 전체 캐시 무효화
|
|
213
|
+
this.preloadCache.clear();
|
|
214
|
+
this.loadHistory.clear();
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* 전역 지연 로더 인스턴스
|
|
221
|
+
*/
|
|
222
|
+
export const lazyLoader = LazyLoader.getInstance();
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* 편의 함수: 필요할 때 로딩
|
|
226
|
+
*/
|
|
227
|
+
export const loadOnDemand = (
|
|
228
|
+
language: string,
|
|
229
|
+
namespace: string,
|
|
230
|
+
loader: (lang: string, ns: string) => Promise<TranslationNamespace>
|
|
231
|
+
) => {
|
|
232
|
+
return lazyLoader.loadOnDemand(language, namespace, loader);
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* 편의 함수: 사전 로딩
|
|
237
|
+
*/
|
|
238
|
+
export const preloadNamespace = (
|
|
239
|
+
language: string,
|
|
240
|
+
namespace: string,
|
|
241
|
+
loader: (lang: string, ns: string) => Promise<TranslationNamespace>
|
|
242
|
+
) => {
|
|
243
|
+
return lazyLoader.preloadNamespace(language, namespace, loader);
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* 편의 함수: 자동 사전 로딩
|
|
248
|
+
*/
|
|
249
|
+
export const autoPreload = (
|
|
250
|
+
language: string,
|
|
251
|
+
currentNamespace: string,
|
|
252
|
+
loader: (lang: string, ns: string) => Promise<TranslationNamespace>
|
|
253
|
+
) => {
|
|
254
|
+
return lazyLoader.autoPreload(language, currentNamespace, loader);
|
|
255
|
+
};
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { Translator } from './translator';
|
|
2
|
+
import { I18nConfig } from '../types';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Translator 인스턴스를 관리하는 Factory 클래스
|
|
6
|
+
* - Config 변경 감지
|
|
7
|
+
* - 테스트 환경 격리
|
|
8
|
+
* - 메모리 관리
|
|
9
|
+
*/
|
|
10
|
+
export class TranslatorFactory {
|
|
11
|
+
private static instances = new Map<string, Translator>();
|
|
12
|
+
private static configCache = new Map<string, I18nConfig>();
|
|
13
|
+
private static readonly MAX_INSTANCES = 10; // 최대 인스턴스 수 제한
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Config를 기반으로 고유 키 생성
|
|
17
|
+
*/
|
|
18
|
+
private static generateConfigKey(config: I18nConfig): string {
|
|
19
|
+
// Config의 핵심 속성들을 기반으로 키 생성
|
|
20
|
+
const keyParts = [
|
|
21
|
+
config.defaultLanguage,
|
|
22
|
+
config.fallbackLanguage || 'en',
|
|
23
|
+
config.namespaces?.join(',') || 'common',
|
|
24
|
+
config.debug ? 'debug' : 'prod',
|
|
25
|
+
];
|
|
26
|
+
return keyParts.join('|');
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Config가 변경되었는지 확인
|
|
31
|
+
*/
|
|
32
|
+
private static isConfigChanged(configKey: string, newConfig: I18nConfig): boolean {
|
|
33
|
+
const cachedConfig = this.configCache.get(configKey);
|
|
34
|
+
if (!cachedConfig) return true;
|
|
35
|
+
|
|
36
|
+
// 핵심 속성들만 비교
|
|
37
|
+
return (
|
|
38
|
+
cachedConfig.defaultLanguage !== newConfig.defaultLanguage ||
|
|
39
|
+
cachedConfig.fallbackLanguage !== newConfig.fallbackLanguage ||
|
|
40
|
+
JSON.stringify(cachedConfig.namespaces) !== JSON.stringify(newConfig.namespaces) ||
|
|
41
|
+
cachedConfig.debug !== newConfig.debug
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Translator 인스턴스 생성 또는 반환
|
|
47
|
+
*/
|
|
48
|
+
static create(config: I18nConfig): Translator {
|
|
49
|
+
const configKey = this.generateConfigKey(config);
|
|
50
|
+
|
|
51
|
+
// Config가 변경되었거나 인스턴스가 없으면 새로 생성
|
|
52
|
+
if (!this.instances.has(configKey) || this.isConfigChanged(configKey, config)) {
|
|
53
|
+
// 최대 인스턴스 수 초과 시 오래된 인스턴스 제거 (LRU 방식)
|
|
54
|
+
if (this.instances.size >= this.MAX_INSTANCES && !this.instances.has(configKey)) {
|
|
55
|
+
// 가장 오래된 인스턴스 제거 (Map은 삽입 순서 유지)
|
|
56
|
+
const oldestKey = this.instances.keys().next().value;
|
|
57
|
+
if (oldestKey) {
|
|
58
|
+
const oldInstance = this.instances.get(oldestKey);
|
|
59
|
+
if (oldInstance) {
|
|
60
|
+
oldInstance.clearCache();
|
|
61
|
+
}
|
|
62
|
+
this.instances.delete(oldestKey);
|
|
63
|
+
this.configCache.delete(oldestKey);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// 기존 인스턴스 정리
|
|
68
|
+
if (this.instances.has(configKey)) {
|
|
69
|
+
const oldInstance = this.instances.get(configKey)!;
|
|
70
|
+
oldInstance.clearCache();
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// 새 인스턴스 생성
|
|
74
|
+
const newInstance = new Translator(config);
|
|
75
|
+
this.instances.set(configKey, newInstance);
|
|
76
|
+
this.configCache.set(configKey, { ...config });
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return this.instances.get(configKey)!;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* 특정 Config 키의 Translator 인스턴스 반환
|
|
84
|
+
*/
|
|
85
|
+
static get(config: I18nConfig): Translator | null {
|
|
86
|
+
const configKey = this.generateConfigKey(config);
|
|
87
|
+
return this.instances.get(configKey) || null;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* 모든 Translator 인스턴스 정리 (테스트용)
|
|
92
|
+
*/
|
|
93
|
+
static clear(): void {
|
|
94
|
+
// 모든 인스턴스의 캐시 정리
|
|
95
|
+
for (const instance of this.instances.values()) {
|
|
96
|
+
instance.clearCache();
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
this.instances.clear();
|
|
100
|
+
this.configCache.clear();
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* 특정 Config 키의 인스턴스만 정리
|
|
105
|
+
*/
|
|
106
|
+
static clearConfig(config: I18nConfig): void {
|
|
107
|
+
const configKey = this.generateConfigKey(config);
|
|
108
|
+
const instance = this.instances.get(configKey);
|
|
109
|
+
if (instance) {
|
|
110
|
+
instance.clearCache();
|
|
111
|
+
this.instances.delete(configKey);
|
|
112
|
+
this.configCache.delete(configKey);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* 현재 관리 중인 인스턴스 수 반환
|
|
118
|
+
*/
|
|
119
|
+
static getInstanceCount(): number {
|
|
120
|
+
return this.instances.size;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* 디버깅용: 모든 인스턴스 정보 반환
|
|
125
|
+
*/
|
|
126
|
+
static debug(): {
|
|
127
|
+
instanceCount: number;
|
|
128
|
+
configKeys: string[];
|
|
129
|
+
instances: Map<string, Translator>;
|
|
130
|
+
} {
|
|
131
|
+
return {
|
|
132
|
+
instanceCount: this.instances.size,
|
|
133
|
+
configKeys: Array.from(this.instances.keys()),
|
|
134
|
+
instances: this.instances,
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
}
|