This is Localization.m in view mode; [Download] [Up]
#import <appkit/Application.h>
#import <appkit/NXImage.h>
#import <appkit/nextstd.h>
#import <objc/NXStringTable.h>
#import <defaults.h>
#import <libc.h>
#define NATIVE_LANGUAGE "English"
static char *launchDir = NULL; /* if this is NULL, it will be calculated */
// note: this should probably use the which_bin() function in Thinker.m
// which won't flail from the command line like this...
static BOOL launchDirOk()
{
const char *slash;
if (launchDir) return YES;
slash = strrchr(NXArgv[0], '/');
if (slash && slash-NXArgv[0]) {
launchDir = malloc((slash-NXArgv[0]+1)*sizeof(char));
strncpy(launchDir, NXArgv[0], slash-NXArgv[0]);
launchDir[slash-NXArgv[0]] = '\0';
return YES;
}
return NO;
}
void InitLocalDateAndTime()
{
time_t t;
char buffer[16];
t = time(NULL);
strftime(buffer, 15, "%H:%M", localtime(&t));
}
id LoadLocalNib(const char *nibFile, id owner)
{
BOOL found = NO;
id retval = nil;
const char *const *languages;
char path[MAXPATHLEN+1];
languages = [NXApp systemLanguages];
if (languages && launchDirOk()) {
while (!found && *languages) {
if (!strcmp(*languages, NATIVE_LANGUAGE)) break;
sprintf(path, "%s/%s.lproj/%s", launchDir, *languages, nibFile);
if (!access(path, R_OK)) {
retval = [NXApp loadNibFile:path owner:owner withNames:NO];
found = YES;
}
languages++;
}
}
return found ? retval : [NXApp loadNibSection:nibFile owner:owner withNames:NO];
}
id LocalImage(const char *file)
{
id retval = nil;
const char *const *languages;
char path[MAXPATHLEN+1];
languages = [NXApp systemLanguages];
if (languages) {
while (!retval && *languages) {
if (!strcmp(*languages, NATIVE_LANGUAGE)) break;
sprintf(path, "%s/%s.lproj/%s", launchDir, *languages, file);
retval = [NXImage newFromFile:file];
languages++;
}
} else {
retval = [NXImage newFromSection:file];
}
return retval;
}
const char *doLocalString(const char *tableName, const char *key, const char *value)
{
id table = nil;
const char *tableValue;
const char *const *languages;
BOOL isMyAppsLanguage = NO;
char buffer[MAXPATHLEN+1];
static int debugLanguages = -1;
static id stringTableTable = nil;
if (!(languages = [NXApp systemLanguages])) return value ? value : key;
if (!tableName) tableName = [NXApp appName];
if (tableName[0] == '"' && tableName[strlen(tableName)-1] == '"') {
strcpy(buffer, tableName+1);
buffer[strlen(buffer)-1] = '\0';
tableName = NXUniqueString(buffer);
}
if (debugLanguages < 0) debugLanguages = NXGetDefaultValue([NXApp appName], "NXDebugLanguages") ? 1 : 0;
if (!(table = [stringTableTable valueForKey:tableName])) {
while (!table && *languages) {
if (!strcmp(*languages, NATIVE_LANGUAGE)) {
isMyAppsLanguage = YES;
break;
}
sprintf(buffer, "%s/%s.lproj/%s.strings", launchDir, *languages, tableName);
table = [NXStringTable newFromFile:buffer];
languages++;
}
if (!table) {
if (debugLanguages && !isMyAppsLanguage) NXLogError("Cannot parse '%s' strings.", tableName);
table = [NXStringTable new];
}
if (!stringTableTable) stringTableTable = [HashTable newKeyDesc:"*"];
[stringTableTable insertKey:tableName value:table];
}
tableValue = [table valueForStringKey:key];
if (!tableValue) {
if (debugLanguages && !isMyAppsLanguage)
NXLogError("Cannot find value for string '%s' in table '%s'.", key, tableName);
tableValue = value ? value : key;
[table insertKey:key value:(void *)value];
}
return tableValue;
}
These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.