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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
|
///////////////////////////////////////////////////////////////////////////////
// Original Name: src/common/intl.cpp
// Original Purpose: Internationalization and localisation for wxWidgets
// Original Author: Vadim Zeitlin
// Modified by: Charlie Fenton for BOINC 13 June, 2013
// Created: 29/01/98
// Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
//
// This file is part of BOINC.
// http://boinc.berkeley.edu
//
// Simplified string localization code for use in BOINC adapted from
// wxWidgets src/common/intl.cpp.
//
// For sample code to get preferred system language, see
// wxLocale::GetSystemLanguage() in wxWidgets src/common/intl.cpp
//
// Those portions of this code which are taken from wxWidgets are
// Copyright: (c) 1998 Vadim Zeitlin
// and are covered by the wxWidgets license which can be found here:
// <http://www.wxwidgets.org/about/licence3.txt>
//
// This code assumes all catalogs are encoded UTF-8
// (Note: wxstd.mo files are encoded ISO 8859-1 not UTF-8.)
//
// We recommended that you first call BOINCTranslationAddCatalog()
// for each desired catalog (*.mo) file for the user's preferred
// language, then for each desired catalog (*.mo) file for the
// user's second preferred language and (optionally) then for each
// desired catalog (*.mo) file for the user's third preferred
// language. This will make it more likley that a translation
// will be found in some language useful to the user.
//
#include <stdint.h>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <sys/param.h> // for MAXPATHLEN
#include <sys/stat.h> // For stat()
#include <filesys.h>
#include "translate.h"
static const uint32_t MSGCATALOG_MAGIC = 0x950412de;
static const uint32_t MSGCATALOG_MAGIC_SW = 0xde120495;
#define MAXCATALOGS 20
#define VERBOSE true
// an entry in the string table
struct MsgTableEntry {
uint32_t nLen; // length of the string
uint32_t ofsString; // pointer to the string
};
// header of a .mo file
struct MsgCatalogHeader {
uint32_t magic, // offset +00: magic id
revision, // +04: revision
numStrings; // +08: number of strings in the file
uint32_t ofsOrigTable, // +0C: start of original string table
ofsTransTable; // +10: start of translated string table
uint32_t nHashSize, // +14: hash table size
ofsHashTable; // +18: offset of hash table start
};
struct MsgCatalogData {
uint8_t *pData; // Pointer to catalog data
uint32_t nSize; // Amount of memory pointed to by pData.
uint32_t NumStrings; // number of strings in this domain
bool bSwapped; // wrong endianness?
MsgTableEntry *pOrigTable; // pointer to original strings
MsgTableEntry *pTransTable; // pointer to translated strings
char languageCode[32]; // language code (e.g., "it_IT")
char catalogName[128]; // catalog name (e.g., "BOINC-Setup")
};
static struct MsgCatalogData theCatalogData[MAXCATALOGS];
static uint32_t numLoadedCatalogs = 0;
// swap the 2 halves of 32 bit integer if needed
static uint32_t Swap(uint32_t ui, bool bSwapped) {
return bSwapped ? (ui << 24) | ((ui & 0xff00) << 8) |
((ui >> 8) & 0xff00) | (ui >> 24)
: ui;
}
// load a catalog from disk
// open disk file and read in it's contents
//
// catalogsDir is the path to the locale directory in
// the BOINC Data directory (ending in "locale/").
//
// language code is the standad language code such
// as "fr" or "zh_CN".
//
// catalogName is the domain (the file name of the
// *.mo file without the .mo such as "BOINC-Manager").
//
// Returns true if successful.
//
static bool LoadCatalog(const char * catalogsDir,
const char *languageCode,
const char *catalogName
) {
unsigned int j;
char searchPath[MAXPATHLEN];
struct stat sbuf;
char temp[32];
char *underscore;
FILE * f;
uint8_t *pData;
uint32_t nSize;
uint32_t NumStrings;
bool bSwapped;
MsgTableEntry *pOrigTable;
MsgTableEntry *pTransTable;
MsgCatalogData *pCatalog;
#if VERBOSE
fprintf(stderr, "Attempting to load catalog %s for language code %s\n",
catalogName, languageCode);
#endif
for (j=0; j<numLoadedCatalogs; ++j) {
pCatalog = &(theCatalogData[j]);
if (!strcmp(pCatalog->catalogName, catalogName)) {
if (!strcmp(pCatalog->languageCode, languageCode)) {
// Don't load a catalog twice for the same language
#if VERBOSE
fprintf(stderr, "Ignoring Catalog %s for language code %s; it was already loaded\n",
catalogName, languageCode);
#endif
return true; // Already loaded
}
// Don't load more languages for this catalog
// if we've already "loaded" it for English
if (!strcmp(pCatalog->languageCode, "en")) {
#if VERBOSE
fprintf(stderr, "Ignoring Catalog %s for language code %s; after English for same catalog\n",
catalogName, languageCode);
#endif
return true; // Already loaded
}
}
}
// Since the original strings are English, no
// translation is needed for language en.
if (!strcmp("en", languageCode)) {
pCatalog = &(theCatalogData[numLoadedCatalogs++]);
strlcpy(pCatalog->languageCode, languageCode, sizeof(pCatalog->languageCode));
strlcpy(pCatalog->catalogName, catalogName, sizeof(pCatalog->catalogName));
return true;
}
strlcpy(searchPath, catalogsDir, sizeof(searchPath));
strlcat(searchPath, languageCode, sizeof(searchPath));
strlcat(searchPath, "/", sizeof(searchPath));
strlcat(searchPath, catalogName, sizeof(searchPath));
strlcat(searchPath, ".mo", sizeof(searchPath));
if (stat(searchPath, &sbuf) != 0) {
// Try just base locale name: for things like "fr_BE" (belgium
// french) we should use "fr" if no belgium specific message
// catalogs exist
strlcpy(temp, languageCode, sizeof(temp));
underscore = strchr(temp, (int)'_');
if (underscore == NULL) return false;
*underscore = '\0';
strlcpy(searchPath, catalogsDir, sizeof(searchPath));
strlcat(searchPath, temp, sizeof(searchPath));
strlcat(searchPath, "/", sizeof(searchPath));
strlcat(searchPath, catalogName, sizeof(searchPath));
strlcat(searchPath, ".mo", sizeof(searchPath));
if (stat(searchPath, &sbuf) != 0) return false;
}
pData = (uint8_t*)malloc(sbuf.st_size);
if (pData == NULL) return false;
f = fopen(searchPath, "r");
if (f == NULL) {
free(pData);
pData = NULL;
return false;
}
nSize = fread(pData, 1, sbuf.st_size, f);
fclose(f);
if (nSize != sbuf.st_size) {
free(pData);
pData = NULL;
return false;
}
// examine header
bool bValid = nSize + (size_t)0 > sizeof(MsgCatalogHeader);
MsgCatalogHeader *pHeader = (MsgCatalogHeader *)pData;
if ( bValid ) {
// we'll have to swap all the integers if it's true
bSwapped = pHeader->magic == MSGCATALOG_MAGIC_SW;
// check the magic number
bValid = bSwapped || pHeader->magic == MSGCATALOG_MAGIC;
}
if ( !bValid ) {
// it's either too short or has incorrect magic number
free(pData);
pData = NULL;
return false;
}
// initialize
NumStrings = Swap(pHeader->numStrings, bSwapped);
if (NumStrings <= 1) {
// This file has no translations (is effectively
// empty) so don't load it for better efficiency.
#if VERBOSE
fprintf(stderr, "File %s contains no translated strings!\n", searchPath);
#endif
free(pData);
pData = NULL;
return true; // Not an error
}
pOrigTable = (MsgTableEntry *)(pData +
Swap(pHeader->ofsOrigTable, bSwapped));
pTransTable = (MsgTableEntry *)(pData +
Swap(pHeader->ofsTransTable, bSwapped));
// now parse catalog's header and try to extract catalog charset
uint32_t ofsString = Swap(pOrigTable->ofsString, bSwapped);
// this check could fail for a corrupt message catalog
if ( ofsString + Swap(pOrigTable->nLen, bSwapped) <= nSize) {
char *begin = strstr((char *)(pData + Swap(pTransTable->ofsString, bSwapped)),
"Content-Type: text/plain; charset="
);
if (begin != NULL) {
begin += 34; //strlen("Content-Type: text/plain; charset=")
if (strncasecmp(begin, "utf-8", 5)) {
#if VERBOSE
fprintf(stderr, "File %s is not utf-8!\n", searchPath);
#endif
free(pData);
pData = NULL;
return false;
}
}
}
pCatalog = &(theCatalogData[numLoadedCatalogs++]);
pCatalog->pData = pData;
pCatalog->nSize = nSize;
pCatalog->NumStrings = NumStrings;
pCatalog->bSwapped = bSwapped;
pCatalog->pOrigTable = pOrigTable;
pCatalog->pTransTable = pTransTable;
strlcpy(pCatalog->languageCode, languageCode, sizeof(pCatalog->languageCode));
strlcpy(pCatalog->catalogName, catalogName, sizeof(pCatalog->catalogName));
#if VERBOSE
fprintf(stderr, "Successfully loaded catalog %s for language code %s\n",
catalogName, languageCode);
#endif
return true;
}
// Searches through the catalogs in the order they were added
// until it finds a translation for the src string, and
// returns a ponter to the UTF-8 encoded localized string.
// Returns a pointer to the original string if no translation
// was found.
//
uint8_t * _(char *src) {
unsigned int i, j;
MsgCatalogData *pCatalog;
for (j=0; j<numLoadedCatalogs; ++j) {
pCatalog = &(theCatalogData[j]);
// Since the original strings are English, no
// translation is needed for language en.
if (!strcmp("en", pCatalog->languageCode)) {
continue; // Try next catalog
}
if (pCatalog->pData == NULL) continue; // Should never happen
for (i=0; i<pCatalog->NumStrings; ++i) {
if (!strcmp((char *)pCatalog->pData + pCatalog->pOrigTable[i].ofsString, src)) {
return (pCatalog->pData + pCatalog->pTransTable[i].ofsString);
}
}
}
return (uint8_t *)src;
}
// catalogsDir is the path to the locale directory in
// the BOINC Data directory (ending in "locale/").
//
// language code is the standad language code such
// as "fr" or "zh_CN".
//
// catalogName is the domain (the file name of the
// *.mo file without the .mo such as "BOINC-Manager").
//
// Returns true if successful.
//
bool BOINCTranslationAddCatalog(const char * catalogsDir,
const char *languageCode,
const char * catalogName
) {
bool success = false;
DIRREF dirp;
char filename[64];
int retval;
if (numLoadedCatalogs >= (MAXCATALOGS)) {
#if VERBOSE
fprintf(stderr, "Trying to load too many catalogs\n");
#endif
return false;
}
// Add a catalog for this exact language and region code
success = LoadCatalog(catalogsDir, languageCode, catalogName);
// Add catalogs for the same language code but different region codes
dirp = dir_open(catalogsDir);
if (dirp) {
while (true) {
retval = dir_scan(filename, dirp, sizeof(filename));
if (retval) break;
if (!strcmp(languageCode, filename)) continue; // Already added above
if (!strncmp(languageCode, filename, 2)) { // First 2 characters match
if (LoadCatalog(catalogsDir, filename, catalogName)) {
success = true;
}
}
}
dir_close(dirp);
}
return success;
}
void BOINCTranslationInit() {
int i;
MsgCatalogData *pCatalog;
numLoadedCatalogs = 0;
for (i=0; i<MAXCATALOGS; ++i) {
pCatalog = &(theCatalogData[i]);
pCatalog->pData = NULL;
pCatalog->nSize = 0;
pCatalog->NumStrings = 0;
pCatalog->bSwapped = false;
pCatalog->pOrigTable = NULL;
pCatalog->pTransTable = NULL;
pCatalog->languageCode[0] = '\0';
pCatalog->catalogName[0] = '\0';
}
}
void BOINCTranslationCleanup() {
int i;
MsgCatalogData *pCatalog;
for (i=0; i<MAXCATALOGS; ++i) {
pCatalog = &(theCatalogData[i]);
if (pCatalog->pData) free(pCatalog->pData);
pCatalog->pData = NULL;
pCatalog->nSize = 0;
pCatalog->NumStrings = 0;
pCatalog->bSwapped = false;
pCatalog->pOrigTable = NULL;
pCatalog->pTransTable = NULL;
pCatalog->languageCode[0] = '\0';
pCatalog->catalogName[0] = '\0';
}
numLoadedCatalogs = 0;
}
|