Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Localization update #40

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions Example/DMPasscode/DMPasscode-Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,7 @@
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>NSFaceIDUsageDescription</key>
<string>Authenticate to access locked feature</string>
</dict>
</plist>
2 changes: 1 addition & 1 deletion Pod/Assets/en.lproj/DMPasscodeLocalisation.strings
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
"dmpasscode_okay" = "Okay";
"dmpasscode_n_left" = "%i attempts left";
"dmpasscode_1_left" = "1 attempt left";
"dmpasscode_touchid_reason" = "Authenticate to access locked feature.";
"dmpasscode_touchid_reason" = "Authenticate to access locked feature.";
23 changes: 11 additions & 12 deletions Pod/Classes/DMPasscode.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@
#import <LocalAuthentication/LocalAuthentication.h>
#endif

#undef NSLocalizedString
#define NSLocalizedString(key, comment) \
[bundle localizedStringForKey:(key) value:@"" table:@"DMPasscodeLocalisation"]

static DMPasscode* instance;
static const NSString* KEYCHAIN_NAME = @"passcode";
static NSBundle* bundle;
Expand Down Expand Up @@ -90,7 +86,8 @@ - (void)showPasscodeInViewController:(UIViewController *)viewController completi
_completion = completion;
LAContext* context = [[LAContext alloc] init];
if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:nil]) {
[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:NSLocalizedString(@"dmpasscode_touchid_reason", nil) reply:^(BOOL success, NSError* error) {
NSBundle *bundle = [NSBundle bundleWithPath:[[NSBundle bundleForClass: [DMPasscode class]] pathForResource:@"DMPasscode" ofType:@"bundle"]];
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not of fan of getting the NSBundle this way, however when I attempted to get it the more clear(?) way, (NSBundle *bundle = [NSBundle bundleForClass: [DMPasscode class]];), the localized string lookup did not work.
This approach was found over on Medium.

[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:NSLocalizedStringFromTableInBundle(@"dmpasscode_touchid_reason", @"DMPasscodeLocalisation", bundle, nil) reply:^(BOOL success, NSError* error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (error) {
switch (error.code) {
Expand Down Expand Up @@ -142,10 +139,11 @@ - (void)openPasscodeWithMode:(int)mode viewController:(UIViewController *)viewCo
DMPasscodeInternalNavigationController* nc = [[DMPasscodeInternalNavigationController alloc] initWithRootViewController:_passcodeViewController];
[nc setModalPresentationStyle:UIModalPresentationFormSheet];
[viewController presentViewController:nc animated:YES completion:nil];
NSBundle *bundle = [NSBundle bundleWithPath:[[NSBundle bundleForClass: [DMPasscode class]] pathForResource:@"DMPasscode" ofType:@"bundle"]];
if (_mode == 0) {
[_passcodeViewController setInstructions:NSLocalizedString(@"dmpasscode_enter_new_code", nil)];
[_passcodeViewController setInstructions:NSLocalizedStringFromTableInBundle(@"dmpasscode_enter_new_code", @"DMPasscodeLocalisation", bundle, nil)];
} else if (_mode == 1) {
[_passcodeViewController setInstructions:NSLocalizedString(@"dmpasscode_enter_to_unlock", nil)];
[_passcodeViewController setInstructions:NSLocalizedStringFromTableInBundle(@"dmpasscode_enter_to_unlock", @"DMPasscodeLocalisation", bundle, nil)];
}
}

Expand All @@ -157,19 +155,20 @@ - (void)closeAndNotify:(BOOL)success withError:(NSError *)error {

#pragma mark - DMPasscodeInternalViewControllerDelegate
- (void)enteredCode:(NSString *)code {
NSBundle *bundle = [NSBundle bundleWithPath:[[NSBundle bundleForClass: [DMPasscode class]] pathForResource:@"DMPasscode" ofType:@"bundle"]];
if (_mode == 0) {
if (_count == 0) {
_prevCode = code;
[_passcodeViewController setInstructions:NSLocalizedString(@"dmpasscode_repeat", nil)];
[_passcodeViewController setInstructions:NSLocalizedStringFromTableInBundle(@"dmpasscode_repeat", @"DMPasscodeLocalisation", bundle, nil)];
[_passcodeViewController setErrorMessage:@""];
[_passcodeViewController reset];
} else if (_count == 1) {
if ([code isEqualToString:_prevCode]) {
[[DMKeychain defaultKeychain] setObject:code forKey:KEYCHAIN_NAME];
[self closeAndNotify:YES withError:nil];
} else {
[_passcodeViewController setInstructions:NSLocalizedString(@"dmpasscode_enter_new_code", nil)];
[_passcodeViewController setErrorMessage:NSLocalizedString(@"dmpasscode_not_match", nil)];
[_passcodeViewController setInstructions:NSLocalizedStringFromTableInBundle(@"dmpasscode_enter_new_code", @"DMPasscodeLocalisation", bundle, nil)];
[_passcodeViewController setErrorMessage:NSLocalizedStringFromTableInBundle(@"dmpasscode_not_match", @"DMPasscodeLocalisation", bundle, nil)];
[_passcodeViewController reset];
_count = 0;
return;
Expand All @@ -180,9 +179,9 @@ - (void)enteredCode:(NSString *)code {
[self closeAndNotify:YES withError:nil];
} else {
if (_count == 1) {
[_passcodeViewController setErrorMessage:NSLocalizedString(@"dmpasscode_1_left", nil)];
[_passcodeViewController setErrorMessage:NSLocalizedStringFromTableInBundle(@"dmpasscode_1_left", @"DMPasscodeLocalisation", bundle, nil)];
} else {
[_passcodeViewController setErrorMessage:[NSString stringWithFormat:NSLocalizedString(@"dmpasscode_n_left", nil), 2 - _count]];
[_passcodeViewController setErrorMessage:[NSString stringWithFormat:NSLocalizedStringFromTableInBundle(@"dmpasscode_n_left", @"DMPasscodeLocalisation", bundle, nil), 2 - _count]];
}
[_passcodeViewController reset];
if (_count >= 2) { // max 3 attempts
Expand Down
7 changes: 7 additions & 0 deletions en.lproj/DMPasscodeLocalisation.strings
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
DMPasscodeLocalisation.strings
Pods

Created by Mike Stanziano on 8/26/19.

*/