* don't display a share connection error when listing share
[open-ps2-loader.git] / src / lang.c
blob8a2976e5fb4efeec97247665ac0e834d724ac40c
1 #include "include/usbld.h"
2 #include "include/lang.h"
3 #include "include/util.h"
4 #include "include/fntsys.h"
5 #include "include/ioman.h"
7 // Language support
8 static char *internalEnglish[LANG_STR_COUNT] = {
9 "English (internal)",
10 " WELCOME TO OPEN PS2 LOADER. MAIN CODE BASED ON SOURCE CODE OF HD PROJECT <http://psx-scene.com> ADAPTATION TO USB ADVANCE FORMAT AND INITIAL GUI BY IFCARO <http://ps2dev.ifcaro.net> MOST OF LOADER CORE IS MADE BY JIMMIKAELKAEL. ALL THE GUI IMPROVEMENTS ARE MADE BY VOLCA. THANKS FOR USING OUR PROGRAM ^^",
11 "Open PS2 Loader %s",
12 "Save changes",
13 "Back",
14 "Network config",
15 "<no values>",
16 "Settings saved...",
17 "Error writing settings!",
18 "Exit",
19 "Settings",
20 "USB Games",
21 "HDD Games",
22 "Network Games",
23 "Apps",
24 "Theme",
25 "Language",
26 "Network startup error: %d",
27 "On",
28 "Off",
29 "Ok",
30 "Game settings",
31 "Remove all settings",
32 "Removed all keys for the game",
33 "Scrolling",
34 "Slow",
35 "Medium",
36 "Fast",
37 "Default menu",
38 "Load from disc",
39 "Please wait",
40 "Error while loading the Game ID",
41 "Automatic sorting",
42 "Error loading the language file",
43 "Disable Debug Colors",
44 "No controller detected, waiting...",
45 "Enable Cover Art",
46 "Wide screen",
47 "Power off",
48 "Loading config",
49 "Saving config",
50 "Start device",
51 "USB device start mode",
52 "HDD device start mode",
53 "ETH device start mode",
54 "Applications start mode",
55 "Auto",
56 "Manual",
57 "Start HDL Server",
58 "HDL Server Starting...",
59 "HDL Server Running... Press [O] to stop",
60 "Press [X] to terminate HDL server",
61 "HDL Server Unloading...",
62 "IGR path",
63 "Background color",
64 "Text color",
65 "IP",
66 "Mask",
67 "Gateway",
68 "Port",
69 "Share",
70 "User",
71 "Password",
72 "<not set>",
73 "Accept",
74 "Item will be permanently deleted, continue ?",
75 "Rename",
76 "Delete",
77 "Run",
78 "Display settings",
79 "Enable Delete and Rename actions",
80 "Check USB game fragmentation",
81 "Remember last played game",
82 "Error, the game is fragmented",
83 "Error, could not run the item",
84 "Test",
85 "Leave empty for GUEST auth.",
86 "Load alternate core",
87 "Alternative data read method",
88 "Unhook Syscalls",
89 "0 PSS mode",
90 "Disable DVD-DL",
91 "Disable IGR",
92 "Reduced cdvdfsv buffer",
93 "Hide dev9 module",
94 "Alternate IGR combo",
95 "Changing the size will reformat the VMC",
96 "Create",
97 "Modify",
98 "Abort",
99 "Reset",
100 "Use generic",
101 "Configure VMC",
102 "Name",
103 "Size",
104 "Status",
105 "Progress",
106 "VMC file exist",
107 "Invalid VMC file, size is incorrect",
108 "VMC file need to be created",
109 "Error with VMC %s, continue with physical MC (slot %d) ?",
110 "Automatic refresh",
111 "About",
112 "Coders",
113 "USB delay",
114 "USB prefix path",
115 "Leave empty to exit to Browser",
116 "Value in minute(s), 0 to disable spin down",
117 "Automatic HDD spin down",
118 "Video mode",
119 "Dialog color",
120 "Selected color",
121 "Display info page",
122 "Info",
123 "Custom ELF",
124 "Color selection",
125 "Reconnect",
126 "Leave empty to list shares",
127 "ETH prefix path"
130 static int guiLangID = 0;
131 static char **lang_strs = internalEnglish;
132 static int nValidEntries = LANG_STR_COUNT;
134 static int nLanguages = 0;
135 static language_t languages[MAX_LANGUAGE_FILES];
136 static char **guiLangNames;
138 // localised string getter
139 char *_l(unsigned int id) {
140 return lang_strs[id];
143 static void lngFreeFromFile() {
144 if (guiLangID == 0)
145 return;
147 int strId = 0;
148 for(; strId < nValidEntries; strId++) {
149 free(lang_strs[strId]);
150 lang_strs[strId] = NULL;
152 free(lang_strs);
153 lang_strs = NULL;
156 static int lngLoadFromFile(char* path, char *name) {
157 file_buffer_t* fileBuffer = openFileBuffer(path, O_RDONLY, 1, 1024);
158 if (fileBuffer) {
159 // file exists, try to read it and load the custom lang
160 lang_strs = (char**) malloc(LANG_STR_COUNT * sizeof(char**));
162 int strId = 0;
163 while (strId < LANG_STR_COUNT && readFileBuffer(fileBuffer, &lang_strs[strId])) {
164 strId++;
166 closeFileBuffer(fileBuffer);
168 LOG("LANG: #### Loaded %d entries\n", strId);
170 // remember how many entries were read from the file (for the free later)
171 nValidEntries = strId;
173 // if necessary complete lang with default internal
174 while (strId < LANG_STR_COUNT) {
175 LOG("LANG: #### Default entry added: %s\n", internalEnglish[strId]);
176 lang_strs[strId] = internalEnglish[strId];
177 strId++;
180 int size = -1;
181 char path[255];
182 snprintf(path, 255, "%s/font_%s.ttf", gBaseMCDir, name);
184 LOG("#### Custom font path: %s\n", path);
186 void* customFont = readFile(path, -1, &size);
187 if (customFont)
188 fntReplace(FNT_DEFAULT, customFont, size, 1, 1); // consider fonts loaded through language as default, so they won't be reset-ed when changing themes
189 else
190 fntSetDefault(FNT_DEFAULT);
192 return 1;
194 return 0;
197 char* lngGetValue() {
198 return guiLangNames[guiLangID];
201 static int lngReadEntry(int index, char* path, char* separator, char* name, unsigned int mode) {
202 if (!FIO_SO_ISDIR(mode)) {
203 if(strstr(name, ".lng") || strstr(name, ".LNG")) {
205 language_t* currLang = &languages[index];
207 // filepath for this language file
208 int length = strlen(path) + 1 + strlen(name) + 1;
209 currLang->filePath = (char*) malloc(length * sizeof(char));
210 sprintf(currLang->filePath, "%s%s%s", path, separator, name);
212 // extract name for this language (will be used for the English translation)
213 length = strlen(name) - 5 - 4 + 1;
214 currLang->name = (char*) malloc(length * sizeof(char));
215 memcpy(currLang->name, name + 5, length);
216 currLang->name[length - 1] = '\0';
218 /*file_buffer_t* fileBuffer = openFileBuffer(currLang->filePath, 1, 1024);
219 if (fileBuffer) {
220 // read localized name of language from file
221 if (readLineContext(fileBuffer, &currLang->name))
222 readLineContext(fileBuffer, &currLang->fontName);
223 closeFileBuffer(fileBuffer);
226 index++;
229 return index;
232 void lngInit() {
233 fntInit();
235 nLanguages = listDir(gBaseMCDir, "/", MAX_LANGUAGE_FILES, &lngReadEntry);
237 // build the languages name list
238 guiLangNames = (char**) malloc((nLanguages + 2) * sizeof(char**));
240 // add default internal (english)
241 guiLangNames[0] = internalEnglish[0];
243 int i = 0;
244 for (; i < nLanguages; i++) {
245 guiLangNames[i + 1] = languages[i].name;
248 guiLangNames[nLanguages + 1] = NULL;
251 void lngEnd() {
252 lngFreeFromFile();
254 int i = 0;
255 for (; i < nLanguages; i++) {
256 free(languages[i].name);
257 free(languages[i].filePath);
260 free(guiLangNames);
262 fntEnd();
265 void lngSetGuiValue(int langID) {
266 if (guiLangID != langID) {
268 lngFreeFromFile();
270 if (langID != 0) {
271 language_t* currLang = &languages[langID - 1];
272 if (lngLoadFromFile(currLang->filePath, currLang->name)) {
273 guiLangID = langID;
274 return;
278 lang_strs = internalEnglish;
279 guiLangID = 0;
283 int lngGetGuiValue() {
284 return guiLangID;
287 int lngFindGuiID(char* lang) {
288 if (lang) {
289 int i = 0;
290 for (; i < nLanguages; i++) {
291 if (stricmp(languages[i].name, lang) == 0)
292 return i + 1; // shift for Gui id
295 return 0;
298 char **lngGetGuiList() {
299 return guiLangNames;