Skip to content

Commit f2ab098

Browse files
mvp
1 parent ed07acc commit f2ab098

File tree

12 files changed

+89
-16
lines changed

12 files changed

+89
-16
lines changed

Example/Localization/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ localize(
66
"en.lproj/Localizable.strings",
77
"es.lproj/Localizable.strings",
88
],
9+
target_name = "example_Source_app_Sources",
910
visibility = [
1011
"//Example/Source:__pkg__",
1112
],

Example/Source/App.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
@implementation App
55

66
+ (NSString *)fetchLocalizationValueForKey:(NSString *)key {
7-
return SSTStringForKeyWithBundleAndSubdirectory(key, [NSBundle bundleForClass:[App class]], nil);
7+
return SSTStringForKeyWithBundleAndSubdirectoryAndTargetName(key, [NSBundle bundleForClass:[self class]], nil, @"example_Source_app_Sources");
88
}
99

1010
@end

Example/Source/AppDelegate.m

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ @implementation AppDelegate
55

66
- (BOOL)application:(UIApplication *)application
77
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
8-
NSString *key1 = @"string1";
9-
NSLog(@"Found %@ for key %@", SSTStringForKeyWithBundleAndSubdirectory(key1, [NSBundle mainBundle], nil), key1);
108
return YES;
119
}
1210

