Skip to content
This repository has been archived by the owner on Oct 6, 2024. It is now read-only.

Commit

Permalink
feat: Extendable localization string keys
Browse files Browse the repository at this point in the history
  • Loading branch information
breautek committed Sep 4, 2023
1 parent 7b57c94 commit db471ea
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 10 deletions.
8 changes: 8 additions & 0 deletions NBSFuse/NBSFuse.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
9489EBA32AA621A70087424D /* NBSFuseViewController.h in Headers */ = {isa = PBXBuildFile; fileRef = 9489EBA22AA621A70087424D /* NBSFuseViewController.h */; settings = {ATTRIBUTES = (Public, ); }; };
9489EBA52AA623B70087424D /* NBSFuseViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 9489EBA42AA623B70087424D /* NBSFuseViewController.m */; };
9489EBAE2AA6445C0087424D /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = 9489EBB02AA6445C0087424D /* Localizable.strings */; };
9489EBB82AA659D70087424D /* NBSFuseLocalization.h in Headers */ = {isa = PBXBuildFile; fileRef = 9489EBB72AA659D70087424D /* NBSFuseLocalization.h */; settings = {ATTRIBUTES = (Public, ); }; };
9489EBBA2AA65A990087424D /* NBSFuseLocalization.m in Sources */ = {isa = PBXBuildFile; fileRef = 9489EBB92AA65A990087424D /* NBSFuseLocalization.m */; };
94A886B72A714F6D0098530D /* NBSFuse.h in Headers */ = {isa = PBXBuildFile; fileRef = 94A8865C2A702C6C0098530D /* NBSFuse.h */; settings = {ATTRIBUTES = (Public, ); }; };
94A886B82A714F6D0098530D /* NBSFuseContext.h in Headers */ = {isa = PBXBuildFile; fileRef = 94A886632A702D240098530D /* NBSFuseContext.h */; settings = {ATTRIBUTES = (Public, ); }; };
94A886B92A714F6D0098530D /* NBSFuseSchemeHandler.h in Headers */ = {isa = PBXBuildFile; fileRef = 94A886682A702F020098530D /* NBSFuseSchemeHandler.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand All @@ -47,6 +49,8 @@
9489EBA22AA621A70087424D /* NBSFuseViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = NBSFuseViewController.h; sourceTree = "<group>"; };
9489EBA42AA623B70087424D /* NBSFuseViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = NBSFuseViewController.m; sourceTree = "<group>"; };
9489EBAF2AA6445C0087424D /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/Localizable.strings; sourceTree = "<group>"; };
9489EBB72AA659D70087424D /* NBSFuseLocalization.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = NBSFuseLocalization.h; sourceTree = "<group>"; };
9489EBB92AA65A990087424D /* NBSFuseLocalization.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = NBSFuseLocalization.m; sourceTree = "<group>"; };
94A8865C2A702C6C0098530D /* NBSFuse.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = NBSFuse.h; sourceTree = "<group>"; };
94A886632A702D240098530D /* NBSFuseContext.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = NBSFuseContext.h; sourceTree = "<group>"; };
94A886662A702D6A0098530D /* NBSFuseContext.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = NBSFuseContext.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -125,6 +129,7 @@
941862AA2A9FE5F700CCFEE1 /* NBSFuseError.h */,
9489EB9E2AA61A1C0087424D /* NBSFuseWebviewUIDelegation.h */,
9489EBA22AA621A70087424D /* NBSFuseViewController.h */,
9489EBB72AA659D70087424D /* NBSFuseLocalization.h */,
);
path = NBSFuse;
sourceTree = "<group>";
Expand All @@ -142,6 +147,7 @@
941862AC2A9FE65E00CCFEE1 /* NBSFuseError.m */,
9489EBA02AA61A770087424D /* NBSFuseWebviewUIDelegation.m */,
9489EBA42AA623B70087424D /* NBSFuseViewController.m */,
9489EBB92AA65A990087424D /* NBSFuseLocalization.m */,
);
path = src;
sourceTree = "<group>";
Expand All @@ -166,6 +172,7 @@
9489EBA32AA621A70087424D /* NBSFuseViewController.h in Headers */,
9418629B2A99564600CCFEE1 /* NBSFuseAPIRouter.h in Headers */,
94A886B92A714F6D0098530D /* NBSFuseSchemeHandler.h in Headers */,
9489EBB82AA659D70087424D /* NBSFuseLocalization.h in Headers */,
941862AB2A9FE5F700CCFEE1 /* NBSFuseError.h in Headers */,
9489EB982AA54FA40087424D /* NBSFuseRuntime.h in Headers */,
9418629F2A995BDC00CCFEE1 /* NBSFuseAPIPacket.h in Headers */,
Expand Down Expand Up @@ -247,6 +254,7 @@
9489EB9A2AA54FED0087424D /* NBSFuseRuntime.m in Sources */,
9489EBA52AA623B70087424D /* NBSFuseViewController.m in Sources */,
941862A52A996F7900CCFEE1 /* NBSFuseAPIResponse.m in Sources */,
9489EBBA2AA65A990087424D /* NBSFuseLocalization.m in Sources */,
94A886BC2A714F6D0098530D /* NBSFuseContext.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
1 change: 1 addition & 0 deletions NBSFuse/NBSFuse/NBSFuse.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ FOUNDATION_EXPORT const unsigned char NBSFuseVersionString[];
#import <NBSFuse/NBSFuseError.h>
#import <NBSFuse/NBSFuseWebviewUIDelegation.h>
#import <NBSFuse/NBSFuseViewController.h>
#import <NBSFuse/NBSFuseLocalization.h>

// Core Plugins
#import <NBSFuse/NBSFuseRuntime.h>
34 changes: 34 additions & 0 deletions NBSFuse/NBSFuse/NBSFuseLocalization.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

/*
Copyright 2023 Norman Breau
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#ifndef NBSFuseLocalization_h
#define NBSFuseLocalization_h

/**
A static class that will look for strings in the mainBundle, and fallback to the framework bundle.
This offers a chance for the application to override UI text that the framework produces.
*/
@interface NBSFuseLocalization: NSObject

