测试已测试 | ✓ |
语言语言 | SwiftSwift |
许可证 | MIT |
发布最新发布 | 2017年1月 |
SwiftSwift 版本 | 3.0 |
SPM支持 SPM | ✓ |
由 Andres Silva 维护。
JsonLocalizable 是一个使用 Swift 编写的框架,旨在简化项目的本地化,包括故事板和字符串。
Swift 包管理器 是一个用于自动分发 Swift 代码的工具,并与 Swift 编译器集成。
一旦设置了 Swift 包,将 JsonLocalizable 添加为依赖项就像将其添加到 Package.swift 中的依赖项值一样简单。
dependencies: [
.Package(url: "https://github.com/Kekiiwaa/JsonLocalizable.git")
]
这并非必需,只在你需要不同结果时才需要。
// AppDelegate.swift
import JsonLocalizable
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let localize = Localizable.shared
// Set your file name
localize.fileName = "myFileName"
// Set your default languaje.
localize.defaultLanguage = .french
// If you want change a user language different to default in device, use this method.
localize.update(language: .english)
// If you want remove storaged languaje, use
localize.resetLanguage()
// The used language that you configured to localize
print(localize.language())
// List of storaged languajes
print(localize.languages())
// List of aviable languajes
print(localize.availableLanguages())
return true
}
请根据这一规则在您的代码中创建一个 JSON 文件
{your file name}-{your lang code}.json
例如
示例 JSON 文件
{
"hello" : {
"world" : "Hello world!",
"name" : "Hello %!"
},
"values" : "Hello % we are %, see you soon",
"username" : "My username is :username",
"navigation.title" : ""
}
print( "hello.world".localize() )
// Hello world!
JsonLocalizable 使用 %
标识符来替换文本
print( "hello.name".localize(value: "everyone") )
// Hello everyone!
JsonLocalizable 使用 %
标识符来替换文本
print( "values".localize(values: "everyone", "Software Developer") )
// Hello everyone we are Software Developer, see you soon
JsonLocalizable使用:yourid
在JSON文件中搜索你的ID
print( "username".localize(dictionary: ["username": "JsonLocalizable"]) )
// My username is JsonLocalizable
你不需要在代码中导入任何内容,JsonLocalizable使用扩展来本地化你的UIView组件
// This is lang-en.json
{
"navigation" : {
"title" : "JsonLocalizable"
},
"app" : {
"label" : "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium.",
"textfield" : "Write some here."
}
}
这是你在模拟器中的结果
你可以使用这些扩展:
UIBarButtonItem
UIButton
UILabel
UINavigationItem
UISearchBar
UISegmentedControl
UITabBarItem
UITextField
UITextView
你不需要在代码中导入任何内容,JsonLocalizable使用扩展来本地化你的UIView组件
// This is lang-en.json
{
"navigation" : {
"title" : "JsonLocalizable"
},
"app" : {
"label" : "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium.",
"textfield" : "Write some here."
}
}
这是你在模拟器中的结果
你可以使用以下类:
LocalizableBarButtonItem
LocalizableButton
LocalizableLabel
LocalizableNavigationBarItem
LocalizableSearchBar
LocalizableSegmentedControler
LocalizableBarItem
LocalizableTextField
LocalizableTextView
当你更改语言时,所有视图将自动更新到新语言的内容
let localize = Localize.shared
localize.update(language: .french)
但是对于字符串,这是不可能的,因此你需要实现一个通知
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(localize), name: NSNotification.Name(LanguageChangeNotification), object: nil)
}
public func localize() {
yourLabel.text = "app.names".localize(values: "mark", "henrry", "peater")
otherLabel.text = "app.username".localize(value: "Your username")
}
实现内部操作以更改语言
@IBAction func updateLanguage(_ sender: Any) {
let actionSheet = UIAlertController(title: nil, message: "app.update.language".localize(), preferredStyle: UIAlertControllerStyle.actionSheet)
for language in Localizable.shared.availableLanguages() {
let displayName = Localizable.shared.displayNameForLanguage(language)
let languageAction = UIAlertAction(title: displayName, style: .default, handler: {
(alert: UIAlertAction!) -> Void in
Localizable.shared.update(language: language)
})
actionSheet.addAction(languageAction)
}
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: {
(alert: UIAlertAction) -> Void in
})
actionSheet.addAction(cancelAction)
self.present(actionSheet, animated: true, completion: nil)
}
Kekiiwaa Inc,Andres Silva Gomez,Andres Felipe Montoya
JsonLocalizable在MIT许可证下发布。有关详细信息,请参阅LICENSE文件。