Localize 是一个用 Swift 编写的框架,旨在帮助您本地化和复数化您的项目。它同时支持 Storyboard 和字符串。
CocoaPods 是 Cocoa 项目的依赖管理器。您可以使用以下命令安装它
gem install cocoapods
需要 CocoaPods 1.1.0+ 才能构建 Localize 1.+。
要使用 CocoaPods 将 Localize 集成到您的 Xcode 项目中,请在您的 Podfile
中指定它
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '9.0'
use_frameworks!
target '<Your Target Name>' do
pod 'Localize' , '~> 2.3.0'
end
# If you are using Swift 4.x
# target '<Your Target Name>' do
# pod 'Localize' , '~> 2.1.0'
# end
然后,运行以下命令
pod install
Carthage 是一个去中心化的依赖管理器,它可以构建您的依赖项并为您提供二进制框架。
您可以使用 Homebrew 通过以下命令安装 Carthage
brew update
brew install carthage
要使用 Carthage 将 Localize 集成到您的 Xcode 项目中,请在您的 Cartfile
中指定它
github "andresilvagomez/Localize"
运行 carthage update
来构建框架并将构建的 Localize.framework
拖到您的 Xcode 项目中。
Swift Package Manager 是一种用于自动化 Swift 代码分发的工具,并已集成到 Swift 编译器中。
设置好 Swift 包后,将 Localize 添加为依赖项就像将其添加到 Package.swift 的 dependencies 值一样简单。
dependencies: [
.Package(url: "https://github.com/andresilvagomez/Localize.git")
]
如果您想本地化,请为任何 String
添加 .localize()
。
您无需在代码中导入任何内容,Localize 使用扩展来本地化您的字符串。
textLabel.text = "hello.world".localize()
// Or
textLabel.text = "hello.world".localized
您可以决定是否使用 JSON 或 Apple Strings,我们都支持。如果您决定使用 JSON,请按照以下说明操作。
请在您的代码中创建一个带有此规则的 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" : ""
}
如果您决定使用 Apple strings,请遵循 Apple 本地化指南 来创建字符串文件。
String 文件示例
"hello.world" = "Hello world!";
"name" = "Hello %";
"values" = "Hello everyone my name is % and I'm %, see you soon";
"username" = "My username is :username";
"level.one.two.three" = "This is a multilevel key";
"the.same.lavel" = "This is a localized in the same level";
"enlish" = "This key only exist in english file.";
无论您选择哪种方式,都使用该方法。
print( "hello.world".localize() )
// Hello world!
// Also you can use
print( "hello.world".localized )
Localize 使用 %
标识符来替换文本
print( "hello.name".localize(value: "everyone") )
// Hello everyone!
Localize 使用 %
标识符来替换文本
print( "values".localize(values: "everyone", "Software Developer") )
// Hello everyone we are Software Developer, see you soon
Localize 使用 :yourid
在 JSON 文件中搜索您的 ID
print( "username".localize(dictionary: ["username": "Localize"]) )
// My username is Localize
如果您决定使用不同的文件,请使用以 tableName
结尾的每个方法,例如。
print( "hello.world".localize(tableName: "Other") )
print( "hello.name".localize(value: "everyone", tableName: "Errors") )
print( "values".localize(values: "everyone", "Software Developer", tableName: "YourFileName") )
print( "username".localize(dictionary: ["username": "Localize"], tableName: "YourFileName") )
您无需在代码中导入任何内容,Localize 使用扩展来本地化您的 UIView 组件
要阻止对您在 storyboard 中创建的某些控件进行自动本地化,可以将 Auto Localize 设置为 Off
{
"navigation" : {
"title" : "Localize"
},
"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
当您更改语言时,所有视图都会自动将您的内容更新为新语言
Localize.update(language: "fr")
要使此功能适用于字符串,您需要实现一个通知
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(localize), name: NSNotification.Name(localizeChangeNotification), 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 Localize.availableLanguages {
let displayName = Localize.displayNameForLanguage(language)
let languageAction = UIAlertAction(title: displayName, style: .default, handler: {
(alert: UIAlertAction!) -> Void in
Localize.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)
}
这不是必需的,仅当您需要不同的结果时才需要。
// AppDelegate.swift
import Localize
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let localize = Localize.shared
// Set your localize provider.
localize.update(provider: .json)
// Set your file name
localize.update(fileName: "lang")
// Set your default language.
localize.update(defaultLanguage: "fr")
// If you want change a user language, different to default in phone use thimethod.
localize.update(language: "en")
// If you want remove storaged language use
localize.resetLanguage()
// The used language
print(localize.currentLanguage)
// List of available language
print(localize.availableLanguages)
// Or you can use static methods for all
Localize.update(fileName: "lang")
Localize.update(defaultLanguage: "fr")
Localize.update(language: "en-DE")
return true
}
print( "people".pluralize(value: 0) )
// there are no people
print( "people".pluralize(value: 1) )
// only one person
print( "people".pluralize(value: 2) )
// two people
print( "people".pluralize(value: 27) )
// many people
print( "people".pluralize(value: 103) )
// hundreds of people
print( "people".pluralize(value: 1010) )
// thousand of people
print( "people".pluralize(value: 1000000) )
// millions of people
你需要如何编写你的文件。
// Json file
{
"people": {
"zero": "there are no people",
"one": "only one person",
"two": "two people",
"many": "many people",
"hundreds": "hundreds of people",
"thousand": "thousand of people",
"millions": "millions of people",
"other": "not defined people"
}
}
# string file
"people.zero" = "there are no people";
"people.one" = "only one person";
"people.two" = "two people";
"people.many" = "many people";
"people.hundreds" = "hundreds of people";
"people.thousand" = "thousand of people";
"people.millions" = "millions of people";
"people.other" = "not defined people";
但你也可以显示你的值
print( "people".pluralize(value: 1) )
/// 1 Person
在你的文件中
// JSON
{
"people": {
"one": "% Person",
...
}
}
// Strings
"people.one" = "% Person";
要使您已本地化应用程序的所有语言在 AppStore 上可见,您必须在项目的设置中添加一种语言。
特别感谢 Benjamin Erhart
Localize 在 MIT 许可证下发布。有关详细信息,请参见 LICENSE。