Add method to export and import strings, add code to parse arguments

This commit is contained in:
Gregory John Casamento 2023-07-01 23:39:22 -04:00
parent abdeb60d0c
commit 8d0c4d107a
3 changed files with 286 additions and 86 deletions

View file

@ -212,19 +212,77 @@
withParentObject: (id)parentObj;
/* Language translation */
/**
* Load a given file into the reciever using `filename`.
*/
- (void) importStringsFromFile: (NSString *)filename;
/**
* This method is used to translate all of the strings in the file from one language
* into another. This is helpful when attempting to translate an application for use
* in different locales.
*/
- (void) translate: (id)sender;
/**
* Export the strings from receiver to the file indicated by 'filename'.
*/
- (void) exportStringsToFile: (NSString *)filename;
/**
* Bring up a save panel to export the strings from the receiver.
*/
- (void) exportStrings: (id)sender;
/* Managing classes */
/**
* Shared class manager
*/
- (GormClassManager*) classManager;
/**
* Create a subclass of the selected class
*/
- (id) createSubclass: (id)sender;
/**
* Instantiate the selected class
*/
- (id) instantiateClass: (id)sender;
/**
* Generate the class files for the selected class
*/
- (id) createClassFiles: (id)sender;
/**
* Add attribute to class
*/
- (id) addAttributeToClass: (id)sender;
/**
* Remove the selected class
*/
- (id) remove: (id)sender;
/**
* Select class named className
*/
- (void) selectClass: (NSString *)className;
/**
* Select class named className and edit it if flag is YES
*/
- (void) selectClass: (NSString *)className editClass: (BOOL)flag;
/**
* Returns YES if a class is selected in the view
*/
- (BOOL) classIsSelected;
/**
* Removes all instances of class named classNamed
*/
- (void) removeAllInstancesOfClass: (NSString *)classNamed;
/* Sound & Image support */

View file

