Update so that save panel automatically fills in filename and selects default language to the current locale

This commit is contained in:
Gregory John Casamento 2023-07-15 19:30:31 -04:00
parent 08d5367812
commit 1ef3691165
2 changed files with 57 additions and 25 deletions

View file

@ -577,24 +577,35 @@
NSSavePanel *savePanel = [NSSavePanel savePanel];
NSBundle *bundle = [NSBundle bundleForClass: [GormLanguageViewController class]];
NSModalResponse result = 0;
_vc = [[GormLanguageViewController alloc] initWithNibName: @"GormLanguageViewController"
bundle: bundle];
GormDocument *doc = (GormDocument *)[self activeDocument];
NSDebugLog(@"view = %@, _vc = %@", [_vc view], _vc);
[savePanel setTitle: @"Export XLIFF"];
[savePanel setAccessoryView: [_vc view]];
[savePanel setDelegate: self];
result = [savePanel runModal];
if (NSModalResponseOK == result)
if (doc != nil)
{
NSString *filename = [[savePanel URL] path];
[(GormDocument *)[self activeDocument] exportXLIFFDocumentWithName: filename
withSourceLanguage: [_vc sourceLanguageIdentifier]
andTargetLanguage: [_vc targetLanguageIdentifier]];
NSString *fn = [[doc fileURL] path];
fn = [[fn lastPathComponent] stringByDeletingPathExtension];
fn = [fn stringByAppendingPathExtension: @"xliff"];
_vc = [[GormLanguageViewController alloc]
initWithNibName: @"GormLanguageViewController"
bundle: bundle];
NSDebugLog(@"view = %@, _vc = %@", [_vc view], _vc);
[savePanel setTitle: @"Export XLIFF"];
[savePanel setAccessoryView: [_vc view]];
[savePanel setDelegate: self];
// [savePanel setURL: [NSURL fileURLWithPath: fn]];
result = [savePanel runModalForDirectory: nil
file: fn];
if (NSModalResponseOK == result)
{
NSString *filename = [[savePanel URL] path];
[doc exportXLIFFDocumentWithName: filename
withSourceLanguage: [_vc sourceLanguageIdentifier]
andTargetLanguage: [_vc targetLanguageIdentifier]];
}
}
}
@ -604,11 +615,11 @@
{
if (flag == YES)
{
NSLog(@"Writing the document... %@", filename);
NSDebugLog(@"Writing the document... %@", filename);
}
else
{
NSLog(@"%@ not saved", filename);
NSDebugLog(@"%@ not saved", filename);
}
return filename;
}

View file

@ -4,6 +4,7 @@
#import <Foundation/NSString.h>
#import <Foundation/NSDictionary.h>
#import <Foundation/NSBundle.h>
#import <Foundation/NSLocale.h>
#import <AppKit/NSPopUpButton.h>
@ -11,6 +12,23 @@
@implementation GormLanguageViewController
- (void) selectPreferredLanguage
{
NSString *language = [[NSLocale preferredLanguages] objectAtIndex: 0];
NSInteger i = [[ldict allKeys] indexOfObject: language];
NSDebugLog(@"language = %@", language);
// Set the default translation to the current language
[sourceLanguage selectItemAtIndex: i];
[targetLanguage selectItemAtIndex: i];
// Set them since the above doesn't invoke the method that sets them.
[self updateTargetLanguage: self];
[self updateSourceLanguage: self];
}
- (void) viewDidLoad
{
NSBundle *bundle = [NSBundle bundleForClass: [self class]];
@ -22,7 +40,7 @@
[targetLanguage removeAllItems];
[sourceLanguage removeAllItems];
NSLog(@"path = %@", path);
NSDebugLog(@"path = %@", path);
ldict = [[NSDictionary alloc] initWithContentsOfFile: path];
if (ldict != nil)
@ -38,6 +56,9 @@
[targetLanguage addItemWithTitle: itemTitle];
[sourceLanguage addItemWithTitle: itemTitle];
}
// Select preferred language in pop up...
[self selectPreferredLanguage];
}
}
else
@ -54,24 +75,24 @@
- (IBAction) updateTargetLanguage: (id)sender
{
// Nothing yet...
NSInteger i = [targetLanguage indexOfSelectedItem];
targetLanguageIdentifier = [[ldict allKeys] objectAtIndex: i];
}
- (IBAction) updateSourceLanguage: (id)sender
{
// Nothing yet...
NSInteger i = [sourceLanguage indexOfSelectedItem];
sourceLanguageIdentifier = [[ldict allKeys] objectAtIndex: i];
}
- (NSString *) sourceLanguageIdentifier
{
NSInteger i = [sourceLanguage indexOfSelectedItem];
return [[ldict allKeys] objectAtIndex: i];
return sourceLanguageIdentifier;
}
- (NSString *) targetLanguageIdentifier
{
NSInteger i = [sourceLanguage indexOfSelectedItem];
return [[ldict allKeys] objectAtIndex: i];
return targetLanguageIdentifier;
}
@end