Skip to content

Commit fdf9b6e

Browse files
committed
fix loading of localized examples files
1 parent 48c1e0c commit fdf9b6e

File tree

6 files changed

+63
-11
lines changed

6 files changed

+63
-11
lines changed

FlashlightApp/Flashlight.xcodeproj/project.pbxproj

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@
4545
5FBEC9B01A1539C0007FEC54 /* ConvenienceCategories.m in Sources */ = {isa = PBXBuildFile; fileRef = 5FBEC9AF1A1539C0007FEC54 /* ConvenienceCategories.m */; };
4646
5FBEC9B61A154DEA007FEC54 /* Icons.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 5FBEC9B51A154DEA007FEC54 /* Icons.xcassets */; };
4747
5FBEC9C41A166C00007FEC54 /* dsa_pub.pem in Resources */ = {isa = PBXBuildFile; fileRef = 5FBEC9C31A166C00007FEC54 /* dsa_pub.pem */; };
48-
5FC016691A1DBBE500F8E4E7 /* NSObject+InternationalizedValueForKey.m in Sources */ = {isa = PBXBuildFile; fileRef = 5FC016681A1DBBE500F8E4E7 /* NSObject+InternationalizedValueForKey.m */; };
4948
5FC0166C1A1DBE7300F8E4E7 /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = 5FC0166E1A1DBE7300F8E4E7 /* Localizable.strings */; };
5049
5FD3EA7E1A202777005A312E /* PluginDirectoryAPI.m in Sources */ = {isa = PBXBuildFile; fileRef = 5FD3EA7D1A202777005A312E /* PluginDirectoryAPI.m */; };
5150
5FD3EA811A2035AB005A312E /* WebKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5FD3EA801A2035AB005A312E /* WebKit.framework */; };
5251
5FD3EA841A204CA1005A312E /* NSURLComponents+ValueForQueryKey.m in Sources */ = {isa = PBXBuildFile; fileRef = 5FD3EA831A204CA1005A312E /* NSURLComponents+ValueForQueryKey.m */; };
5352
5FF5D4111A675F0800641D7A /* UpdateChecker.m in Sources */ = {isa = PBXBuildFile; fileRef = 5FF5D4101A675F0800641D7A /* UpdateChecker.m */; };
53+
5FF873161A6D71EA00EAD72F /* NSObject+InternationalizedValueForKey.m in Sources */ = {isa = PBXBuildFile; fileRef = 5FF873151A6D71EA00EAD72F /* NSObject+InternationalizedValueForKey.m */; };
5454
6C3A4C44159EADF900985CCD /* FlashlightSIMBLAgent.app in CopyFiles */ = {isa = PBXBuildFile; fileRef = 6C3A4C35159E7E7000985CCD /* FlashlightSIMBLAgent.app */; };
5555
6C564485159E7C0800215467 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6C564484159E7C0800215467 /* Cocoa.framework */; };
5656
6C56448F159E7C0800215467 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 6C56448D159E7C0800215467 /* InfoPlist.strings */; };
@@ -179,8 +179,6 @@
179179
5FBEC9AF1A1539C0007FEC54 /* ConvenienceCategories.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ConvenienceCategories.m; sourceTree = "<group>"; };
180180
5FBEC9B51A154DEA007FEC54 /* Icons.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Icons.xcassets; sourceTree = "<group>"; };
181181
5FBEC9C31A166C00007FEC54 /* dsa_pub.pem */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = dsa_pub.pem; sourceTree = "<group>"; };
182-
5FC016671A1DBBE500F8E4E7 /* NSObject+InternationalizedValueForKey.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSObject+InternationalizedValueForKey.h"; sourceTree = "<group>"; };
183-
5FC016681A1DBBE500F8E4E7 /* NSObject+InternationalizedValueForKey.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSObject+InternationalizedValueForKey.m"; sourceTree = "<group>"; };
184182
5FC0166D1A1DBE7300F8E4E7 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/Localizable.strings; sourceTree = "<group>"; };
185183
5FC0166F1A1DBE9800F8E4E7 /* fr */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = fr; path = fr.lproj/Localizable.strings; sourceTree = "<group>"; };
186184
5FC13C161A4906FC008A0FE3 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/MainMenu.xib; sourceTree = "<group>"; };
@@ -204,6 +202,8 @@
204202
5FD3EA831A204CA1005A312E /* NSURLComponents+ValueForQueryKey.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSURLComponents+ValueForQueryKey.m"; sourceTree = "<group>"; };
205203
5FF5D40F1A675F0800641D7A /* UpdateChecker.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UpdateChecker.h; sourceTree = "<group>"; };
206204
5FF5D4101A675F0800641D7A /* UpdateChecker.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = UpdateChecker.m; sourceTree = "<group>"; };
205+
5FF873141A6D71EA00EAD72F /* NSObject+InternationalizedValueForKey.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "NSObject+InternationalizedValueForKey.h"; path = "../FlashlightKit/FlashlightKit/NSObject+InternationalizedValueForKey.h"; sourceTree = "<group>"; };
206+
5FF873151A6D71EA00EAD72F /* NSObject+InternationalizedValueForKey.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "NSObject+InternationalizedValueForKey.m"; path = "../FlashlightKit/FlashlightKit/NSObject+InternationalizedValueForKey.m"; sourceTree = "<group>"; };
207207
653963C51A2766CD00D8F6E9 /* libPods-Flashlight.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = "libPods-Flashlight.a"; path = "../../../../../Library/Developer/Xcode/DerivedData/Pods-gpoaxpycjinulnbzpvilkgtvohcj/Build/Products/Debug/libPods-Flashlight.a"; sourceTree = "<group>"; };
208208
658988FB1A270ACF00ABE5E9 /* it */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = it; path = it.lproj/Localizable.strings; sourceTree = "<group>"; };
209209
6C3A4C2B159E7E6F00985CCD /* SIMBL.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; path = SIMBL.xcodeproj; sourceTree = "<group>"; };
@@ -346,8 +346,8 @@
346346
children = (
347347
5FD3EA821A204CA1005A312E /* NSURLComponents+ValueForQueryKey.h */,
348348
5FD3EA831A204CA1005A312E /* NSURLComponents+ValueForQueryKey.m */,
349-
5FC016671A1DBBE500F8E4E7 /* NSObject+InternationalizedValueForKey.h */,
350-
5FC016681A1DBBE500F8E4E7 /* NSObject+InternationalizedValueForKey.m */,
349+
5FF873141A6D71EA00EAD72F /* NSObject+InternationalizedValueForKey.h */,
350+
5FF873151A6D71EA00EAD72F /* NSObject+InternationalizedValueForKey.m */,
351351
5FBEC9AE1A1539BF007FEC54 /* ConvenienceCategories.h */,
352352
5FBEC9AF1A1539C0007FEC54 /* ConvenienceCategories.m */,
353353
);
@@ -636,14 +636,14 @@
636636
5F60B0FB1A194F11004BCF08 /* PluginEditorWindowController.m in Sources */,
637637
5F6326FB1A0933E800CE241E /* ZZAESDecryptInputStream.mm in Sources */,
638638
5F2B37751A3E42E500AC2C19 /* SearchPluginEditorWindowController.m in Sources */,
639-
5FC016691A1DBBE500F8E4E7 /* NSObject+InternationalizedValueForKey.m in Sources */,
640639
6CCA2AF115AAC6200085ABB4 /* SIMBL.m in Sources */,
641640
5F6326C91A08538B00CE241E /* PluginCellView.m in Sources */,
642641
5F6327091A0933E800CE241E /* ZZOldArchiveEntryWriter.mm in Sources */,
643642
5F6327011A0933E800CE241E /* ZZDeflateOutputStream.m in Sources */,
644643
5F6327041A0933E800CE241E /* ZZFileChannelOutput.m in Sources */,
645644
5FD3EA841A204CA1005A312E /* NSURLComponents+ValueForQueryKey.m in Sources */,
646645
5F63270C1A0933E800CE241E /* ZZStoreOutputStream.m in Sources */,
646+
5FF873161A6D71EA00EAD72F /* NSObject+InternationalizedValueForKey.m in Sources */,
647647
5F6326FF1A0933E800CE241E /* ZZDataChannel.m in Sources */,
648648
5F6326CD1A09310F00CE241E /* PluginInstallTask.m in Sources */,
649649
5F6327031A0933E800CE241E /* ZZFileChannel.m in Sources */,

