Skip to content

Commit ef3306d

Browse files
authored
feat: add objective c support
2 parents 30c4bcb + 40756b9 commit ef3306d

33 files changed

+664
-331
lines changed

AKLanguageManager.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ s.name = "AKLanguageManager"
44
s.summary = "AKLanguageManager is a language manager for iOS and tvOS applications."
55
s.requires_arc = true
66

7-
s.version = "1.0.2"
7+
s.version = "2.0.0"
88
s.license = { :type => "MIT", :file => "LICENSE" }
99
s.author = { "Amr Koritem" => "amr.koritem92@gmail.com" }
1010
s.homepage = "https://github.com/AmrKoritem/AKLanguageManager"

AKLanguageManager.xcodeproj/project.pbxproj

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
0B479F185AA146FE63EF613B /* Pods_AKLanguageManager.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 2228D014A590DE102910C3C8 /* Pods_AKLanguageManager.framework */; };
1212
1D2A6C8628F0FD34000EE30D /* View+Localized.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1D2A6C8528F0FD34000EE30D /* View+Localized.swift */; };
1313
1D2A6C8828F0FD6E000EE30D /* Image+Localized.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1D2A6C8728F0FD6E000EE30D /* Image+Localized.swift */; };
14-
1D2A6C8A28F10D8F000EE30D /* ObservedLocalizerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1D2A6C8928F10D8F000EE30D /* ObservedLocalizerTests.swift */; };
1514
1D2A6C8C28F1105F000EE30D /* LocalizedViewTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1D2A6C8B28F1105F000EE30D /* LocalizedViewTests.swift */; };
1615
1D2CC63128D0D5640000DFEF /* UIImageView+Localizable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1D2CC63028D0D5640000DFEF /* UIImageView+Localizable.swift */; };
1716
1D4074F828D0153B000A1DF0 /* Localizable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1D4074F728D0153B000A1DF0 /* Localizable.swift */; };
@@ -25,8 +24,9 @@
2524
1D40750828D01C53000A1DF0 /* UIButton+Localizable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1D40750728D01C53000A1DF0 /* UIButton+Localizable.swift */; };
2625
1D40750A28D01D08000A1DF0 /* UISegmentedControl+Localizable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1D40750928D01D08000A1DF0 /* UISegmentedControl+Localizable.swift */; };
2726
1D43EC8028EA741E000259A3 /* Bundle.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1D43EC7F28EA741E000259A3 /* Bundle.swift */; };
28-
1D4EE68828EBAE9A00EC3885 /* ObservedLocalizer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1D4EE68728EBAE9A00EC3885 /* ObservedLocalizer.swift */; };
2927
1D4EE68A28ECF2B800EC3885 /* LocalizedView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1D4EE68928ECF2B800EC3885 /* LocalizedView.swift */; };
28+
1D7D4D03290A0C0D00085878 /* NSString+Localized.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1D7D4D02290A0C0D00085878 /* NSString+Localized.swift */; };
29+
1D7D4D05290A210900085878 /* CTTextAlignment+Localized.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1D7D4D04290A210900085878 /* CTTextAlignment+Localized.swift */; };
3030
1D87DAD928C40C7E00838580 /* AKLanguageManager.docc in Sources */ = {isa = PBXBuildFile; fileRef = 1D87DAD828C40C7E00838580 /* AKLanguageManager.docc */; };
3131
1D87DADF28C40C7E00838580 /* AKLanguageManager.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1D87DAD428C40C7E00838580 /* AKLanguageManager.framework */; };
3232
1D87DAE428C40C7E00838580 /* AKLanguageManagerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1D87DAE328C40C7E00838580 /* AKLanguageManagerTests.swift */; };
@@ -71,7 +71,6 @@
7171
0709192ADF1F3C149994BACE /* Pods-AKLanguageManager-AKLanguageManagerTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-AKLanguageManager-AKLanguageManagerTests.debug.xcconfig"; path = "Target Support Files/Pods-AKLanguageManager-AKLanguageManagerTests/Pods-AKLanguageManager-AKLanguageManagerTests.debug.xcconfig"; sourceTree = "<group>"; };
7272
1D2A6C8528F0FD34000EE30D /* View+Localized.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "View+Localized.swift"; sourceTree = "<group>"; };
7373
1D2A6C8728F0FD6E000EE30D /* Image+Localized.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Image+Localized.swift"; sourceTree = "<group>"; };
74-
1D2A6C8928F10D8F000EE30D /* ObservedLocalizerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ObservedLocalizerTests.swift; sourceTree = "<group>"; };
7574
1D2A6C8B28F1105F000EE30D /* LocalizedViewTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocalizedViewTests.swift; sourceTree = "<group>"; };
7675
1D2CC63028D0D5640000DFEF /* UIImageView+Localizable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIImageView+Localizable.swift"; sourceTree = "<group>"; };
7776
1D4074F728D0153B000A1DF0 /* Localizable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Localizable.swift; sourceTree = "<group>"; };
@@ -85,8 +84,9 @@
8584
1D40750728D01C53000A1DF0 /* UIButton+Localizable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIButton+Localizable.swift"; sourceTree = "<group>"; };
8685
1D40750928D01D08000A1DF0 /* UISegmentedControl+Localizable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UISegmentedControl+Localizable.swift"; sourceTree = "<group>"; };
8786
1D43EC7F28EA741E000259A3 /* Bundle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Bundle.swift; sourceTree = "<group>"; };
88-
1D4EE68728EBAE9A00EC3885 /* ObservedLocalizer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ObservedLocalizer.swift; sourceTree = "<group>"; };
8987
1D4EE68928ECF2B800EC3885 /* LocalizedView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocalizedView.swift; sourceTree = "<group>"; };
88+
1D7D4D02290A0C0D00085878 /* NSString+Localized.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSString+Localized.swift"; sourceTree = "<group>"; };
89+
1D7D4D04290A210900085878 /* CTTextAlignment+Localized.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CTTextAlignment+Localized.swift"; sourceTree = "<group>"; };
9090
1D87DAD428C40C7E00838580 /* AKLanguageManager.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = AKLanguageManager.framework; sourceTree = BUILT_PRODUCTS_DIR; };
9191
1D87DAD728C40C7E00838580 /* AKLanguageManager.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AKLanguageManager.h; sourceTree = "<group>"; };
9292
1D87DAD828C40C7E00838580 /* AKLanguageManager.docc */ = {isa = PBXFileReference; lastKnownFileType = folder.documentationcatalog; path = AKLanguageManager.docc; sourceTree = "<group>"; };
@@ -157,7 +157,6 @@
157157
1D9A3C5D28DF506900261C65 /* LocalizedUIViewTests.swift */,
158158
1D9A3C5928DE8ECC00261C65 /* LocalizedUtilitiesTests.swift */,
159159
1D9A3C5128DAA35600261C65 /* LocalizedStringTests.swift */,
160-
1D2A6C8928F10D8F000EE30D /* ObservedLocalizerTests.swift */,
161160
1D2A6C8B28F1105F000EE30D /* LocalizedViewTests.swift */,
162161
1D70F06528EA677600364114 /* Helpers */,
163162
1D05A85728EA6F5C00E622DB /* Resources */,
@@ -208,8 +207,8 @@
208207
1D40750928D01D08000A1DF0 /* UISegmentedControl+Localizable.swift */,
209208
1D2CC63028D0D5640000DFEF /* UIImageView+Localizable.swift */,
210209
1D98A9B428E8C4D2007AFCD1 /* UIImage+Localized.swift */,
211-
1D98A9B828E8C543007AFCD1 /* NSAttributedString+Localized.swift */,
212210
1D98A9B628E8C50C007AFCD1 /* NSTextAlignment+Localized.swift */,
211+
1D7D4D04290A210900085878 /* CTTextAlignment+Localized.swift */,
213212
);
214213
path = UIKit;
215214
sourceTree = "<group>";
@@ -229,6 +228,8 @@
229228
1D4074FB28D0181B000A1DF0 /* String+Localized.swift */,
230229
1D98A9BA28E8C65C007AFCD1 /* Int+Localized.swift */,
231230
1D98A9BC28E8CA43007AFCD1 /* Double+Localized.swift */,
231+
1D7D4D02290A0C0D00085878 /* NSString+Localized.swift */,
232+
1D98A9B828E8C543007AFCD1 /* NSAttributedString+Localized.swift */,
232233
1D98A9BE28E8CB29007AFCD1 /* NSNumber+Localized.swift */,
233234
);
234235
path = DataTypes;
@@ -289,7 +290,6 @@
289290
1DD5803528CAF0CB00DCD270 /* Storage.swift */,
290291
1D4074F728D0153B000A1DF0 /* Localizable.swift */,
291292
1DB2181C28E58181009BF6ED /* Swizzle.swift */,
292-
1D4EE68728EBAE9A00EC3885 /* ObservedLocalizer.swift */,
293293
1D4EE68928ECF2B800EC3885 /* LocalizedView.swift */,
294294
);
295295
path = Helpers;
@@ -491,16 +491,17 @@
491491
buildActionMask = 2147483647;
492492
files = (
493493
1D4074FA28D01699000A1DF0 /* UIView+Localizable.swift in Sources */,
494-
1D4EE68828EBAE9A00EC3885 /* ObservedLocalizer.swift in Sources */,
495494
1D2A6C8828F0FD6E000EE30D /* Image+Localized.swift in Sources */,
496495
1D2CC63128D0D5640000DFEF /* UIImageView+Localizable.swift in Sources */,
496+
1D7D4D03290A0C0D00085878 /* NSString+Localized.swift in Sources */,
497497
1D98A9BD28E8CA43007AFCD1 /* Double+Localized.swift in Sources */,
498498
1DD5803028CAF0B400DCD270 /* Language.swift in Sources */,
499499
1D98A9B928E8C543007AFCD1 /* NSAttributedString+Localized.swift in Sources */,
500500
1D40750628D01BEF000A1DF0 /* UITabBar+Localizable.swift in Sources */,
501501
1D4074F828D0153B000A1DF0 /* Localizable.swift in Sources */,
502502
1D2A6C8628F0FD34000EE30D /* View+Localized.swift in Sources */,
503503
1D98A9BB28E8C65C007AFCD1 /* Int+Localized.swift in Sources */,
504+
1D7D4D05290A210900085878 /* CTTextAlignment+Localized.swift in Sources */,
504505
1D40750228D01B63000A1DF0 /* UITextView+Localizable.swift in Sources */,
505506
1D40750028D01B07000A1DF0 /* UILabel+Localizable.swift in Sources */,
506507
1D4074FC28D0181B000A1DF0 /* String+Localized.swift in Sources */,
@@ -529,7 +530,6 @@
529530
1D9A3C5228DAA35600261C65 /* LocalizedStringTests.swift in Sources */,
530531
1DD325CE28D71A93003D3C0A /* XibFileViewController.swift in Sources */,
531532
1D9A3C5E28DF506900261C65 /* LocalizedUIViewTests.swift in Sources */,
532-
1D2A6C8A28F10D8F000EE30D /* ObservedLocalizerTests.swift in Sources */,
533533
1D9A3C4E28DA9E1000261C65 /* StorageTests.swift in Sources */,
534534
1D9A3C5A28DE8ECC00261C65 /* LocalizedUtilitiesTests.swift in Sources */,
535535
1D2A6C8C28F1105F000EE30D /* LocalizedViewTests.swift in Sources */,

Example/Example/SupportFiles/AppDelegate.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import AKLanguageManager
1111
@main
1212
class AppDelegate: UIResponder, UIApplicationDelegate {
1313
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
14-
AKLanguageManager.shared.configureWith(defaultLanguage: .en)
14+
AKLanguageManager.shared.defaultLanguage = .en
1515
return true
1616
}
1717
}

