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

Commit

Permalink
Add Bidi utilities (#42)
Browse files Browse the repository at this point in the history
Initial import of Bidi utility functions.
  • Loading branch information
ianegordon committed Jul 30, 2018
1 parent f71a090 commit 340899b
Show file tree
Hide file tree
Showing 19 changed files with 753 additions and 14 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,4 @@ script:
- env
- set -o pipefail
- echo xcodebuild -project "$TRAVIS_XCODE_PROJECT" -scheme "$TRAVIS_XCODE_SCHEME" -sdk "$SDK" -destination "$DESTINATION" test
- xcodebuild -project "$TRAVIS_XCODE_PROJECT" -scheme "$TRAVIS_XCODE_SCHEME" -sdk "$SDK" -destination "$DESTINATION" test | xcpretty

- xcodebuild -project "$TRAVIS_XCODE_PROJECT" -scheme "$TRAVIS_XCODE_SCHEME" -sdk "$SDK" -destination "$DESTINATION" test | xcpretty
6 changes: 6 additions & 0 deletions Examples/Flags/Flags.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
40CFE6791D82047100745882 /* Countries.strings in Resources */ = {isa = PBXBuildFile; fileRef = 40CFE67B1D82047100745882 /* Countries.strings */; };
40CFE67E1D820EC800745882 /* Flags.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 40CFE67D1D820EC800745882 /* Flags.xcassets */; };
40E3986B1D9DAFAB007F01DE /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 40E3986D1D9DAFAB007F01DE /* InfoPlist.strings */; };
40F9EF0E20DC5FB300F761C2 /* BidirectionalViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 40F9EF0D20DC5FB300F761C2 /* BidirectionalViewController.m */; };
40FE8A9B1D9AC719000A2A57 /* InformationViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 40FE8A9A1D9AC719000A2A57 /* InformationViewController.m */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -83,6 +84,8 @@
40E3986C1D9DAFAB007F01DE /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = "<group>"; };
40E3986E1D9DAFAD007F01DE /* ar */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = ar; path = ar.lproj/InfoPlist.strings; sourceTree = "<group>"; };
40E3A2551DBEC54700C8B4CC /* ar */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = ar; path = ar.lproj/Main.strings; sourceTree = "<group>"; };
40F9EF0C20DC5FB300F761C2 /* BidirectionalViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = BidirectionalViewController.h; sourceTree = "<group>"; };
40F9EF0D20DC5FB300F761C2 /* BidirectionalViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = BidirectionalViewController.m; sourceTree = "<group>"; };
40FE8A991D9AC719000A2A57 /* InformationViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = InformationViewController.h; sourceTree = "<group>"; };
40FE8A9A1D9AC719000A2A57 /* InformationViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = InformationViewController.m; sourceTree = "<group>"; };
/* End PBXFileReference section */
Expand Down Expand Up @@ -131,6 +134,8 @@
children = (
4096E62C1D78ED7600389ECD /* AppDelegate.h */,
4096E62D1D78ED7600389ECD /* AppDelegate.m */,
40F9EF0C20DC5FB300F761C2 /* BidirectionalViewController.h */,
40F9EF0D20DC5FB300F761C2 /* BidirectionalViewController.m */,
40CFE6711D82017200745882 /* CountryCell.h */,
40CFE6721D82017200745882 /* CountryCell.m */,
40CFE6741D82018D00745882 /* CountryTableViewController.h */,
Expand Down Expand Up @@ -288,6 +293,7 @@
40CFE6731D82017200745882 /* CountryCell.m in Sources */,
4096E62E1D78ED7600389ECD /* AppDelegate.m in Sources */,
40CFE6761D82018D00745882 /* CountryTableViewController.m in Sources */,
40F9EF0E20DC5FB300F761C2 /* BidirectionalViewController.m in Sources */,
4096E62B1D78ED7600389ECD /* main.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "baseline_compare_arrows_black_36pt_1x.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "baseline_compare_arrows_black_36pt_2x.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "baseline_compare_arrows_black_36pt_3x.png",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
119 changes: 109 additions & 10 deletions Examples/Flags/MDFInternationalizationExample/Base.lproj/Main.storyboard

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
Copyright 2018-present Google Inc. All Rights Reserved.
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 <UIKit/UIKit.h>

@interface BidirectionalViewController : UIViewController

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
Copyright 2018-present Google Inc. All Rights Reserved.
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 "BidirectionalViewController.h"


static NSString *kMDFLTREmbedding = @"\u202a"; // left-to-right embedding
static NSString *kMDFRTLEmbedding = @"\u202b"; // right-to-left embedding
static NSString *kMDFBidiPopEmbedding = @"\u202c"; // pop directional embedding

static NSString *kMDFLTRMark = @"\u200e"; // left-to-right mark
static NSString *kMDFRTLMark = @"\u200f"; // right-to-left mark

// The following only work on iOS 10+
static NSString *kMDFLTRIsolate = @"\u2066"; // left-to-right isolate
static NSString *kMDFRTLIsolate = @"\u2067"; // right-to-left isolate
static NSString *kMDFFirstStrongIsolate = @"\u2068"; // first strong isolate
static NSString *kMDFPopIsolate = @"\u2069"; // pop directional isolate


@interface BidirectionalViewController ()

@property (weak, nonatomic) IBOutlet UILabel *labelOne;
@property (weak, nonatomic) IBOutlet UILabel *labelTwo;
@property (weak, nonatomic) IBOutlet UILabel *labelThree;
@property (weak, nonatomic) IBOutlet UILabel *labelFour;

@end

@implementation BidirectionalViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// The following lines will display a (: if the markers are supported, ): if not.
self.labelOne.text =
[NSString stringWithFormat:@"Bidi Isolate supported %@)%@:", kMDFRTLIsolate, kMDFPopIsolate];
self.labelTwo.text =
[NSString stringWithFormat:@"Bidi Embed supported %@)%@:", kMDFRTLEmbedding, kMDFBidiPopEmbedding];
// self.labelThree.text = @"1st a-\u2068.)\u200f\u2069-b";
// self.labelFour.text = @"1st a-\u2068\u200f.)\u2069-b";
// self.labelFour.text = @"الو" // Hello
NSString *three = [NSString stringWithFormat:@"Read %@15 books%@ EOL", kMDFFirstStrongIsolate, kMDFPopIsolate];
self.labelThree.text = three;
NSString *four = [NSString stringWithFormat:@"Read %@15 كتاب%@ EOL", kMDFFirstStrongIsolate, kMDFPopIsolate];
self.labelFour.text = four;
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/

@end
20 changes: 20 additions & 0 deletions MDFInternationalization.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
objects = {

/* Begin PBXBuildFile section */
4008C247201B6A3D00F089DB /* MDFBidiTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 4008C246201B6A3D00F089DB /* MDFBidiTests.m */; };
404B29FF20F5631800FA5DB0 /* NSLocale+MaterialRTL.m in Sources */ = {isa = PBXBuildFile; fileRef = 404B29FD20F5631800FA5DB0 /* NSLocale+MaterialRTL.m */; };
404B2A0020F5631800FA5DB0 /* NSLocale+MaterialRTL.h in Headers */ = {isa = PBXBuildFile; fileRef = 404B29FE20F5631800FA5DB0 /* NSLocale+MaterialRTL.h */; settings = {ATTRIBUTES = (Public, ); }; };
405E9FD41FFC894A005FA14D /* README.md in Resources */ = {isa = PBXBuildFile; fileRef = 405E9FD31FFC894A005FA14D /* README.md */; };
407431D5201A67000031D58A /* NSString+MaterialBidi.h in Headers */ = {isa = PBXBuildFile; fileRef = 407431D3201A67000031D58A /* NSString+MaterialBidi.h */; settings = {ATTRIBUTES = (Public, ); }; };
407431D6201A67000031D58A /* NSString+MaterialBidi.m in Sources */ = {isa = PBXBuildFile; fileRef = 407431D4201A67000031D58A /* NSString+MaterialBidi.m */; };
4096E5BA1D78CB5500389ECD /* MDFInternationalization.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4096E5AF1D78CB5500389ECD /* MDFInternationalization.framework */; };
4096E5D51D78CBD400389ECD /* MDFInternationalization.h in Headers */ = {isa = PBXBuildFile; fileRef = 4096E5CC1D78CBD400389ECD /* MDFInternationalization.h */; settings = {ATTRIBUTES = (Public, ); }; };
4096E5D71D78CBD400389ECD /* MDFRTL.h in Headers */ = {isa = PBXBuildFile; fileRef = 4096E5CE1D78CBD400389ECD /* MDFRTL.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand All @@ -31,7 +36,12 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
4008C246201B6A3D00F089DB /* MDFBidiTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = MDFBidiTests.m; path = Tests/MDFBidiTests.m; sourceTree = "<group>"; };
404B29FD20F5631800FA5DB0 /* NSLocale+MaterialRTL.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "NSLocale+MaterialRTL.m"; path = "Sources/NSLocale+MaterialRTL.m"; sourceTree = "<group>"; };
404B29FE20F5631800FA5DB0 /* NSLocale+MaterialRTL.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "NSLocale+MaterialRTL.h"; path = "Sources/NSLocale+MaterialRTL.h"; sourceTree = "<group>"; };
405E9FD31FFC894A005FA14D /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
407431D3201A67000031D58A /* NSString+MaterialBidi.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = "NSString+MaterialBidi.h"; path = "Sources/NSString+MaterialBidi.h"; sourceTree = "<group>"; };
407431D4201A67000031D58A /* NSString+MaterialBidi.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = "NSString+MaterialBidi.m"; path = "Sources/NSString+MaterialBidi.m"; sourceTree = "<group>"; };
4096E5AF1D78CB5500389ECD /* MDFInternationalization.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = MDFInternationalization.framework; sourceTree = BUILT_PRODUCTS_DIR; };
4096E5B91D78CB5500389ECD /* MDFInternationalizationTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = MDFInternationalizationTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
4096E5CB1D78CBD400389ECD /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = Info.plist; path = Sources/Info.plist; sourceTree = "<group>"; };
Expand Down Expand Up @@ -93,6 +103,10 @@
4096E5CC1D78CBD400389ECD /* MDFInternationalization.h */,
4096E5CE1D78CBD400389ECD /* MDFRTL.h */,
4096E5CF1D78CBD400389ECD /* MDFRTL.m */,
404B29FE20F5631800FA5DB0 /* NSLocale+MaterialRTL.h */,
404B29FD20F5631800FA5DB0 /* NSLocale+MaterialRTL.m */,
407431D3201A67000031D58A /* NSString+MaterialBidi.h */,
407431D4201A67000031D58A /* NSString+MaterialBidi.m */,
4096E5D01D78CBD400389ECD /* UIImage+MaterialRTL.h */,
4096E5D11D78CBD400389ECD /* UIImage+MaterialRTL.m */,
4096E5D21D78CBD400389ECD /* UIView+MaterialRTL.h */,
Expand All @@ -108,6 +122,7 @@
4096E5DD1D78CBE700389ECD /* Info.plist */,
4096E5DE1D78CBE700389ECD /* MDFInternationalizationTests.m */,
4096E5E51D78E02600389ECD /* MDFRTLTests.m */,
4008C246201B6A3D00F089DB /* MDFBidiTests.m */,
);
name = Tests;
sourceTree = "<group>";
Expand Down Expand Up @@ -137,6 +152,8 @@
files = (
4096E5D71D78CBD400389ECD /* MDFRTL.h in Headers */,
4096E5DB1D78CBD400389ECD /* UIView+MaterialRTL.h in Headers */,
404B2A0020F5631800FA5DB0 /* NSLocale+MaterialRTL.h in Headers */,
407431D5201A67000031D58A /* NSString+MaterialBidi.h in Headers */,
4096E5D91D78CBD400389ECD /* UIImage+MaterialRTL.h in Headers */,
4096E5D51D78CBD400389ECD /* MDFInternationalization.h in Headers */,
);
Expand Down Expand Up @@ -239,6 +256,8 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
404B29FF20F5631800FA5DB0 /* NSLocale+MaterialRTL.m in Sources */,
407431D6201A67000031D58A /* NSString+MaterialBidi.m in Sources */,
4096E5D81D78CBD400389ECD /* MDFRTL.m in Sources */,
4096E5DA1D78CBD400389ECD /* UIImage+MaterialRTL.m in Sources */,
4096E5DC1D78CBD400389ECD /* UIView+MaterialRTL.m in Sources */,
Expand All @@ -251,6 +270,7 @@
files = (
4096E5E61D78E02600389ECD /* MDFRTLTests.m in Sources */,
4096E5E11D78CC0100389ECD /* MDFInternationalizationTests.m in Sources */,
4008C247201B6A3D00F089DB /* MDFBidiTests.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
2 changes: 2 additions & 0 deletions Sources/MDFInternationalization.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#import <UIKit/UIKit.h>

#import <MDFInternationalization/MDFRTL.h>
#import <MDFInternationalization/NSLocale+MaterialRTL.h>
#import <MDFInternationalization/NSString+MaterialBidi.h>
#import <MDFInternationalization/UIImage+MaterialRTL.h>
#import <MDFInternationalization/UIView+MaterialRTL.h>

Expand Down
36 changes: 36 additions & 0 deletions Sources/NSLocale+MaterialRTL.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
Copyright 2018-present Google Inc. All Rights Reserved.
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>

@interface NSLocale (MaterialRTL)

/**
Is the direction of the current locale's default language Left-To-Right?
@return YES if the language is LTR, NO if the language is any other direction.
*/
+ (BOOL)mdf_isDefaultLanguageLTR;

/**
Is the direction of the current locale's default language Right-To-Left?
@return YES if the language is RTL, NO if the language is any other direction.
*/
+ (BOOL)mdf_isDefaultLanguageRTL;

@end

38 changes: 38 additions & 0 deletions Sources/NSLocale+MaterialRTL.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
Copyright 2018-present Google Inc. All Rights Reserved.
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 "NSLocale+MaterialRTL.h"

@implementation NSLocale (MaterialRTL)

+ (BOOL)mdf_isDefaultLanguageLTR {
NSString *languageCode = [[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode];
NSLocaleLanguageDirection characterDirection =
[NSLocale characterDirectionForLanguage:languageCode];
BOOL localeLanguageDirectionIsLTR = (characterDirection == NSLocaleLanguageDirectionLeftToRight);
return localeLanguageDirectionIsLTR;
}

+ (BOOL)mdf_isDefaultLanguageRTL {
NSString *languageCode = [[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode];
NSLocaleLanguageDirection characterDirection =
[NSLocale characterDirectionForLanguage:languageCode];
BOOL localeLanguageDirectionIsRTL = (characterDirection == NSLocaleLanguageDirectionRightToLeft);
return localeLanguageDirectionIsRTL;
}

@end

Loading

0 comments on commit 340899b

Please sign in to comment.