├── Localizable ├── DemoAssets │ ├── Demo.gif │ ├── Demo_.gif │ ├── tut_4.png │ ├── tut_1_1.png │ ├── tut_1_2.png │ ├── tut_2_1.png │ ├── tut_2_2.png │ ├── tut_3_1.png │ └── tut_3_2.png ├── ar.lproj │ ├── flag.png │ └── Localizable.strings ├── en.lproj │ ├── flag.png │ └── Localizable.strings ├── uk.lproj │ ├── flag.png │ └── Localizable.strings ├── Assets.xcassets │ └── AppIcon.appiconset │ │ └── Contents.json ├── Info.plist ├── MainVC.swift ├── AppDelegate.swift └── Localizable.swift ├── LICENSE ├── .gitignore └── README.md /Localizable/DemoAssets/Demo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/romansorochak/Localizable/HEAD/Localizable/DemoAssets/Demo.gif -------------------------------------------------------------------------------- /Localizable/ar.lproj/flag.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/romansorochak/Localizable/HEAD/Localizable/ar.lproj/flag.png -------------------------------------------------------------------------------- /Localizable/en.lproj/flag.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/romansorochak/Localizable/HEAD/Localizable/en.lproj/flag.png -------------------------------------------------------------------------------- /Localizable/uk.lproj/flag.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/romansorochak/Localizable/HEAD/Localizable/uk.lproj/flag.png -------------------------------------------------------------------------------- /Localizable/DemoAssets/Demo_.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/romansorochak/Localizable/HEAD/Localizable/DemoAssets/Demo_.gif -------------------------------------------------------------------------------- /Localizable/DemoAssets/tut_4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/romansorochak/Localizable/HEAD/Localizable/DemoAssets/tut_4.png -------------------------------------------------------------------------------- /Localizable/DemoAssets/tut_1_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/romansorochak/Localizable/HEAD/Localizable/DemoAssets/tut_1_1.png -------------------------------------------------------------------------------- /Localizable/DemoAssets/tut_1_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/romansorochak/Localizable/HEAD/Localizable/DemoAssets/tut_1_2.png -------------------------------------------------------------------------------- /Localizable/DemoAssets/tut_2_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/romansorochak/Localizable/HEAD/Localizable/DemoAssets/tut_2_1.png -------------------------------------------------------------------------------- /Localizable/DemoAssets/tut_2_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/romansorochak/Localizable/HEAD/Localizable/DemoAssets/tut_2_2.png -------------------------------------------------------------------------------- /Localizable/DemoAssets/tut_3_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/romansorochak/Localizable/HEAD/Localizable/DemoAssets/tut_3_1.png -------------------------------------------------------------------------------- /Localizable/DemoAssets/tut_3_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/romansorochak/Localizable/HEAD/Localizable/DemoAssets/tut_3_2.png -------------------------------------------------------------------------------- /Localizable/ar.lproj/Localizable.strings: -------------------------------------------------------------------------------- 1 | /* 2 | Localizable.strings 3 | Localizable 4 | 5 | Created by Roman Sorochak on 6/23/17. 6 | Copyright © 2017 MagicLab. All rights reserved. 7 | */ 8 | 9 | "main_page_language" = "العربية"; 10 | "main_page_change_language" = "تغيير اللغة"; 11 | 12 | "alert_change_language_title" = "تغيير اللغة ل"; 13 | "alert_cancel" = "إلغاء"; 14 | "en" = "الإنجليزية"; 15 | "uk" = "الأوكراني"; 16 | "ar" = "العربية"; 17 | -------------------------------------------------------------------------------- /Localizable/en.lproj/Localizable.strings: -------------------------------------------------------------------------------- 1 | /* 2 | Localizable.strings 3 | Localizable 4 | 5 | Created by Roman Sorochak on 6/23/17. 6 | Copyright © 2017 MagicLab. All rights reserved. 7 | */ 8 | 9 | 10 | "main_page_language" = "English"; 11 | "main_page_change_language" = "Change language"; 12 | 13 | "alert_change_language_title" = "Change language to"; 14 | "alert_cancel" = "Cancel"; 15 | "en" = "English"; 16 | "uk" = "Ukrainian"; 17 | "ar" = "Arabic"; 18 | -------------------------------------------------------------------------------- /Localizable/uk.lproj/Localizable.strings: -------------------------------------------------------------------------------- 1 | /* 2 | Localizable.strings 3 | Localizable 4 | 5 | Created by Roman Sorochak on 6/23/17. 6 | Copyright © 2017 MagicLab. All rights reserved. 7 | */ 8 | 9 | "main_page_language" = "Українська"; 10 | "main_page_change_language" = "Поміняти мову"; 11 | 12 | "alert_change_language_title" = "Поміняти мову на"; 13 | "alert_cancel" = "Скасувати"; 14 | "en" = "Англійська"; 15 | "uk" = "Українська"; 16 | "ar" = "Арабська"; 17 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Roman Sorochak 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Localizable/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "29x29", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "40x40", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "40x40", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "60x60", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "60x60", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "ipad", 35 | "size" : "29x29", 36 | "scale" : "1x" 37 | }, 38 | { 39 | "idiom" : "ipad", 40 | "size" : "29x29", 41 | "scale" : "2x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "40x40", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "40x40", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "76x76", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "76x76", 61 | "scale" : "2x" 62 | } 63 | ], 64 | "info" : { 65 | "version" : 1, 66 | "author" : "xcode" 67 | } 68 | } -------------------------------------------------------------------------------- /Localizable/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | UILaunchStoryboardName 24 | LaunchScreen 25 | UIMainStoryboardFile 26 | Main 27 | UIRequiredDeviceCapabilities 28 | 29 | armv7 30 | 31 | UISupportedInterfaceOrientations 32 | 33 | UIInterfaceOrientationPortrait 34 | UIInterfaceOrientationLandscapeLeft 35 | UIInterfaceOrientationLandscapeRight 36 | 37 | UISupportedInterfaceOrientations~ipad 38 | 39 | UIInterfaceOrientationPortrait 40 | UIInterfaceOrientationPortraitUpsideDown 41 | UIInterfaceOrientationLandscapeLeft 42 | UIInterfaceOrientationLandscapeRight 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## Build generated 6 | build/ 7 | DerivedData/ 8 | 9 | ## Various settings 10 | *.pbxuser 11 | !default.pbxuser 12 | *.mode1v3 13 | !default.mode1v3 14 | *.mode2v3 15 | !default.mode2v3 16 | *.perspectivev3 17 | !default.perspectivev3 18 | xcuserdata/ 19 | 20 | ## Other 21 | *.moved-aside 22 | *.xccheckout 23 | *.xcscmblueprint 24 | 25 | ## Obj-C/Swift specific 26 | *.hmap 27 | *.ipa 28 | *.dSYM.zip 29 | *.dSYM 30 | 31 | ## Playgrounds 32 | timeline.xctimeline 33 | playground.xcworkspace 34 | 35 | # Swift Package Manager 36 | # 37 | # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. 38 | # Packages/ 39 | # Package.pins 40 | .build/ 41 | 42 | # CocoaPods 43 | # 44 | # We recommend against adding the Pods directory to your .gitignore. However 45 | # you should judge for yourself, the pros and cons are mentioned at: 46 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 47 | # 48 | # Pods/ 49 | 50 | # Carthage 51 | # 52 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 53 | # Carthage/Checkouts 54 | 55 | Carthage/Build 56 | 57 | # fastlane 58 | # 59 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 60 | # screenshots whenever they are needed. 61 | # For more information about the recommended setup visit: 62 | # https://docs.fastlane.tools/best-practices/source-control/#source-control 63 | 64 | fastlane/report.xml 65 | fastlane/Preview.html 66 | fastlane/screenshots 67 | fastlane/test_output 68 | -------------------------------------------------------------------------------- /Localizable/MainVC.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.swift 3 | // Localizable 4 | // 5 | // Created by Roman Sorochak on 6/23/17. 6 | // Copyright © 2017 MagicLab. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class MainVC: UIViewController { 12 | 13 | @IBOutlet weak var label: UILabel! 14 | @IBOutlet weak var imageView: UIImageView! 15 | @IBOutlet weak var button: UIButton! 16 | 17 | override func viewDidLoad() { 18 | super.viewDidLoad() 19 | 20 | setupUI() 21 | } 22 | 23 | private func setupUI() { 24 | label.text = "main_page_language".localized 25 | imageView.image = "flag".localizedImage 26 | button.setTitle( 27 | "main_page_change_language".localized, 28 | for: .normal 29 | ) 30 | } 31 | 32 | @IBAction func changeLanguage(_ sender: Any) { 33 | let alert = UIAlertController( 34 | title: "alert_change_language_title".localized, 35 | message: nil, 36 | preferredStyle: .actionSheet 37 | ) 38 | 39 | func addActionLanguage(language: Language) { 40 | alert.addAction( 41 | UIAlertAction( 42 | title: language.rawValue.localized, 43 | style: UIAlertActionStyle.default, 44 | handler: { _ in 45 | Language.language = language 46 | }) 47 | ) 48 | } 49 | addActionLanguage(language: Language.english) 50 | addActionLanguage(language: Language.ukrainian) 51 | addActionLanguage(language: Language.arabic) 52 | 53 | alert.addAction( 54 | UIAlertAction( 55 | title: "alert_cancel".localized, 56 | style: UIAlertActionStyle.cancel, 57 | handler: nil 58 | ) 59 | ) 60 | present(alert, animated: true, completion: nil) 61 | } 62 | } 63 | 64 | -------------------------------------------------------------------------------- /Localizable/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // Localizable 4 | // 5 | // Created by Roman Sorochak on 6/23/17. 6 | // Copyright © 2017 MagicLab. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @UIApplicationMain 12 | class AppDelegate: UIResponder, UIApplicationDelegate { 13 | 14 | var window: UIWindow? 15 | 16 | 17 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 18 | // Override point for customization after application launch. 19 | return true 20 | } 21 | 22 | func applicationWillResignActive(_ application: UIApplication) { 23 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 24 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 25 | } 26 | 27 | func applicationDidEnterBackground(_ application: UIApplication) { 28 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 29 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 30 | } 31 | 32 | func applicationWillEnterForeground(_ application: UIApplication) { 33 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 34 | } 35 | 36 | func applicationDidBecomeActive(_ application: UIApplication) { 37 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 38 | } 39 | 40 | func applicationWillTerminate(_ application: UIApplication) { 41 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 42 | } 43 | 44 | 45 | } 46 | 47 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Localizable 2 | - Simple approach to localize strings/images. 3 | - Change app language in the app. 4 | 5 | 6 | ![Demo](https://github.com/romansorochak/Localizable/blob/master/Localizable/DemoAssets/Demo_.gif) 7 | 8 | ## Contents 9 | - [Requirements](#requirements) 10 | - [Installation](#installation) 11 | - [Usage](#usage) 12 | - [License](#license) 13 | 14 | 15 | ## Requirements 16 | 17 | - iOS 9.0+ 18 | - Xcode 8.0+ 19 | - Swift 3.0+ 20 | 21 | ## Installation 22 | All logic is in [Localizable.swift](https://github.com/romansorochak/Localizable/blob/master/Localizable/Localizable.swift) file. 23 | Just copy this [file](https://github.com/romansorochak/Localizable/blob/master/Localizable/Localizable.swift) to your project. 24 | 25 | # Usage 26 | 27 | ## Localize your project 28 | 1) Create Localizable.strings file 29 | 30 | 31 | 32 | 33 | 2) In Localizable.strings file tap Localize button and select english language. 34 | 35 | 36 | 37 | 38 | 3) In project file add supported languages. 39 | 40 | 41 | 42 | 43 | 4) Recommendation: do not localize storyboards or xibs. Make it simpler. Put all your localized strings in Localizable.strings file. 44 | 45 | 46 | 5) Reproduce step 2 for needed languages. 47 | 48 | ## Localization 49 | 1) Recommendation: do not localize storyboards/xibs. Always set strings from code. 50 | 51 | 2) Extend enum Language with appropriate languages used in the project 52 | ```swift 53 | enum Language: String { 54 | 55 | case english = "en" 56 | case arabic = "ar" 57 | case ukrainian = "uk" 58 | //... 59 | ``` 60 | 61 | ## Get appropriate localized string 62 | ```swift 63 | "main_page_language".localized 64 | ``` 65 | 66 | ## Get appropriate localized image 67 | ```swift 68 | "flag".localizedImage 69 | ``` 70 | 71 | ## Change language in the app 72 | To change language just set Language case to Language.language static property. 73 | It will change app language and semantic if need. 74 | To enable changes it will restart the app within instantiating initial view controller from Main storyboard. 75 | - Important: use appropriate properties described above. 76 | 77 | ```swift 78 | Language.language = Language.english 79 | ``` 80 | 81 | ## Author 82 | Roman Sorochak - iOS developer - roman.sorochak@gmail.com 83 | 84 | ## License 85 | 86 | Localizable is released under the MIT license. See [LICENSE](https://github.com/romansorochak/Localizable/blob/master/LICENSE) for details. 87 | -------------------------------------------------------------------------------- /Localizable/Localizable.swift: -------------------------------------------------------------------------------- 1 | // 2 | // String+Localizable.swift 3 | // Localizable 4 | // 5 | // Created by Roman Sorochak on 6/23/17. 6 | // Copyright © 2017 MagicLab. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | 12 | private let appleLanguagesKey = "AppleLanguages" 13 | 14 | 15 | enum Language: String { 16 | 17 | case english = "en" 18 | case arabic = "ar" 19 | case ukrainian = "uk" 20 | 21 | var semantic: UISemanticContentAttribute { 22 | switch self { 23 | case .english, .ukrainian: 24 | return .forceLeftToRight 25 | case .arabic: 26 | return .forceRightToLeft 27 | } 28 | } 29 | 30 | 31 | static var language: Language { 32 | get { 33 | if let languageCode = UserDefaults.standard.string(forKey: appleLanguagesKey), 34 | let language = Language(rawValue: languageCode) { 35 | return language 36 | } else { 37 | let preferredLanguage = NSLocale.preferredLanguages[0] as String 38 | let index = preferredLanguage.index( 39 | preferredLanguage.startIndex, 40 | offsetBy: 2 41 | ) 42 | guard let localization = Language( 43 | rawValue: preferredLanguage.substring(to: index) 44 | ) else { 45 | return Language.english 46 | } 47 | 48 | return localization 49 | } 50 | } 51 | set { 52 | guard language != newValue else { 53 | return 54 | } 55 | 56 | //change language in the app 57 | //the language will be changed after restart 58 | UserDefaults.standard.set([newValue.rawValue], forKey: appleLanguagesKey) 59 | UserDefaults.standard.synchronize() 60 | 61 | //Changes semantic to all views 62 | //this hack needs in case of languages with different semantics: leftToRight(en/uk) & rightToLeft(ar) 63 | UIView.appearance().semanticContentAttribute = newValue.semantic 64 | 65 | //initialize the app from scratch 66 | //show initial view controller 67 | //so it seems like the is restarted 68 | //NOTE: do not localize storboards 69 | //After the app restart all labels/images will be set 70 | //see extension String below 71 | UIApplication.shared.windows[0].rootViewController = UIStoryboard( 72 | name: "Main", 73 | bundle: nil 74 | ).instantiateInitialViewController() 75 | } 76 | } 77 | } 78 | 79 | 80 | extension String { 81 | 82 | var localized: String { 83 | return Bundle.localizedBundle.localizedString(forKey: self, value: nil, table: nil) 84 | } 85 | 86 | var localizedImage: UIImage? { 87 | return localizedImage() 88 | ?? localizedImage(type: ".png") 89 | ?? localizedImage(type: ".jpg") 90 | ?? localizedImage(type: ".jpeg") 91 | ?? UIImage(named: self) 92 | } 93 | 94 | private func localizedImage(type: String = "") -> UIImage? { 95 | guard let imagePath = Bundle.localizedBundle.path(forResource: self, ofType: type) else { 96 | return nil 97 | } 98 | return UIImage(contentsOfFile: imagePath) 99 | } 100 | } 101 | 102 | extension Bundle { 103 | //Here magic happens 104 | //when you localize resources: for instance Localizable.strings, images 105 | //it creates different bundles 106 | //we take appropriate bundle according to language 107 | static var localizedBundle: Bundle { 108 | let languageCode = Language.language.rawValue 109 | guard let path = Bundle.main.path(forResource: languageCode, ofType: "lproj") else { 110 | return Bundle.main 111 | } 112 | return Bundle(path: path)! 113 | } 114 | } 115 | --------------------------------------------------------------------------------