测试测试过的 | ✓ |
Lang语言 | SwiftSwift |
许可证 | MIT |
发布最后一次发布 | 2017年4月 |
SwiftSwift版本 | 3.1 |
SPM支持SPM | ✗ |
由 sgr-ksmt 维护。
Swift 的国际化。
"order_view_title" = "Order"
"order_view_button" = "Order (price: %@)"
==========================
// "Order"
titleLabel.text = NSLocalizedString("order_view_title", comment: "")
// "Order (price: USD100.00)"
let price = "$100.00"
let buttonTitle = String(format: NSLocalizedString("order_view_button", comment: ""), arguments: price)
orderButton.setTitle(buttonTitle, for: .normal)
⬇️⬇️⬇️
// "Order"
titleLabel.text = i18n.t(.orderViewTitle)
// "Order (price: USD100.00)"
orderButton.setTitle(i18n.t(.orderViewButton, args: i18n.currencyISO(100)), for: .normal)
Localizable.strings
"hello_world" = "Hello, world!";
"total_count" = "Total: %d";
将 Localizable.strings
添加到您的项目中,并定义键和值。
LocalizedString
extension i18n.LocalizedString {
static let helloWorld: i18n.LocalizedString = "hello_world"
static let totalCount: i18n.LocalizedString = "total_count"
// or
static let helloWorld = i18n.LocalizedString(rawValue: "hello_world")
}
将 LocalizedString
的静态变量作为 i18n.LocalizedString
的扩展。
它与 Notification.Name
类似。
extension Notification.Name {
static let fooDidUpdate = Notification.Name(rawValue: "FooDidUpdate")
}
i18n
本地化let message = i18n.t(.helloWorld)
print(message) // "Hello, world!
// localize and embed paramter(s)
let total = i18n.t(.totalCount, args: 100)
print(total) // "Total: 100"
提示 如果您可以将参数嵌入到本地化字符串中,请添加 args:
标签和参数。
i18nSwift 为应用程序提供更改语言的功能。
如果您更改了语言,本地化的结果将更改。
// en
"greeting" = "Hello!";
// fr
"greeting" = "Bonjour!";
// system language is "en"
print(i18n.t(.greeting)) // "Hello!"
// Change language to "fr"
i18n.Language.current = "fr"
print(i18n.t(.greeting)) // "Bonjour!"
// ===================
// Reboot application!!
// ===================
// `Language.current` is being stored.
print(i18n.t(.greeting)) // "Bonjour!"
// Reset to system language
i18n.Language.reset()
// change temporary in localization
print(i18n.t(.greeting, "fr")) // "Bonjour!"
提示 : 使用 i18n.Language.current
,并通过 i18n.Language.Constant.currentLanguageKey
保存到 UserDefaults。
i18nSwift 将数字转换为本地化货币。
// default is `Locale.current`
print(i18n.currency(100)) // $100.00
print(i18n.currency(100, Locale(identifier: "ja_JP"))) // ¥100
print(i18n.currency(100, fractionDigits: (4, 4))) // $100.0000
print(i18n.currencyISO(100)) // USD100.00
下载所有 *.swift
文件并将其放入您的项目中。
变更日志在这里:Change log。
i18nSwift 使用MIT许可证。更多信息请参阅LICENSE文件。