Example/Source/Module/BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ objc_library(
88
srcs = ["Module.m"],
99
hdrs = ["Module.h"],
1010
data = [
11-
"//Example/Source/Module/Localization:localize_module_test",
11+
"//Example/Source/Module/Localization:localizations",
1212
],
1313
deps = [
1414
"//:SSTSmallStrings",

Example/Source/Module/Localization/BUILD

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,19 @@ localize(
66
"en.lproj/Localizable.strings",
77
"es.lproj/Localizable.strings",
88
],
9+
target_name = "example_Source_Module_module",
10+
visibility = [
11+
"//Example/Source/Module:__pkg__",
12+
],
13+
)
14+
15+
filegroup(
16+
name = "localizations",
17+
srcs = [
18+
"en.lproj/Localizable.stringsdict",
19+
"es.lproj/Localizable.stringsdict",
20+
":localize_module_test",
21+
],
922
visibility = [
1023
"//Example/Source/Module:__pkg__",
1124
],
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>string1_stringdict</key>
6+
<dict>
7+
<key>NSStringLocalizedFormatKey</key>
8+
<string>string1_stringdict_en %#@formattedValue@</string>
9+
<key>formattedValue</key>
10+
<dict>
11+
<key>NSStringFormatSpecTypeKey</key>
12+
<string>NSStringPluralRuleType</string>
13+
<key>NSStringFormatValueTypeKey</key>
14+
<string>zd</string>
15+
<key>one</key>
16+
<string>%1$zd thing</string>
17+
<key>other</key>
18+
<string>%1$zd things</string>
19+
</dict>
20+
</dict>
21+
</dict>
22+
</plist>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>string1_stringdict</key>
6+
<dict>
7+
<key>NSStringLocalizedFormatKey</key>
8+
<string>string1_stringdict_es %#@formattedValue@</string>
9+
<key>formattedValue</key>
10+
<dict>
11+
<key>NSStringFormatSpecTypeKey</key>
12+
<string>NSStringPluralRuleType</string>
13+
<key>NSStringFormatValueTypeKey</key>
14+
<string>zd</string>
15+
<key>one</key>
16+
<string>%1$zd thing</string>
17+
<key>other</key>
18+
<string>%1$zd things</string>
19+
</dict>
20+
</dict>
21+
</dict>
22+
</plist>

Example/Source/Module/Module.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
@implementation Module
55

66
+ (NSString *)fetchLocalizationValueForKey:(NSString *)key {
7-
return SSTStringForKeyWithBundleAndSubdirectory(key, [NSBundle bundleForClass:[Module class]], nil);
7+
return SSTStringForKeyWithBundleAndSubdirectoryAndTargetName(key, [NSBundle bundleForClass:[self class]], nil, @"example_Source_Module_module");
88
}
99

1010
@end

Source/SSTSmallStrings.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
NSString *SSTStringForKey(NSString *key);
44
NSString *SSTStringForKeyWithBundle(NSString *key, NSBundle *bundle);
55
NSString *SSTStringForKeyWithBundleAndSubdirectory(NSString *key, NSBundle *bundle, NSString *subdirectory);
6+
NSString *SSTStringForKeyWithBundleAndSubdirectoryAndTargetName(NSString *key, NSBundle *bundle, NSString *subdirectory, NSString *targetName);

Source/SSTSmallStrings.m

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,22 @@ id SSTJsonForName(NSString *name, NSBundle *bundle, NSString *subdirectory)
2525
return [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
2626
}
2727

28-
NSDictionary <NSString *, NSString *> *SSTCreateKeyToString(NSBundle *bundle, NSString *subdirectory)
28+
NSDictionary <NSString *, NSString *> *SSTCreateKeyToString(NSBundle *bundle, NSString *subdirectory, NSString *targetName)
2929
{
3030
// Note that the preferred list does seem to at least include the development region as a fallback if there aren't
3131
// any other languages
3232
NSString *bestLocalization = [[bundle preferredLocalizations] firstObject] ?: [bundle developmentLocalization];
3333
if (!bestLocalization) {
3434
return @{};
3535
}
36-
NSString *valuesPath = [NSString stringWithFormat:@"%@.values.json.lzfse", bestLocalization];
36+
NSString *targetNamePrefix = @"";
37+
if (targetName) {
38+
targetNamePrefix = [NSString stringWithFormat:@"%@.", targetName];
39+
}
40+
NSString *valuesPath = [NSString stringWithFormat:@"%@%@.values.json.lzfse", targetNamePrefix, bestLocalization];
3741
NSArray <id> *values = SSTJsonForName(valuesPath, bundle, subdirectory);
3842

39-
NSArray <NSString *> *keys = SSTJsonForName(@"keys.json.lzfse", bundle, subdirectory);
43+
NSArray <NSString *> *keys = SSTJsonForName([NSString stringWithFormat:@"%@keys.json.lzfse", targetNamePrefix], bundle, subdirectory);
4044

4145
NSMutableDictionary <NSString *, NSString *> *keyToString = [NSMutableDictionary dictionaryWithCapacity:keys.count];
4246
NSInteger count = keys.count;
@@ -51,19 +55,24 @@ id SSTJsonForName(NSString *name, NSBundle *bundle, NSString *subdirectory)
5155
return keyToString; // Avoid -copy to be a bit faster
5256
}
5357

54-
NSString *SSTStringForKeyWithBundleAndSubdirectory(NSString *key, NSBundle *bundle, NSString *subdirectory) {
58+
NSString *SSTStringForKeyWithBundleAndSubdirectoryAndTargetName(NSString *key, NSBundle *bundle, NSString *subdirectory, NSString *targetName) {
5559
static dispatch_once_t onceToken;
5660
dispatch_once(&onceToken, ^{
57-
sKeyToString = SSTCreateKeyToString(bundle, subdirectory);
61+
sKeyToString = SSTCreateKeyToString(bundle, subdirectory, targetName);
5862
});
5963
// Haven't tested with CFBundleAllowMixedLocalizations set to YES, although it seems like that'd be handled by the
6064
// NSLocalizedString fallback
6165
return sKeyToString[key] ?: NSLocalizedString(key, @"");
6266
}
6367

68+
NSString *SSTStringForKeyWithBundleAndSubdirectory(NSString *key, NSBundle *bundle, NSString *subdirectory)
69+
{
70+
return SSTStringForKeyWithBundleAndSubdirectoryAndTargetName(key, bundle, subdirectory, nil);
71+
}
72+
6473
NSString *SSTStringForKeyWithBundle(NSString *key, NSBundle *bundle)
6574
{
66-
return SSTStringForKeyWithBundleAndSubdirectory(key, bundle, @"localization");
75+
return SSTStringForKeyWithBundleAndSubdirectoryAndTargetName(key, bundle, @"localization", nil);
6776
}
6877

6978
NSString *SSTStringForKey(NSString *key)

0 commit comments

Comments
 (0)