L18n
关于
L18n 是一个用于 iOS 应用的本地化框架,允许本地化 Storyboards 和源代码。
备注
为了避免导出或导入 .xiff
文件时出现问题,请取消选中 使用基本国际化,并使用其他语言如 en
作为基础。
您的 Storyboard 不需要按每种本地化语言拆分,您可以选择默认设置,如以下图片所示,以避免管理相同 Storyboard 的多个副本来进行管理。
示例
要运行示例项目,请克隆仓库,然后首先从 Example 目录中运行 pod install
如何在 Storyboard 中使用 L18n
所有 Storyboard 对象在检查器属性中公开以下字段
- 包标识符:此字段允许您使用指定的标识符创建包的实例,生成的包用于本地化;如果您没有定义标识符,则默认使用
Bundle.main
- 表名:它允许定义用于本地化的表,如果没有定义值,则默认使用
Localizable
- 默认语言:允许您定义基本语言,如果在当前手机语言中没有要本地化的键,我们将使用默认语言;默认情况下,我们使用
en
(英语en.lproj
)
UIBarItem 的子类
属性检查器允许定义与元素标题相关联的键
UIButton
属性检查器允许定义与按钮的每个可能状态 normal
、highlighted
、selected
、disabled
相关联的 title
键
UILabel
属性检查器允许定义与标签标题相关联的键
UINavigationItem
属性检查器允许定义与元素 title
和 prompt
相关联的键
UISearchBar
属性检查器允许定义与 title
、placeholder
和 prompt
相关联的键
属性检查器允许定义与段标题的键相关联的键,每个键用 ,
分隔
UISegmentedControl
UITextField
属性检查器能够定义与text
和placeholder
关联的键
UIViewController
属性检查器允许定义与ViewController标题以及底部标签栏相关元素的标题关联的键
如何在源代码中使用L18n
定义你的字符串
MessagesStrings.strings
"welcome_message_key" = "Welcome to use L18n %@ %@";
"bye_message_key" = "See you soon";
创建一些类型(优先使用枚举)以符合 L18n
MessagesStrings.swift
import L18n
enum MessagesStrings: String, L18n{
case welcomeMessage = "welcome_message_key"
case byeMessage = "bye_message_key"
//var tableName: String = "TableName" //If no defined on code L18n will infer the table name using the same name that this instance 'MessagesStrings'
//baseLanguage: String = "es" // If no defined L18n will use by default 'en'
//bundle: Bundle = MiFramework.bundle // If no defined L18n will use by default 'Bundle.main'
// Return the localization value based on self, If no defined L18n will return the localized value based on this instance's values
//public var localized: String {
// return rawValue.localized(bundle: self.bundle, tableName: self.tableName, baseLanguage: self.baseLanguage)
//}
}
使用符合 L18n 的实例
let welcomeMessage = MessagesStrings.localize(.welcomeMessage, ["Mr.", "iOS Developer"])
print(welcomeMessage) //Will print "Welcome to use L18n Mr. iOS Developer"
let byeMessage = MessagesStrings.localize(.byeMessage)
print(byeMessage) //Will print "See you soon"
let byeMessageAlt = MessagesStrings.byeMessage.localized
print(byeMessageAlt) //Will print "See you soon"
要求
- iOS 8+
- Swift 4.0
安装
CocoaPods
L18n 可以通过 CocoaPods 获得。要安装它,只需将以下行添加到您的 Podfile 中
pod 'L18n'
作者
Dani Manuel Céspedes Lara, [email protected]
许可证
L18n 在 MIT 许可证下提供。有关更多信息,请参阅 LICENSE 文件。