@ -3142,6 +3142,51 @@ static void _real_close(GormDocument *self,
return allObjects;
}
- (void) importStringsFromFile: (NSString *)filename
{
NSMutableArray *allObjects = [self _collectAllObjects];
NSDictionary *dictionary = nil;
NSEnumerator *en = nil;
id obj = nil;
dictionary = [[NSString stringWithContentsOfFile: filename] propertyListFromStringsFileFormat];
// change to translated values.
en = [allObjects objectEnumerator];
while((obj = [en nextObject]) != nil)
{
NSString *translation = nil;
if([obj respondsToSelector: @selector(setTitle:)] &&
[obj respondsToSelector: @selector(title)])
{
translation = [dictionary objectForKey: [obj title]];
if(translation != nil)
{
[obj setTitle: translation];
}
}
else if([obj respondsToSelector: @selector(setStringValue:)] &&
[obj respondsToSelector: @selector(stringValue)])
{
translation = [dictionary objectForKey: [obj stringValue]];
if(translation != nil)
{
[obj setStringValue: translation];
}
}
else if([obj respondsToSelector: @selector(setLabel:)] &&
[obj respondsToSelector: @selector(label)])
{
translation = [dictionary objectForKey: [obj label]];
if(translation != nil)
{
[obj setLabel: translation];
}
}
}
}
/**
* This method is used to translate all of the strings in the file from one language
* into another. This is helpful when attempting to translate an application for use
@ -3163,13 +3208,12 @@ static void _real_close(GormDocument *self,
{
NSMutableArray *allObjects = [self _collectAllObjects];
NSString *filename = [oPanel filename];
NSDictionary *dictionary = nil;
NSEnumerator *en = nil;
id obj = nil;
NS_DURING
{
dictionary = [[NSString stringWithContentsOfFile: filename] propertyListFromStringsFileFormat];
[self importStringsFromFile: filename];
}
NS_HANDLER
{
@ -3177,50 +3221,17 @@ static void _real_close(GormDocument *self,
NSRunAlertPanel(_(@"Problem loading strings"),
message, nil, nil, nil);
}
NS_ENDHANDLER
NS_ENDHANDLER;
[self touch]; // mark the document as modified...
// change to translated values.
en = [allObjects objectEnumerator];
while((obj = [en nextObject]) != nil)
{
NSString *translation = nil;
if([obj respondsToSelector: @selector(setTitle:)] &&
[obj respondsToSelector: @selector(title)])
if([obj isKindOfClass: [NSView class]])
{
translation = [dictionary objectForKey: [obj title]];
if(translation != nil)
{
[obj setTitle: translation];
}
}
else if([obj respondsToSelector: @selector(setStringValue:)] &&
[obj respondsToSelector: @selector(stringValue)])
{
translation = [dictionary objectForKey: [obj stringValue]];
if(translation != nil)
{
[obj setStringValue: translation];
}
}
else if([obj respondsToSelector: @selector(setLabel:)] &&
[obj respondsToSelector: @selector(label)])
{
translation = [dictionary objectForKey: [obj label]];
if(translation != nil)
{
[obj setLabel: translation];
}
}
if(translation != nil)
{
if([obj isKindOfClass: [NSView class]])
{
[obj setNeedsDisplay: YES];
}
[self touch];
[obj setNeedsDisplay: YES];
}
// redisplay/flush, if the object is a window.
@ -3234,11 +3245,57 @@ static void _real_close(GormDocument *self,
[w enableFlushWindow];
[w flushWindowIfNeeded];
}
}
}
}
- (void) exportStringsToFile: (NSString *)filename
{
NSMutableArray *allObjects = [self _collectAllObjects];
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
NSEnumerator *en = [allObjects objectEnumerator];
id obj = nil;
BOOL touched = NO;
// change to translated values.
while((obj = [en nextObject]) != nil)
{
NSString *string = nil;
if([obj respondsToSelector: @selector(setTitle:)] &&
[obj respondsToSelector: @selector(title)])
{
string = [obj title];
}
else if([obj respondsToSelector: @selector(setStringValue:)] &&
[obj respondsToSelector: @selector(stringValue)])
{
string = [obj stringValue];
}
else if([obj respondsToSelector: @selector(setLabel:)] &&
[obj respondsToSelector: @selector(label)])
{
string = [obj label];
}
if(string != nil)
{
[dictionary setObject: string forKey: string];
touched = YES;
}
}
if(touched)
{
NSString *stringToWrite =
@"/* TRANSLATORS: Make sure to quote all translated strings if\n"
@" they contain spaces or non-ASCII characters. */\n\n";
stringToWrite = [stringToWrite stringByAppendingString:
[dictionary descriptionInStringsFileFormat]];
[stringToWrite writeToFile: filename atomically: YES];
}
}
/**
* This method is used to export all strings in a document to a file for Language
* translation. This allows the user to see all of the strings which can be translated
@ -3255,50 +3312,8 @@ static void _real_close(GormDocument *self,
file: nil];
if (result == NSOKButton)
{
NSMutableArray *allObjects = [self _collectAllObjects];
NSString *filename = [sp filename];
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
NSEnumerator *en = [allObjects objectEnumerator];
id obj = nil;
BOOL touched = NO;
// change to translated values.
while((obj = [en nextObject]) != nil)
{
NSString *string = nil;
if([obj respondsToSelector: @selector(setTitle:)] &&
[obj respondsToSelector: @selector(title)])
{
string = [obj title];
}
else if([obj respondsToSelector: @selector(setStringValue:)] &&
[obj respondsToSelector: @selector(stringValue)])
{
string = [obj stringValue];
}
else if([obj respondsToSelector: @selector(setLabel:)] &&
[obj respondsToSelector: @selector(label)])
{
string = [obj label];
}
if(string != nil)
{
[dictionary setObject: string forKey: string];
touched = YES;
}
}
if(touched)
{
NSString *stringToWrite =
@"/* TRANSLATORS: Make sure to quote all translated strings if\n"
@" they contain spaces or non-ASCII characters. */\n\n";
stringToWrite = [stringToWrite stringByAppendingString:
[dictionary descriptionInStringsFileFormat]];
[stringToWrite writeToFile: filename atomically: YES];
}
[self exportStringsToFile: filename];
}
}

