-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranslationLoader.ts
More file actions
162 lines (138 loc) · 4.43 KB
/
translationLoader.ts
File metadata and controls
162 lines (138 loc) · 4.43 KB
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
import i18n from 'i18next';
import { exists, readTextFile } from '@tauri-apps/api/fs';
import { join } from '@tauri-apps/api/path';
interface TranslationFile {
locale: string;
name: string;
translations: Record<string, string>;
}
interface CommandDefinition {
id: string;
properties?: Array<{
key: string;
type: string;
enum?: string[];
}>;
}
// Development fallback for when Tauri is not available
const loadDevelopmentTranslations = async (): Promise<void> => {
try {
// Try to load sample translation files in development
const languages = [
{ file: 'english', code: 'en' },
{ file: 'japanese', code: 'ja' },
];
for (const { file, code } of languages) {
try {
const response = await fetch(`/src/sample/i18n/${file}.json`);
if (response.ok) {
const data: TranslationFile = await response.json();
i18n.addResourceBundle(code, 'translation', data.translations, true, true);
}
} catch (error) {
console.warn(`Failed to load ${file} translations in dev mode:`, error);
}
}
} catch (error) {
console.error('Failed to load development translations:', error);
}
};
// Load translations from i18n folder
export async function loadTranslations(): Promise<void> {
// Check if we're in Tauri environment
if (!window.__TAURI__) {
await loadDevelopmentTranslations();
return;
}
try {
// Get the project path from the store
const { useSkitStore } = await import('../store/skitStore');
const projectPath = useSkitStore.getState().projectPath;
if (!projectPath) {
console.warn('No project path set, using development translations');
await loadDevelopmentTranslations();
return;
}
const i18nPath = await join(projectPath, 'i18n');
// Check if i18n directory exists
const i18nDirExists = await exists(i18nPath);
if (!i18nDirExists) {
console.warn('i18n directory not found, using development translations');
await loadDevelopmentTranslations();
return;
}
// Load all available language files
const languages = ['english', 'japanese', 'chinese', 'spanish'];
for (const lang of languages) {
const filePath = await join(i18nPath, `${lang}.json`);
if (await exists(filePath)) {
try {
const content = await readTextFile(filePath);
const translationFile: TranslationFile = JSON.parse(content);
// Add translations to i18n
i18n.addResourceBundle(
translationFile.locale,
'translation',
translationFile.translations,
true,
true
);
} catch (error) {
console.error(`Failed to load ${lang} translations:`, error);
}
}
}
} catch (error) {
console.error('Failed to load translations in Tauri mode:', error);
await loadDevelopmentTranslations();
}
}
// Generate translation keys for a command
export function generateCommandTranslationKeys(command: CommandDefinition): string[] {
const keys: string[] = [
`command.${command.id}.name`,
`command.${command.id}.description`,
];
if (command.properties) {
command.properties.forEach((property) => {
keys.push(
`command.${command.id}.property.${property.key}.name`,
`command.${command.id}.property.${property.key}.description`,
`command.${command.id}.property.${property.key}.placeholder`
);
if (property.enum) {
property.enum.forEach((enumValue) => {
keys.push(
`command.${command.id}.property.${property.key}.enum.${enumValue}`
);
});
}
});
}
return keys;
}
// Get available languages
export async function getAvailableLanguages(): Promise<Array<{ code: string; name: string }>> {
const languages = Object.keys(i18n.services.resourceStore.data);
return languages.map((lang) => ({
code: lang,
name: i18n.t(`language.${lang}`, { lng: lang, defaultValue: lang }),
}));
}
// Helper to get translation with fallback
export function getTranslationWithFallback(
key: string,
fallback?: string,
options?: any
): string {
const translation = i18n.t(key, options);
// Ensure we return a string
if (typeof translation !== 'string') {
return fallback || key;
}
// If translation is the same as key, use fallback
if (translation === key && fallback) {
return fallback;
}
return translation;
}