FlashlightApp/FlashlightKit/FlashlightKit.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@
7676
5FC13E6E1A4E2EB3008A0FE3 /* FlashlightQueryEngine.m in Sources */ = {isa = PBXBuildFile; fileRef = 5FC13E6C1A4E2EB3008A0FE3 /* FlashlightQueryEngine.m */; };
7777
5FC13E741A4E6B58008A0FE3 /* NSTask+FlashlightExtensions.h in Headers */ = {isa = PBXBuildFile; fileRef = 5FC13E721A4E6B58008A0FE3 /* NSTask+FlashlightExtensions.h */; };
7878
5FC13E751A4E6B58008A0FE3 /* NSTask+FlashlightExtensions.m in Sources */ = {isa = PBXBuildFile; fileRef = 5FC13E731A4E6B58008A0FE3 /* NSTask+FlashlightExtensions.m */; };
79+
5FF873121A6D718800EAD72F /* NSObject+InternationalizedValueForKey.h in Headers */ = {isa = PBXBuildFile; fileRef = 5FF873101A6D718800EAD72F /* NSObject+InternationalizedValueForKey.h */; settings = {ATTRIBUTES = (Public, ); }; };
80+
5FF873131A6D718800EAD72F /* NSObject+InternationalizedValueForKey.m in Sources */ = {isa = PBXBuildFile; fileRef = 5FF873111A6D718800EAD72F /* NSObject+InternationalizedValueForKey.m */; };
7981
/* End PBXBuildFile section */
8082