View file

@ -99,6 +99,72 @@ static NSMutableArray *__types = nil;
#pragma GCC diagnostic pop
@interface ArgPair : NSObject <NSCopying>
{
NSString *_argument;
NSString *_value;
}
- (void) setArgument: (NSString *)arg;
- (NSString *) argument;
- (void) setValue: (NSString *)val;
- (NSString *) value;
@end
@implementation ArgPair
- (id) init
{
self = [super init];
if (self != nil)
{
_argument = nil;
_value = nil;
}
return self;
}
- (void) dealloc
{
RELEASE(_argument);
RELEASE(_value);
[super dealloc];
}
- (void) setArgument: (NSString *)arg
{
ASSIGN(_argument, arg);
}
- (NSString *) argument
{
return _argument;
}
- (void) setValue: (NSString *)val
{
ASSIGN(_value, val);
}
- (NSString *) value
{
return _value;
}
- (id) copyWithZone: (NSZone *)z
{
id obj = [[[self class] allocWithZone: z] init];
[obj setArgument: _argument];
[obj setValue: _value];
return obj;
}
@end
// AppDelegate...
@implementation AppDelegate
@ -110,6 +176,56 @@ static NSMutableArray *__types = nil;
}
}
- (NSDictionary *) parseArguments
{
GormDocumentController *dc = [GormDocumentController sharedDocumentController];
NSMutableDictionary *result = [NSMutableDictionary dictionary];
NSProcessInfo *pi = [NSProcessInfo processInfo];
NSArray *args = [pi arguments];
NSEnumerator *en = [args objectEnumerator];
id obj = nil;
BOOL parse_val = NO;
ArgPair *pair = nil; // [[ArgPair alloc] init];
while ((obj = [en nextObject]) != nil)
{
if (parse_val)
{
[pair setValue: obj];
[result setObject: pair forKey: [pair argument]];
parse_val = NO;
continue;
}
else
{
pair = [[ArgPair alloc] init];
if ([dc typeFromFileExtension: [obj pathExtension]] != nil)
{
[pair setArgument: @"--read"];
[pair setValue: obj];
[result setObject: pair forKey: @"--read"];
}
else if ([obj isEqualToString: @"--write"])
{
[pair setArgument: obj];
parse_val = YES;
}
else if ([obj isEqualToString: @"--export-strings-file"])
{
[pair setArgument: obj];
parse_val = YES;
}
else if ([obj isEqualToString: @"--import-strings-file"])
{
[pair setArgument: obj];
parse_val = YES;
}
}
}
return result;
}
- (void) process
{
NSProcessInfo *pi = [NSProcessInfo processInfo];
@ -123,10 +239,21 @@ static NSMutableArray *__types = nil;
NSString *file = [[pi arguments] objectAtIndex: 1];
GormDocumentController *dc = [GormDocumentController sharedDocumentController];
GormDocument *doc = nil;
NSDictionary *args = [self parseArguments];
ArgPair *opt = nil;
NSLog(@"args = %@", args);
NSLog(@"file = %@", file);
doc = [dc openDocumentWithContentsOfFile: file display: NO];
NSLog(@"Document = %@", doc);
NSDebugLog(@"Document = %@", doc);
opt = [args objectForKey: @"--export-strings-file"];
if (opt != nil)
{
NSString *stringsFile = [opt value];
[doc exportStringsToFile: stringsFile];
}
}
[NSClassSwapper setIsInInterfaceBuilder: NO];