- (instancetype) init NS_UNAVAILABLE;

+ (NSString*) lookup:(NSString*) key;

@end


#endif /* NBSFuseLocalization_h */
42 changes: 42 additions & 0 deletions NBSFuse/src/NBSFuseLocalization.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

/*
Copyright 2023 Norman Breau
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#import <Foundation/Foundation.h>
#import <NBSFuseLocalization.h>

@implementation NBSFuseLocalization : NSObject

- (instancetype) init {
@throw [NSException exceptionWithName:@"CannotInstantiateException" reason:@"NBSFuseLocalization is a static class" userInfo: nil];
}

+ (NSString*) lookup: (NSString*) key {
NSBundle* mainBundle = [NSBundle mainBundle];
NSBundle* bundle = [NSBundle bundleForClass: [NBSFuseLocalization class]];

NSString* str = NSLocalizedStringWithDefaultValue(key, nil, mainBundle, nil, nil);

if (str == nil) {
// If we can't resolve the key, then default the text to the key. Make the error obvious in the UI so that it can
// be corrected.
str = NSLocalizedStringWithDefaultValue(key, nil, bundle, key, nil);
}

return str;
}

@end
16 changes: 6 additions & 10 deletions NBSFuse/src/NBSFuseWebviewUIDelegation.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#import <Foundation/Foundation.h>
#import <NBSFuseWebviewUIDelegation.h>
#import <NBSFuse/NBSFuseLocalization.h>

@implementation NBSFuseWebviewUIDelegation

Expand All @@ -15,7 +16,6 @@ - (void) webView:(WKWebView*) webView
initiatedByFrame:(WKFrameInfo*) frame
completionHandler:(void (^)(void)) completionHandler
{
NSBundle* bundle = [NSBundle bundleForClass: [NBSFuseWebviewUIDelegation class]];

UIAlertController* alertController = [ UIAlertController
alertControllerWithTitle: nil
Expand All @@ -27,7 +27,7 @@ - (void) webView:(WKWebView*) webView
alertController
addAction: [
UIAlertAction
actionWithTitle: [bundle localizedStringForKey:@"FUSE_DIALOG_OK_BUTTON_LABEL" value:@"" table: nil]
actionWithTitle: [NBSFuseLocalization lookup:@"FUSE_DIALOG_OK_BUTTON_LABEL"]
style: UIAlertActionStyleDefault
handler: ^(UIAlertAction * _Nonnull action) {
completionHandler();
Expand All @@ -43,8 +43,6 @@ - (void) webView:(WKWebView*) webView
initiatedByFrame:(WKFrameInfo*) frame
completionHandler:(void (^)(BOOL)) completionHandler
{
NSBundle* bundle = [NSBundle bundleForClass: [NBSFuseWebviewUIDelegation class]];

UIAlertController* alertController = [
UIAlertController
alertControllerWithTitle: nil
Expand All @@ -56,7 +54,7 @@ - (void) webView:(WKWebView*) webView
alertController
addAction: [
UIAlertAction
actionWithTitle: [bundle localizedStringForKey:@"FUSE_DIALOG_CANCEL_BUTTON_LABEL" value:@"" table: nil]
actionWithTitle: [NBSFuseLocalization lookup:@"FUSE_DIALOG_CANCEL_BUTTON_LABEL"]
style: UIAlertActionStyleDefault
handler: ^(UIAlertAction* _Nonnull action) {
completionHandler(false);
Expand All @@ -68,7 +66,7 @@ - (void) webView:(WKWebView*) webView
alertController
addAction: [
UIAlertAction
actionWithTitle: [bundle localizedStringForKey:@"FUSE_DIALOG_OK_BUTTON_LABEL" value:@"" table: nil]
actionWithTitle: [NBSFuseLocalization lookup:@"FUSE_DIALOG_OK_BUTTON_LABEL"]
style: UIAlertActionStyleDefault
handler: ^(UIAlertAction* _Nonnull action) {
completionHandler(true);
Expand All @@ -85,8 +83,6 @@ - (void) webView:(WKWebView*) webView
initiatedByFrame:(WKFrameInfo*) frame
completionHandler:(void (^)(NSString* _Nullable)) completionHandler
{
NSBundle* bundle = [NSBundle bundleForClass: [NBSFuseWebviewUIDelegation class]];

UIAlertController* alertController = [
UIAlertController alertControllerWithTitle: nil
message: prompt
Expand All @@ -104,7 +100,7 @@ - (void) webView:(WKWebView*) webView
alertController
addAction: [
UIAlertAction
actionWithTitle: [bundle localizedStringForKey:@"FUSE_DIALOG_CANCEL_BUTTON_LABEL " value:@"" table: nil]
actionWithTitle: [NBSFuseLocalization lookup:@"FUSE_DIALOG_CANCEL_BUTTON_LABEL"]
style: UIAlertActionStyleDefault
handler: ^(UIAlertAction * _Nonnull action) {
completionHandler(nil);
Expand All @@ -116,7 +112,7 @@ - (void) webView:(WKWebView*) webView
alertController
addAction: [
UIAlertAction
actionWithTitle: [bundle localizedStringForKey:@"FUSE_DIALOG_OK_BUTTON_LABEL" value:@"" table: nil]
actionWithTitle: [NBSFuseLocalization lookup:@"FUSE_DIALOG_OK_BUTTON_LABEL"]
style: UIAlertActionStyleDefault
handler: ^(UIAlertAction * _Nonnull action) {
completionHandler(alertController.textFields.firstObject.text);
Expand Down

0 comments on commit db471ea

Please sign in to comment.