README.md

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
[![Swift](https://img.shields.io/badge/Swift-5.0+-orange?style=flat-square)](https://img.shields.io/badge/Swift-5.0+-Orange?style=flat-square)
2+
[![Objective C](https://img.shields.io/badge/Obj-C-orange?style=flat-square)](https://img.shields.io/badge/Obj-C-Orange?style=flat-square)
23
[![UIKit](https://img.shields.io/badge/UIKit-Compatible-red?style=flat-square)](https://img.shields.io/badge/UIKit-Compatible-Red?style=flat-square)
34
[![SwiftUI](https://img.shields.io/badge/SwiftUI-Compatible-red?style=flat-square)](https://img.shields.io/badge/SwiftUI-Compatible-Red?style=flat-square)
45
[![iOS](https://img.shields.io/badge/iOS-Platform-blue?style=flat-square)](https://img.shields.io/badge/iOS-Platform-Blue?style=flat-square)
@@ -8,7 +9,11 @@
89

910
# AKLanguageManager
1011

11-
A Language manager to handle changing app localization without restarting the app.
12+
A Language manager to handle changing app localization without restarting the app.<br>
13+
- It works on Swift and Objective C projects.<br>
14+
- It's compatible with both UIKit and SwiftUI.<br>
15+
- It supports iOS and tvOS.<br>
16+
- It can be integrated via Cocoa Pods and Swift Package Manager.<br>
1217

1318
## ScreenShots
1419

@@ -31,11 +36,11 @@ Note: If you've already configured your app to be localizable, then skip to step
3136

3237
2 - Add a `.strings` file to your project resources to localise your string literals (preferabley named `Localizable.strings`), then go to file inspector and below localization press localize.
3338

34-
3 - For a UIKit, your default language must be set before your rootViewController is set using `configureWith(defaultLanguage:observedLocalizer:)`. For example, you can set it in the `AppDelegate.application(_:didFinishLaunchingWithOptions:)` method. If the default language wasn't set in your UIKit app, you will encounter errors.
39+
3 - For a UIKit project, your default language must be set before your rootViewController is set using `configureWith(defaultLanguage:observedLocalizer:)`. For example, you can set it in the `AppDelegate.application(_:didFinishLaunchingWithOptions:)` method. If the default language wasn't set in your UIKit app, you will encounter errors.
3540
```swift
3641
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
3742
// you can use .deviceLanguage to keep the device default language.
38-
AKLanguageManager.shared.configureWith(defaultLanguage: .en)
43+
AKLanguageManager.shared.defaultLanguage = .en
3944
return true
4045
}
4146
```
@@ -49,30 +54,31 @@ Note: If you've already configured your app to be localizable, then skip to step
4954
}
5055
```
5156

52-
5 - Note that you will have to call `localized()` on views that will be presented.
57+
5 - Note that you will have to call `localized()` on presented views.
5358
```swift
5459
VStack {
5560
...
5661
}
57-
.popover(isPresented: $isPresented) {
62+
.popover(isPresented: $isPresented) { // same goes for other presentation styles.
5863
AnotherView()
5964
.localized()
6065
}
6166
```
6267

63-
6 - The default language is the language your app will be localized in when it runs first time.
68+
Note: The default language is the language your app will be localized in when it runs first time.
6469

6570
## Usage
6671

67-
1 - If you want to change the language in a UIKit, use the `setLanguage(language:)` method by passing to it the new language.
72+
1 - If you want to change the language, use the `setLanguage(language:)` method by passing to it the new language.<br>
73+
In a UIKit project, at least the parameter `viewControllerFactory` must be provided in addition to the language:<br>
6874
```swift
6975
// Change Language and set rootViewController to the initial view controller
7076
@IBAction func changeLanguage() {
7177
// Swap between anglish and arabic languages
7278
let newLanguage = AKLanguageManager.shared.selectedLanguage == .en ? Language.ar : Language.en
7379
AKLanguageManager.shared.setLanguage(
7480
language: newLanguage,
75-
viewControllerFactory: { _ in
81+
viewControllerFactory: { windowTitle in
7682
// The view controller that you want to show after changing the language
7783
let settingsVC = Storyboard.Main.instantiate(viewController: SettingsViewController.self)
7884
return Storyboard.Main.initialViewController ?? settingsVC
@@ -84,27 +90,13 @@ Note: If you've already configured your app to be localizable, then skip to step
8490
)
8591
}
8692
```
87-
88-
2 - Note that images change direction by default in UIKit UI elements according to the language direction. If you want a UI element (for example: UIImageView) not to change its direction, you can set its `shouldLocalizeDirection` property to `false`.
89-
```swift
90-
@IBOutlet weak var fixedImageView: UIImageView!
91-
92-
override func viewDidLoad() {
93-
super.viewDidLoad()
94-
// Make the image direction fixed even when localization direction change
95-
fixedImageView.shouldLocalizeDirection = false
96-
}
97-
```
98-
99-
3 - If you want to change the language in a SwiftUI set the `selectedLanguage` property in the environment object with the new language.
93+
<br>
94+
In a SwiftUI project only the language is needed:<br>
10095
```swift
10196
import SwiftUI
10297
import AKLanguageManager
10398

10499
struct LangaugeView: View {
105-
@EnvironmentObject
106-
var localizer: ObservedLocalizer
107-
108100
var body: some View {
109101
VStack {
110102
Text("Select a language".localized)
@@ -113,14 +105,14 @@ struct LangaugeView: View {
113105
HStack {
114106
Button("العربية") {
115107
withAnimation {
116-
localizer.selectedLanguage = .ar
108+
AKLanguageManager.shared.setLanguage(language: .ar)
117109
}
118110
}
119111
.padding()
120112
Spacer()
121113
Button("English") {
122114
withAnimation {
123-
localizer.selectedLanguage = .en
115+
AKLanguageManager.shared.setLanguage(language: .en)
124116
}
125117
}
126118
.padding()
@@ -130,13 +122,24 @@ struct LangaugeView: View {
130122
}
131123
```
132124

133-
4 - Note that images are fixed by default in SwiftUI views. If you want to change an image's direction according to the selected language direction, use the method `directionLocalized()`.
125+
2 - Note that images change direction by default in UIKit UI elements according to the language direction. If you want a UI element (for example: UIImageView) not to change its direction, you can set its `shouldLocalizeDirection` property to `false`.
126+
```swift
127+
@IBOutlet weak var fixedImageView: UIImageView!
128+
129+
override func viewDidLoad() {
130+
super.viewDidLoad()
131+
// Make the image direction fixed even when localization direction change
132+
fixedImageView.shouldLocalizeDirection = false
133+
}
134+
```
135+
136+
3 - Note that images are fixed by default in SwiftUI views. If you want to change an image's direction according to the selected language direction, use the method `directionLocalized()`.
134137
```swift
135138
Image("image")
136139
.directionLocalized()
137140
```
138141

139-
5 - String, Int, and Double can be localized using the property `localized`.
142+
4 - String, Int, and Double can be localized using the property `localized`.
140143
```swift
141144
// where selected language is .ar
142145
print("01.10 key".localized)
@@ -147,7 +150,7 @@ struct LangaugeView: View {
147150
// prints ٠١
148151
```
149152

150-
6 - Numbers are localized by default, you can stop numbers localization by setting the property `shouldLocalizeNumbers` to `false`.
153+
5 - Numbers are localized by default, you can stop numbers localization by setting the property `shouldLocalizeNumbers` to `false`.
151154
```swift
152155
// where selected language is .ar
153156
AKLanguageManager.shared.shouldLocalizeNumbers = false

0 commit comments

Comments
 (0)