Suggestion from "mgh".
[open-ps2-loader.git] / src / lang.c
blob11322a1a7756a91ed24609a90ebf45bc5c02e7c5
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 "ETH 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 "Unused",
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",
128 "Backspace",
129 "Space",
130 "Enter",
131 "Mode",
132 "VMC Slot 1",
133 "VMC Slot 2",
134 "Game ID",
135 "DMA Mode",
136 "V-Sync",
137 "Mode 1",
138 "Mode 2",
139 "Mode 3",
140 "Mode 4",
141 "Mode 5",
142 "Mode 6",
143 "Mode 7",
144 "Mode 8",
145 "Mode 9",
146 "Callback timer",
147 "Apply a delay to CDVD functions (0 is default)"
150 static int guiLangID = 0;
151 static char **lang_strs = internalEnglish;
152 static int nValidEntries = LANG_STR_COUNT;
154 static int nLanguages = 0;
155 static language_t languages[MAX_LANGUAGE_FILES];
156 static char **guiLangNames;
158 // localised string getter
159 char *_l(unsigned int id) {
160 return lang_strs[id];
163 static void lngFreeFromFile() {
164 if (guiLangID == 0)
165 return;
167 int strId = 0;
168 for(; strId < nValidEntries; strId++) {
169 free(lang_strs[strId]);
170 lang_strs[strId] = NULL;
172 free(lang_strs);
173 lang_strs = NULL;
176 static int lngLoadFromFile(char* path, char *name) {
177 file_buffer_t* fileBuffer = openFileBuffer(path, O_RDONLY, 1, 1024);
178 if (fileBuffer) {
179 // file exists, try to read it and load the custom lang
180 lang_strs = (char**) malloc(LANG_STR_COUNT * sizeof(char**));
182 int strId = 0;
183 while (strId < LANG_STR_COUNT && readFileBuffer(fileBuffer, &lang_strs[strId])) {
184 strId++;
186 closeFileBuffer(fileBuffer);
188 LOG("LANG Loaded %d entries\n", strId);
190 // remember how many entries were read from the file (for the free later)
191 nValidEntries = strId;
193 // if necessary complete lang with default internal
194 while (strId < LANG_STR_COUNT) {
195 LOG("LANG Default entry added: %s\n", internalEnglish[strId]);
196 lang_strs[strId] = internalEnglish[strId];
197 strId++;
200 char path[255];
201 snprintf(path, 255, "%s/font_%s.ttf", gBaseMCDir, name);
202 LOG("LANG Custom font path: %s\n", path);
203 fntLoadDefault(path);
205 return 1;
207 return 0;
210 char* lngGetValue() {
211 return guiLangNames[guiLangID];
214 static int lngReadEntry(int index, char* path, char* separator, char* name, unsigned int mode) {
215 if (!FIO_SO_ISDIR(mode)) {
216 if(strstr(name, ".lng") || strstr(name, ".LNG")) {
218 language_t* currLang = &languages[index];
220 // filepath for this language file
221 int length = strlen(path) + 1 + strlen(name) + 1;
222 currLang->filePath = (char*) malloc(length * sizeof(char));
223 sprintf(currLang->filePath, "%s%s%s", path, separator, name);
225 // extract name for this language (will be used for the English translation)
226 length = strlen(name) - 5 - 4 + 1;
227 currLang->name = (char*) malloc(length * sizeof(char));
228 memcpy(currLang->name, name + 5, length);
229 currLang->name[length - 1] = '\0';
231 /*file_buffer_t* fileBuffer = openFileBuffer(currLang->filePath, 1, 1024);
232 if (fileBuffer) {
233 // read localized name of language from file
234 if (readLineContext(fileBuffer, &currLang->name))
235 readLineContext(fileBuffer, &currLang->fontName);
236 closeFileBuffer(fileBuffer);
239 index++;
242 return index;
245 void lngInit() {
246 fntInit();
248 nLanguages = listDir(gBaseMCDir, "/", MAX_LANGUAGE_FILES, &lngReadEntry);
250 // build the languages name list
251 guiLangNames = (char**) malloc((nLanguages + 2) * sizeof(char**));
253 // add default internal (english)
254 guiLangNames[0] = internalEnglish[0];
256 int i = 0;
257 for (; i < nLanguages; i++) {
258 guiLangNames[i + 1] = languages[i].name;
261 guiLangNames[nLanguages + 1] = NULL;
264 void lngEnd() {
265 lngFreeFromFile();
267 int i = 0;
268 for (; i < nLanguages; i++) {
269 free(languages[i].name);
270 free(languages[i].filePath);
273 free(guiLangNames);
275 fntEnd();
278 void lngSetGuiValue(int langID) {
279 if (guiLangID != langID) {
281 lngFreeFromFile();
283 if (langID != 0) {
284 language_t* currLang = &languages[langID - 1];
285 if (lngLoadFromFile(currLang->filePath, currLang->name)) {
286 guiLangID = langID;
287 return;
291 lang_strs = internalEnglish;
292 guiLangID = 0;
296 int lngGetGuiValue() {
297 return guiLangID;
300 int lngFindGuiID(char* lang) {
301 if (lang) {
302 int i = 0;
303 for (; i < nLanguages; i++) {
304 if (stricmp(languages[i].name, lang) == 0)
305 return i + 1; // shift for Gui id
308 return 0;
311 char **lngGetGuiList() {
312 return guiLangNames;