項目語言國際化流程崎脉,以作備忘囚灼!
1 創(chuàng)建配置文件
工程內(nèi)新建Strings File
文件灶体,并命名Localizable
創(chuàng)建好后如下圖所示
2 添加語言
選中Localizable.strings
文件,點擊右側(cè)Localize...
點擊后如下所示
3 設(shè)置語言
前往工程PROJECT
樟结,點擊Localizations
下的+
在彈出的語言中選擇想要使用的語言即可瓢宦,本次以簡體中文
和英文
為例
簡體中文為Chinese(Simplified)
英文為English
在彈出的窗口上點擊Finish
即可
有使用Main.storyboard
和LaunchScreen.storyboard
可以勾選這兩個
完成后就會如下圖所示,這兩個文件就是當前工程的簡體中文
和英文
配置文件
4 簡單使用
配置文件填寫對應(yīng)的文字
加載對應(yīng)文字的時候如下
let label = UILabel(frame: CGRect(x: 20, y: 200, width: UIScreen.main.bounds.width - 40, height: 40))
label.backgroundColor = UIColor.lightGray
label.text = NSLocalizedString("國際化語言展示", comment: "")
label.textColor = UIColor.red
label.textAlignment = .center
label.font = UIFont.boldSystemFont(ofSize: 16)
view.addSubview(label)
模擬器語言為英文和簡體中文狀態(tài)下的展示結(jié)果
5 手動切換語言
創(chuàng)建國際化語言管理工具類
import UIKit
fileprivate let UserLanguage = "UserLanguage"
fileprivate let AppleLanguages = "AppleLanguages"
enum LanguageType: Int {
case Chinese = 0
case English
}
class InternationalTool {
/// 單例
static var shared: InternationalTool {
struct Static {
static let instance: InternationalTool = InternationalTool()
}
return Static.instance
}
private var bundle: Bundle?
/// 獲取國際化語言
///
/// - Parameter key: key
/// - Returns: 國際化語言
public func string(_ key: String) -> String {
let bundle = InternationalTool.shared.bundle
let str = bundle?.localizedString(forKey: key, value: nil, table: nil)
return str ?? ""
}
/// 初始化語言 Appdelegate 中使用
public func initUserLanguage() {
var str = UserDefaults.standard.value(forKey: UserLanguage) as? String
if str?.count == 0 || str == nil {
let languages = UserDefaults.standard.object(forKey: AppleLanguages) as? NSArray
if languages?.count != 0 {
let current = languages?.object(at: 0) as? String
if current != nil {
str = current ?? ""
UserDefaults.standard.set(current, forKey: UserLanguage)
UserDefaults.standard.synchronize()
}
}
}
str = str?.replacingOccurrences(of: "-CN", with: "")
str = str?.replacingOccurrences(of: "-US", with: "")
var path = Bundle.main.path(forResource: str, ofType: "lproj")
if path == nil {
path = Bundle.main.path(forResource: "en", ofType: "lproj")
}
bundle = Bundle(path: path!)
}
/// 設(shè)置當前語言
///
/// - Parameter language: 當前語言
public func setLanguage(_ type: LanguageType) {
var str = ""
switch type {
case .Chinese:
str = "zh-Hans"
case .English:
str = "en"
}
let path = Bundle.main.path(forResource: str, ofType: "lproj")
bundle = Bundle(path: path!)
UserDefaults.standard.set(str, forKey: UserLanguage)
UserDefaults.standard.synchronize()
}
/// 當前語言
///
/// - Returns: 當前語言類型
public func current() -> String {
return UserDefaults.standard.value(forKey: UserLanguage) as! String
}
}
在APPdelegate
中調(diào)用摘悴,建議在Window
加載前調(diào)用蹂喻,就不會出現(xiàn)因為加載過快導致第一個界面語言沒有國際化的現(xiàn)象
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
InternationalTool.shared.initUserLanguage()
window = UIWindow(frame: UIScreen.main.bounds)
window?.backgroundColor = UIColor.white
window?.rootViewController = ViewController()
window?.makeKeyAndVisible()
return true
}
加載文字時口四,直接調(diào)用工具類的方法即可
label.text = InternationalTool.shared.string("國際化語言展示")
創(chuàng)建界面進行手動切換語言
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let label = UILabel(frame: CGRect(x: 20, y: 200, width: UIScreen.main.bounds.width - 40, height: 40))
label.backgroundColor = UIColor.lightGray
label.text = InternationalTool.shared.string("國際化語言展示")
label.textColor = UIColor.red
label.textAlignment = .center
label.font = UIFont.boldSystemFont(ofSize: 16)
view.addSubview(label)
let button = UIButton(type: .custom)
button.frame = CGRect(x: 40, y: 260, width: 140, height: 50)
button.backgroundColor = UIColor.purple
button.setTitle(InternationalTool.shared.string("英文"), for: .normal)
button.setTitleColor(UIColor.white, for: .normal)
button.addTarget(self, action: #selector(buttonClick), for: .touchUpInside)
view.addSubview(button)
let button02 = UIButton(type: .custom)
button02.frame = CGRect(x: 200, y: 260, width: 140, height: 50)
button02.backgroundColor = UIColor.purple
button02.setTitle(InternationalTool.shared.string("中文"), for: .normal)
button02.setTitleColor(UIColor.white, for: .normal)
button02.addTarget(self, action: #selector(button02Click), for: .touchUpInside)
view.addSubview(button02)
}
@objc func buttonClick(_ sender: UIButton) {
InternationalTool.shared.setLanguage(.English)
UIApplication.shared.keyWindow?.rootViewController = ViewController()
}
@objc func button02Click(_ sender: UIButton) {
InternationalTool.shared.setLanguage(.Chinese)
UIApplication.shared.keyWindow?.rootViewController = ViewController()
}
}
具體效果如下
6 App名稱國際化
創(chuàng)建Strings File
文件,并命名為InfoPlist.strings
選中創(chuàng)建好的文件赤嚼,點擊右側(cè)Localize...
在彈出的窗口中點擊Localize
在右側(cè)勾選語言
配置InfoPlist.strings
CFBundleDisplayName = ""
效果如下