i18nSwift 0.3.1

i18nSwift 0.3.1

测试测试过的
Lang语言 SwiftSwift
许可证 MIT
发布最后一次发布2017年4月
SwiftSwift版本3.1
SPM支持SPM

sgr-ksmt 维护。



i18nSwift 0.3.1

  • 作者:
  • Suguru Kishimoto

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)

功能

  • Swift 风格处理
  • 本地化
    • 安全性,
    • 静态类型
    • 不切换系统语言设置即可更改语言。
  • 货币(使用地区将数字转换为货币)
  • 测试完美。

待办事项

  • [ ] 数字
  • [ ] 日期
  • [ ] 复数

要求

  • Swift 3.x
  • Xcode 8.x
  • iOS 9.0 或更高版本

使用方法

本地化

  • 创建 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

通信

  • 如果您发现了bug,请创建一个问题。
  • 如果您有功能请求,请创建一个问题。
  • 如果您想贡献力量,请提交一个pull请求。💪

许可证

i18nSwift 使用MIT许可证。更多信息请参阅LICENSE文件。