8183
/* Begin PBXContainerItemProxy section */
@@ -161,6 +163,8 @@
161163
5FC13E6C1A4E2EB3008A0FE3 /* FlashlightQueryEngine.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FlashlightQueryEngine.m; sourceTree = "<group>"; };
162164
5FC13E721A4E6B58008A0FE3 /* NSTask+FlashlightExtensions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSTask+FlashlightExtensions.h"; sourceTree = "<group>"; };
163165
5FC13E731A4E6B58008A0FE3 /* NSTask+FlashlightExtensions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSTask+FlashlightExtensions.m"; sourceTree = "<group>"; };
166+
5FF873101A6D718800EAD72F /* NSObject+InternationalizedValueForKey.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSObject+InternationalizedValueForKey.h"; sourceTree = "<group>"; };
167+
5FF873111A6D718800EAD72F /* NSObject+InternationalizedValueForKey.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSObject+InternationalizedValueForKey.m"; sourceTree = "<group>"; };
164168
/* End PBXFileReference section */
165169

166170
/* Begin PBXFrameworksBuildPhase section */
@@ -220,6 +224,8 @@
220224
5FC13E0A1A4E12FB008A0FE3 /* Parser */,
221225
5FC13E721A4E6B58008A0FE3 /* NSTask+FlashlightExtensions.h */,
222226
5FC13E731A4E6B58008A0FE3 /* NSTask+FlashlightExtensions.m */,
227+
5FF873101A6D718800EAD72F /* NSObject+InternationalizedValueForKey.h */,
228+
5FF873111A6D718800EAD72F /* NSObject+InternationalizedValueForKey.m */,
223229
5FC13E5F1A4E169E008A0FE3 /* FlashlightResult.h */,
224230
5FC13E601A4E169E008A0FE3 /* FlashlightResult.m */,
225231
5FC13E631A4E1722008A0FE3 /* FlashlightResultView.h */,
@@ -330,6 +336,7 @@
330336
5FC13E3C1A4E12FB008A0FE3 /* PSHelpers.h in Headers */,
331337
5FC13DF41A4E12DB008A0FE3 /* FlashlightKit.h in Headers */,
332338
5FC13E451A4E12FB008A0FE3 /* PSParsnipSource.h in Headers */,
339+
5FF873121A6D718800EAD72F /* NSObject+InternationalizedValueForKey.h in Headers */,
333340
5FC13E471A4E12FB008A0FE3 /* PSPluginDispatcher.h in Headers */,
334341
5FC13E491A4E12FB008A0FE3 /* PSPluginExampleSource.h in Headers */,
335342
5F870A5D1A546C0700161E2F /* PSTaggedText+ToJSON.h in Headers */,
@@ -475,6 +482,7 @@
475482
5FC13E4C1A4E12FB008A0FE3 /* PSProbabilityCounter.m in Sources */,
476483
5FC13E391A4E12FB008A0FE3 /* PSBackgroundProcessor.m in Sources */,
477484
5FC13E3B1A4E12FB008A0FE3 /* PSEndNode.m in Sources */,
485+
5FF873131A6D718800EAD72F /* NSObject+InternationalizedValueForKey.m in Sources */,
478486
5FC13E4E1A4E12FB008A0FE3 /* PSSpecialNode.m in Sources */,
479487
5FC13E3F1A4E12FB008A0FE3 /* PSMerging.m in Sources */,
480488
5FC13E541A4E12FB008A0FE3 /* PSTaggedText+ParseExample.m in Sources */,

FlashlightApp/FlashlightKit/FlashlightKit/FlashlightKit.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ FOUNDATION_EXPORT const unsigned char FlashlightKitVersionString[];
2323
#import <FlashlightKit/FlashlightResultView.h>
2424
#import <FlashlightKit/PSPluginExampleSource.h>
2525
#import <FlashlightKit/PSPluginDispatcher.h>
26+
#import <FlashlightKit/NSObject+InternationalizedValueForKey.h>

FlashlightApp/EasySIMBL/NSObject+InternationalizedValueForKey.h renamed to FlashlightApp/FlashlightKit/FlashlightKit/NSObject+InternationalizedValueForKey.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,10 @@
1313
- (id)internationalizedValueForKey:(NSString *)key;
1414

