|
| 1 | +[](https://img.shields.io/badge/Swift-5.0+-Orange?style=flat-square) |
| 2 | +[](https://img.shields.io/badge/UIKit-Compatible-Red?style=flat-square) |
| 3 | +[](https://img.shields.io/badge/SwiftUI-Compatible-Red?style=flat-square) |
| 4 | +[](https://img.shields.io/badge/iOS-Platform-Blue?style=flat-square) |
| 5 | +[](https://img.shields.io/badge/tvOS-Platform-Blue?style=flat-square) |
| 6 | +[](https://img.shields.io/badge/CocoaPods-Support-Yellow?style=flat-square) |
| 7 | +[](https://img.shields.io/badge/Swift_Package_Manager-Support-Yellow?style=flat-square) |
| 8 | + |
1 | 9 | # AKLanguageManager |
2 | 10 |
|
3 | | -A description of this package. |
| 11 | +A Language manager to handle changing app localization without restarting the app. |
| 12 | + |
| 13 | +## ScreenShots |
| 14 | + |
| 15 | +<img src="https://raw.githubusercontent.com/AmrKoritem/AKLanguageManager/master/README/aklm-example.gif" width="300"> |
| 16 | + |
| 17 | +## Installation |
| 18 | + |
| 19 | +AKLanguageManager can be installed using [CocoaPods](https://cocoapods.org). Add the following lines to your Podfile: |
| 20 | +```ruby |
| 21 | +pod 'AKLanguageManager' |
| 22 | +``` |
| 23 | + |
| 24 | +You can also install it using [swift package manager](https://developer.apple.com/documentation/xcode/adding_package_dependencies_to_your_app) as well. |
| 25 | + |
| 26 | +## Setup |
| 27 | + |
| 28 | +Note: If you've already configured your app to be localizable, then skip to step 3. |
| 29 | + |
| 30 | +1 - Configure your app to be localizable by going to `PROJECT > Localisation`, then add the languages you want to support (Arabic for example), dialog will appear to ask you which resources files you want to localize (if any exist), select all the files you wish to localize. |
| 31 | + |
| 32 | +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. |
| 33 | + |
| 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. |
| 35 | +```swift |
| 36 | + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { |
| 37 | + // you can use .deviceLanguage to keep the device default language. |
| 38 | + AKLanguageManager.shared.configureWith(defaultLanguage: .en) |
| 39 | + return true |
| 40 | + } |
| 41 | +``` |
| 42 | + |
| 43 | +4 - As for SwiftUI, add your root view as a child to `LocalizedView`. |
| 44 | +```swift |
| 45 | + LocalizedView(.en) { |
| 46 | + ContentView() |
| 47 | + // Animation that will be used when localization is triggered. |
| 48 | + .transition(.slide) |
| 49 | + } |
| 50 | +``` |
| 51 | + |
| 52 | +5 - Note that you will have to call `localized()` on views that will be presented. |
| 53 | +```swift |
| 54 | + VStack { |
| 55 | + ... |
| 56 | + } |
| 57 | + .popover(isPresented: $isPresented) { |
| 58 | + AnotherView() |
| 59 | + .localized() |
| 60 | + } |
| 61 | +``` |
| 62 | + |
| 63 | +6 - The default language is the language your app will be localized in when it runs first time. |
| 64 | + |
| 65 | +## Usage |
| 66 | + |
| 67 | +1 - If you want to change the language in a UIKit, use the `setLanguage(language:)` method by passing to it the new language. |
| 68 | +```swift |
| 69 | + // Change Language and set rootViewController to the initial view controller |
| 70 | + @IBAction func changeLanguage() { |
| 71 | + // Swap between anglish and arabic languages |
| 72 | + let newLanguage = AKLanguageManager.shared.selectedLanguage == .en ? Language.ar : Language.en |
| 73 | + AKLanguageManager.shared.setLanguage( |
| 74 | + language: newLanguage, |
| 75 | + viewControllerFactory: { _ in |
| 76 | + // The view controller that you want to show after changing the language |
| 77 | + let settingsVC = Storyboard.Main.instantiate(viewController: SettingsViewController.self) |
| 78 | + return Storyboard.Main.initialViewController ?? settingsVC |
| 79 | + }, |
| 80 | + animation: { view in |
| 81 | + // Do custom animation |
| 82 | + view.alpha = 0 |
| 83 | + } |
| 84 | + ) |
| 85 | + } |
| 86 | +``` |
| 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. |
| 100 | +```swift |
| 101 | +import SwiftUI |
| 102 | +import AKLanguageManager |
| 103 | + |
| 104 | +struct LangaugeView: View { |
| 105 | + @EnvironmentObject |
| 106 | + var localizer: ObservedLocalizer |
| 107 | + |
| 108 | + var body: some View { |
| 109 | + VStack { |
| 110 | + Text("Select a language".localized) |
| 111 | + .fontWeight(.bold) |
| 112 | + .padding() |
| 113 | + HStack { |
| 114 | + Button("العربية") { |
| 115 | + withAnimation { |
| 116 | + localizer.selectedLanguage = .ar |
| 117 | + } |
| 118 | + } |
| 119 | + .padding() |
| 120 | + Spacer() |
| 121 | + Button("English") { |
| 122 | + withAnimation { |
| 123 | + localizer.selectedLanguage = .en |
| 124 | + } |
| 125 | + } |
| 126 | + .padding() |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | +} |
| 131 | +``` |
| 132 | + |
| 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()`. |
| 134 | +```swift |
| 135 | + Image("image") |
| 136 | + .directionLocalized() |
| 137 | +``` |
| 138 | + |
| 139 | +5 - String, Int, and Double can be localized using the property `localized`. |
| 140 | +```swift |
| 141 | + // where selected language is .ar |
| 142 | + print("01.10 key".localized) |
| 143 | + // prints ٠١٫١٠ مفتاح |
| 144 | + print(01.10.localized) |
| 145 | + // prints ٠١٫١٠ |
| 146 | + print(01.localized) |
| 147 | + // prints ٠١ |
| 148 | +``` |
| 149 | + |
| 150 | +6 - Numbers are localized by default, you can stop numbers localization by setting the property `shouldLocalizeNumbers` to `false`. |
| 151 | +```swift |
| 152 | + // where selected language is .ar |
| 153 | + AKLanguageManager.shared.shouldLocalizeNumbers = false |
| 154 | + print("01.10 key".localized) |
| 155 | + // prints 01.10 مفتاح |
| 156 | +``` |
| 157 | + |
| 158 | +Please check the example project in this repo to see how it works. You can check a full set of examples [here](https://github.com/AmrKoritem/AKLibrariesExamples) as well. |
| 159 | + |
| 160 | +## Contribution |
| 161 | + |
| 162 | +All contributions are welcome. Please check the [Known issues](https://github.com/AmrKoritem/AKLanguageManager#known-issues) and [Future plans](https://github.com/AmrKoritem/AKLanguageManager#future-plans) sections if you don't know where to start. And of course feel free to raise your own issues and create PRs for them! |
| 163 | + |
| 164 | +## Known issues |
| 165 | + |
| 166 | +1 - Strings shown in launch screen are not localized. [#6](https://github.com/AmrKoritem/AKLanguageManager/issues/6)<br> |
| 167 | + Unfortunately, this is intended by apple as stated [here](https://developer.apple.com/design/human-interface-guidelines/patterns/launching/#:~:text=Avoid%20including%20text%20on%20your%20launch%20screen.). |
| 168 | + |
| 169 | +2 - Localized version of an image asset doesn't show when changing app language. [#7](https://github.com/AmrKoritem/AKLanguageManager/issues/7)<br> |
| 170 | + We are currently looking into this issue. The current workaround is to use different names for each image localization, and get them using localized strings. |
| 171 | + |
| 172 | +3 - SF Symbol images size is reduced when their direction change. [#8](https://github.com/AmrKoritem/AKLanguageManager/issues/8)<br> |
| 173 | + |
| 174 | +## Future plans |
| 175 | + |
| 176 | +1 - Get localized strings with comments. [#9](https://github.com/AmrKoritem/AKLanguageManager/issues/9)<br> |
| 177 | +2 - Localizing plurals. [#10](https://github.com/AmrKoritem/AKLanguageManager/issues/10)<br> |
| 178 | +3 - Carthage support. [#11](https://github.com/AmrKoritem/AKLanguageManager/issues/11)<br> |
| 179 | +4 - Check text language. [#12](https://github.com/AmrKoritem/AKLanguageManager/issues/12)<br> |
| 180 | + |
| 181 | +## Credit |
| 182 | + |
| 183 | +This library was inspired by [Abedalkareem's LanguageManager-iOS](https://github.com/Abedalkareem/LanguageManager-iOS) library. Please check his work and give him the credit he deserves 🚀 |
| 184 | + |
| 185 | +## Follow me |
| 186 | + |
| 187 | +[LinkedIn](https://www.linkedin.com/in/amr-koritem-976bb0125/) |
| 188 | + |
| 189 | +## License |
| 190 | + |
| 191 | +Please check the license file. |
0 commit comments