本地化 2.3.0

Localize 2.3.0

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布上次发布2019年4月
SPM支持 SPM

Andres Silva 维护。



Localize 2.3.0

  • Andres Silva

Localize

Carthage compatible codecov.io CocoaPods Build Status Language GitHub license Awesome

Localize 是一个 Swift 编写的框架,用于帮助您本地化和复数化您的项目。它支持故事板和字符串。

Localize Storyboard


功能

  • 使用 IBInspectable 的故事板
  • 对键进行复数化和本地化
  • 保留您的应用程序已使用的 File.strings 文件
  • 支持 Apple 字符串和 JSON 文件
  • 在不更改设备语言的情况下更改应用程序语言
  • 无需额外文件或 ID 就可以本地化 Storyboards

要求

  • iOS 9.0+
  • Xcode 8.0+
  • Swift 3.0+

安装

苹果Pods

苹果Pods是一个Cocoa项目依赖管理器。您可以使用以下命令安装它

gem install cocoapods

确保您的苹果Pods版本为1.1.0+,这样才能构建Localize 1.+。

要将Localize整合到您的Xcode项目中使用苹果Pods,请在您的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

Carthage是一款去中心化的依赖管理器,它构建您的依赖并为您提供二进制框架。

您可以使用以下命令通过Homebrew安装Carthage

brew update
brew install carthage

要将Localize整合到您的Xcode项目中使用Carthage,请在您的Cartfile中指定它

github "andresilvagomez/Localize"

运行carthage update以构建框架,并将构建的Localize.framework拖动到您的Xcode项目。

Swift包管理器

Swift包管理器是一种用于自动化Swift代码分发工具,并与Swift编译器集成。

一旦您设置了Swift包,将Localize添加为依赖关系就像将其添加到Package.swift中依赖项值一样简单。

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字符串,我们支持两者。如果您决定使用JSON,请按照以下步骤进行。

创建 JSON 文件

请按照以下规则在您的代码中创建一个 JSON 文件

{your file name}-{your lang code}.json

例如

  • lang-en.json
  • lang-es.json
  • lang-fr.json

JSON 文件示例

{
    "hello" : {
        "world" : "Hello world!",
        "name" : "Hello %!"
    },
    "values" : "Hello % we are %, see you soon",
    "username" : "My username is :username",
    "navigation.title" : ""
}

创建字符串文件

如果您决定使用苹果字符串,请遵循苹果本地化指南来创建字符串文件。

字符串文件示例


"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 )

本地化字符串,替换文本

使用 % 标识符来替换文本

print( "hello.name".localize(value: "everyone") )

// Hello everyone!

本地化字符串,替换多个文本

使用 % 标识符来替换文本

print( "values".localize(values: "everyone", "Software Developer") )

// Hello everyone we are Software Developer, see you soon

本地化字符串,替换字典值

本地化使用 :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") )

我们在Storyboard方面表现惊人

您不需要在代码中导入任何内容,Localize使用扩展来本地化您的UIView组件

为了防止自动本地化某些在Storyboard中创建的控件,可以将 Auto Localize 设置为 Off

Localize Storyboard

  • lang-en.json
{
    "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 languaje.
    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 languaje use
    localize.resetLanguage()
    // The used language
    print(localize.currentLanguage)
    // List of aviable languajes
    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发行版编写说明

要使您本地化应用的所有语言在AppStore上可见,您必须在项目的设置中添加一种语言。

  1. 为此,请在左侧侧栏中单击您的项目名称。
  2. 然后,选择项目,而不是目标。
  3. 在底部,在“本地化”下面,按+按钮并选择要添加的语言
  4. 在提示时,取消选中Xcode想要添加本地化的所有文件,但保留一个实际不会本地化的文件,例如您的启动画面。
    • 如果您需要本地化所有文件,我建议添加一个占位符Storyboard文件,然后将其添加到本地化中
  5. 完成!(您实际上不需要本地化占位符文件。)现在AppStore将显示您应用本地化中的新语言。

致谢

Andres Silva Gomez

特此感谢 Benjamin Erhart

许可证

Localize在MIT许可证下发布。有关详细信息,请参阅LICENSE。