运行时改变本地化的 iOS 库。
要求:iOS 9.0+ • Swift 5.0+
基本用法
在您的 AppDelegate 中
import SMLocalize
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, ReloadableAppDelegate {
var window: UIWindow?
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Uncomment the next line if u want to use Arabic as the default language at the first app launch before the user changes the language manually.
// SMLocalize.defaultLanguage = "ar"
SMLocalize.configure()
reload()
return true
}
func reload() {
if window == nil {
window = UIWindow(frame: UIScreen.main.bounds)
window!.makeKeyAndVisible()
}
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateInitialViewController()
window!.rootViewController = vc
}
}
然后在您的更改语言动作中
import SMLocalize
class ViewController: UIViewController {
...
...
@IBAction func changeLanguageTapped(_ sender: UIButton) {
SMLocalize.currentLanguage = "ar" // Your new language
SMLocalize.reloadAppDelegate()
}
}
动画
在语言更改期间播放动画。
在您的更改语言动作中
import SMLocalize
class ViewController: UIViewController {
...
...
@IBAction func changeLanguageTapped(_ sender: UIButton) {
SMLocalize.currentLanguage = "ar" // Your new language
// Optional animation. Change to nil if not needed.
SMLocalize.reloadAppDelegate(animation: [.transitionFlipFromRight, .curveEaseOut], duration: 0.3)
}
}
默认语言
设置一个默认语言,以便在用户更改语言之前第一次启动应用程序时使用。
在您的 AppDelegate 中
import SMLocalize
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, ReloadableAppDelegate {
var window: UIWindow?
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
SMLocalize.defaultLanguage = "ar" // Must be set before SMLocalize.configure()
SMLocalize.configure()
reload()
return true
}
}
翻转图像
翻转图像以匹配当前语言方向,例如箭头。
在您的 AppDelegate 中
import SMLocalize
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, ReloadableAppDelegate {
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
SMLocalize.configure()
// Flip images in views with tags from 1 to 10
// Avoid including 0 in this set since it will cause UIKit issues.
SMLocalize.flipImagesInViewsWithTags = Set(1...10)
return true
}
}
在您的ViewController
import SMLocalize
class ViewController: UIViewController {
...
...
override func viewDidLoad() {
super.viewDidLoad()
arrowImgToFlip.tag = 1
anotherImgToFlip.tag = 2
myContainerView.tag = 5
imgInsideMyContainerView.tag = 6
}
}
支持翻转图像的视图
视图 | 是否支持翻转图像? | 注意 |
---|---|---|
UIImageView | _ | |
UIButton | (对于所有状态) |
_ |
UISlider | _ | |
UICollectionViewCell | 使用 UIImage.imageFlippedForRightToLeftLayoutDirection() 在您的cellForItem代理函数中 |
|
UITableViewCell | 使用 UIImage.imageFlippedForRightToLeftLayoutDirection() 在您的cellForRow代理函数中 |
有关如何使用库的示例
要运行示例项目,请克隆存储库,然后从示例目录中打开SMLocalizeExample.xcworkspace。
安装
SMLocalize可以通过CocoaPods获取。要安装,只需将以下行添加到您的Podfile中
pod 'SMLocalize'
待办事项
- 支持通过Carthage和Swift Package Manager进行安装(需要帮助)
- 自动使用文本本地化视图
- 改进库API吗?
致谢
其他库
SMLocalize受到了以下库的启发。在某些部分采用了相同的技术,在其他部分则有所不同。
文章
作者
Steven, [email protected]
许可证
SMLocalize在MIT许可证下提供。有关更多信息,请参阅LICENSE文件。