1515
@end
16+
17+
18+
@interface NSObject (Internationalization)
19+
20+
+ (void)enumerateLocalizedVariantsOfKey:(NSString *)key block:(void(^)(NSString *key, BOOL *stop))block;
21+
22+
@end

FlashlightApp/EasySIMBL/NSObject+InternationalizedValueForKey.m renamed to FlashlightApp/FlashlightKit/FlashlightKit/NSObject+InternationalizedValueForKey.m

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,36 @@
1111
@implementation NSDictionary (InternationalizedValueForKey)
1212

1313
- (id)internationalizedValueForKey:(NSString *)key {
14+
__block id val = nil;
15+
[[self class] enumerateLocalizedVariantsOfKey:key block:^(NSString *key, BOOL *stop) {
16+
val = self[key];
17+
*stop = !!val;
18+
}];
19+
return val;
20+
}
21+
22+
@end
23+
24+
@implementation NSObject (Internationalization)
25+
26+
+ (void)enumerateLocalizedVariantsOfKey:(NSString *)key block:(void(^)(NSString *key, BOOL *stop))block {
27+
BOOL stop = NO;
1428
for (NSString *fullLang in [NSLocale preferredLanguages]) {
29+
if (stop) return;
1530
NSString *lang = fullLang;
1631
while (1) {
1732
NSString *localKey = [lang isEqualToString:@"en"] ? key : [NSString stringWithFormat:@"%@_%@", key, lang];
18-
if ([self objectForKey:localKey]) {
19-
return [self objectForKey:localKey];
33+
block(localKey, &stop);
34+
if (stop) {
35+
break;
2036
} else if ([lang rangeOfString:@"-" options:NSBackwardsSearch].location != NSNotFound) {
2137
lang = [lang substringToIndex:[lang rangeOfString:@"-" options:NSBackwardsSearch].location];
2238
} else {
2339
break;
2440
}
2541
}
2642
}
27-
return [self objectForKey:key];
43+
if (!stop) block(key, &stop); // fallback to just the plain key
2844
}
2945

3046
@end

FlashlightApp/FlashlightKit/FlashlightKit/Parser/PSPluginExampleSource.m

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#import "PSHelpers.h"
1111
#import "PSTaggedText+ParseExample.h"
1212
#import "Parsnip.h"
13+
#import "NSObject+InternationalizedValueForKey.h"
1314

1415
NSString * const PSParsnipSourceDataPluginPathForIntentDictionaryKey = @"PSParsnipSourceDataPluginPathForIntentDictionaryKey";
1516

@@ -84,8 +85,7 @@ - (void)didChange {
8485
NSString *intentTag = [@"plugin_intent/" stringByAppendingString:name];
8586
pluginPathsForIntents[intentTag] = pluginPath;
8687

87-
NSString *examplesFilePath = [pluginPath stringByAppendingPathComponent:@"examples.txt"];
88-
for (NSString *line in [[[NSString alloc] initWithContentsOfFile:examplesFilePath usedEncoding:nil error:nil] componentsSeparatedByString:@"\n"]) {
88+
for (NSString *line in [self exampleLinesForPluginAtPath:pluginPath]) {
8989
NSString *trimmed = [line stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
9090
if (trimmed.length) {
9191
if ([trimmed.lowercaseString isEqualToString:@"!always_invoke"]) {
@@ -134,6 +134,26 @@ - (void)didChange {
134134
self.pathsOfPluginsToAlwaysInvoke = pathsForPluginsToAlwaysInvoke;
135135
}
136136

137+
- (NSArray *)exampleLinesForPluginAtPath:(NSString *)pluginPath {
138+
// load examples for first supported user-preferred language, plus english
139+
NSMutableSet *filePathsToRead = [NSMutableSet new];
140+
[[self class] enumerateLocalizedVariantsOfKey:@"examples" block:^(NSString *key, BOOL *stop) {
141+
NSString *filename = [key stringByAppendingPathExtension:@"txt"];
142+
NSString *fullPath = [pluginPath stringByAppendingPathComponent:filename];
143+
if ([[NSFileManager defaultManager] fileExistsAtPath:fullPath]) {
144+
[filePathsToRead addObject:fullPath];
145+
*stop = YES;
146+
}
147+
}];
148+
[filePathsToRead addObject:[pluginPath stringByAppendingPathComponent:@"examples.txt"]];
149+
return [filePathsToRead.allObjects flatMap:^NSArray *(id obj) {
150+
return [[[[NSString alloc] initWithContentsOfFile:obj encoding:NSUTF8StringEncoding error:nil] componentsSeparatedByString:@"\n"] mapFilter:^id(id obj) {
151+
NSString *line = [obj stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
152+
return line.length ? line : nil;
153+
}];
154+
}];
155+
}
156+
137157
- (void)dealloc {
138158
dispatch_source_cancel(self.dispatchSource);
139159
}

0 commit comments

Comments
 (0)