Skip to content

Commit

Permalink
addressed edge cases which led to 404 on dict.leo.org
Browse files Browse the repository at this point in the history
  • Loading branch information
vaddieg committed Jul 14, 2019
1 parent 3f37c67 commit beed647
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions LookupLeoDict/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,27 @@ - (void)lookupWord:(NSPasteboard *)pboard userData:(NSString *)userData error:(N
NSString *lookup = [pboardString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLPathAllowedCharacterSet]];

NSString *requestStr = nil;
if ([pboardString componentsSeparatedByString:@" "].count > 2) {

BOOL germanUsed = [[SimpleDictOpener lleoOptions][srcOpt] isEqualToString:@"deutsch"] || [[SimpleDictOpener lleoOptions][targOpt] isEqualToString:@"deutsch"];
BOOL shouldUseLeo = ([pboardString componentsSeparatedByString:@" "].count <= 2) && germanUsed;

if (!shouldUseLeo) {

// Looks like a sentence, use google or deepL
NSString *fmt = @[@"https://www.deepl.com/translator#%@/%@/%@",
@"https://translate.google.com/#%@/%@/%@"][engine];


requestStr = [NSString stringWithFormat:fmt, [SimpleDictOpener gtOptions][srcOpt], [SimpleDictOpener gtOptions][targOpt], lookup];
} else {
// Use leo (it handles 2 words sometimes)
requestStr = [NSString stringWithFormat:@"https://dict.leo.org/%@-%@/%@",[SimpleDictOpener lleoOptions][srcOpt], [SimpleDictOpener lleoOptions][targOpt] ,lookup];
// use leo-related url hacks, de should appear as 2nd arg
NSString *nonDeLang = [[SimpleDictOpener lleoOptions][srcOpt] isEqualToString:@"deutsch"] ? [SimpleDictOpener lleoOptions][targOpt] : [SimpleDictOpener lleoOptions][srcOpt];

requestStr = [NSString stringWithFormat:@"https://dict.leo.org/%@-deutsch/%@",nonDeLang ,lookup];
}


NSLog(@"opening url:%@", requestStr);
if (![[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:requestStr]]) {
NSLog(@"Can't open URL %@", requestStr);
}
Expand All @@ -88,7 +96,6 @@ - (IBAction)didChangeSourceLanguage:(NSPopUpButton *)sender {
[[NSUserDefaults standardUserDefaults] setInteger:sender.indexOfSelectedItem forKey:kSourceLangDefaultsKey];
}


- (IBAction)didChangeTargetLanguage:(NSPopUpButton *)sender {
[[NSUserDefaults standardUserDefaults] setInteger:sender.indexOfSelectedItem forKey:kTargetLangDefaultsKey];
}
Expand Down

0 comments on commit beed647

Please sign in to comment.