| 1 | /* |
| 2 | Copyright (C) 2006 Parallel Realities |
| 3 | |
| 4 | This program is free software; you can redistribute it and/or |
| 5 | modify it under the terms of the GNU General Public License |
| 6 | as published by the Free Software Foundation; either version 2 |
| 7 | of the License, or (at your option) any later version. |
| 8 | |
| 9 | This program is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | |
| 13 | See the GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with this program; if not, write to the Free Software |
| 17 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 18 | |
| 19 | */ |
| 20 | |
| 21 | #include "localeLoader.h" |
| 22 | |
| 23 | void loadLocaleIndex() |
| 24 | { |
| 25 | Properties *properties = &LocaleManager::getInstance()->localeIndex; |
| 26 | |
| 27 | if (pak->getFileLocation("data/locale/index") == FILE_NOT_FOUND) |
| 28 | { |
| 29 | printf("Locale data has not been installed. Blob And Conquer cannot run without it.\n"); |
| 30 | exit(1); |
| 31 | } |
| 32 | |
| 33 | List *list = loadKeyValuePairs("data/locale/index"); |
| 34 | |
| 35 | if (list == NULL) |
| 36 | { |
| 37 | printf("ERROR: Couldn't load Locale Index File (data/locale/index)"); |
| 38 | exit(1); |
| 39 | } |
| 40 | |
| 41 | for (Data *data = (Data*)list->getFirstElement() ; data != NULL ; data = (Data*)data->next) |
| 42 | { |
| 43 | properties->setProperty(data->getKey(), data->getValue()); |
| 44 | } |
| 45 | |
| 46 | delete list; |
| 47 | } |
| 48 | |
| 49 | Properties *loadLocaleData(const char *filename) |
| 50 | { |
| 51 | debug(("Loading locale data from '%s'\n", filename)); |
| 52 | |
| 53 | if (!pak->unpack(filename), PAK::TEXT) |
| 54 | { |
| 55 | if (strcmp(localeManager->getLanguage(), "en") == 0) |
| 56 | { |
| 57 | printf("ERROR - Could not load '%s'\n", filename); |
| 58 | exit(1); |
| 59 | } |
| 60 | return NULL; |
| 61 | } |
| 62 | |
| 63 | Properties *properties = new Properties(); |
| 64 | |
| 65 | char key[1024]; |
| 66 | char value[1024]; |
| 67 | |
| 68 | char *token = strtok((char*)pak->dataBuffer, "\n"); |
| 69 | |
| 70 | bool done = false; |
| 71 | |
| 72 | while (!done) |
| 73 | { |
| 74 | // ignore blank lines |
| 75 | if ((strcmp(token, "") == 0) || (strlen(token) == 0)) |
| 76 | { |
| 77 | token = strtok(NULL, "\n"); |
| 78 | continue; |
| 79 | } |
| 80 | |
| 81 | // looks like a comment. |
| 82 | if (token[0] == '/') |
| 83 | { |
| 84 | token = strtok(NULL, "\n"); |
| 85 | continue; |
| 86 | } |
| 87 | |
| 88 | if (strcmp(token, "@END@") == 0) |
| 89 | { |
| 90 | done = true; |
| 91 | } |
| 92 | else if (!strstr(token, ";")) |
| 93 | { |
| 94 | token = strtok(NULL, "\n"); |
| 95 | continue; |
| 96 | } |
| 97 | else |
| 98 | { |
| 99 | sscanf(token, "%s %[^;]", key, value); |
| 100 | |
| 101 | properties->setProperty(key, value); |
| 102 | } |
| 103 | |
| 104 | token = strtok(NULL, "\n"); |
| 105 | } |
| 106 | |
| 107 | pak->freeAll(); |
| 108 | |
| 109 | return properties; |
| 110 | } |
| 111 | |
| 112 | void loadGameLocaleData(const char *filename, bool append) |
| 113 | { |
| 114 | String path; |
| 115 | |
| 116 | path.setText("data/locale/en/%s", filename); |
| 117 | Properties *defaultLang = loadLocaleData(path.getText()); |
| 118 | |
| 119 | if (defaultLang != NULL) |
| 120 | { |
| 121 | if (!append) |
| 122 | { |
| 123 | debug(("Setting Game Properties to Locale Manager...\n")); |
| 124 | LocaleManager::getInstance()->setLocaleData(defaultLang); |
| 125 | } |
| 126 | else |
| 127 | { |
| 128 | debug(("Appending Game Properties to Locale Manager...\n")); |
| 129 | LocaleManager::getInstance()->appendLocaleData(defaultLang); |
| 130 | delete defaultLang; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | if (strcmp(localeManager->getLanguage(), "en") == 0) |
| 135 | { |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | path.setText("data/locale/%s/%s", localeManager->getLanguage(), filename); |
| 140 | |
| 141 | Properties *translated = loadLocaleData(path.getText()); |
| 142 | |
| 143 | if (translated != NULL) |
| 144 | { |
| 145 | if (!append) |
| 146 | { |
| 147 | debug(("Setting Game Properties to Locale Manager...\n")); |
| 148 | LocaleManager::getInstance()->setLocaleData(translated); |
| 149 | } |
| 150 | else |
| 151 | { |
| 152 | debug(("Appending Game Properties to Locale Manager...\n")); |
| 153 | LocaleManager::getInstance()->appendLocaleData(translated); |
| 154 | delete translated; |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | void loadSystemLocaleData(const char *filename) |
| 160 | { |
| 161 | String path; |
| 162 | |
| 163 | path.setText("data/locale/en/%s", filename); |
| 164 | Properties *defaultLang = loadLocaleData(path.getText()); |
| 165 | |
| 166 | if (defaultLang != NULL) |
| 167 | { |
| 168 | debug(("Setting System Properties to Locale Manager...\n")); |
| 169 | LocaleManager::getInstance()->setSystemLocaleData(defaultLang); |
| 170 | } |
| 171 | |
| 172 | if (strcmp(localeManager->getLanguage(), "en") == 0) |
| 173 | { |
| 174 | return; |
| 175 | } |
| 176 | |
| 177 | path.setText("data/locale/%s/%s", localeManager->getLanguage(), filename); |
| 178 | |
| 179 | Properties *translated = loadLocaleData(path.getText()); |
| 180 | |
| 181 | if (translated != NULL) |
| 182 | { |
| 183 | debug(("Setting System Properties to Locale Manager...\n")); |
| 184 | LocaleManager::getInstance()->setSystemLocaleData(translated); |
| 185 | } |
| 186 | } |