SwiftyLocalization 版本号 1.0.0

SwiftyLocalization 1.0.0

Serj Agopian 维护。



  • 作者
  • Serj

SwiftyLocalization Logo

Swift 5.0 Version Platform Carthage Compatible SPM

SwiftyLocalization

Swifty Localization 帮助您更容易组织和管理本地化字符串。

功能

  • 通过本地化键定义枚举并本地化它们
  • 通过强类型参数安全地定义枚举,并获得复数化和参数化本地化

示例

示例应用程序是查看 SwiftyLocalization 在行动中的最佳方式。只需打开 SwiftyLocalization.xcodeproj 并运行 Example 计划。

安装

CocoaPods

SwiftyLocalization 可通过 CocoaPods 获得。要安装它,只需将以下行添加到您的 Podfile 中

pod 'SwiftyLocalization'

Carthage

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

要使用 Carthage 将 SwiftyLocalization 集成到您的 Xcode 项目中,请在您的 Cartfile 中指定它。

github "serjooo/SwiftyLocalization"

运行 carthage update 以构建框架,并将构建好的 SwiftyLocalization.framework 拖入您的 Xcode 项目。

在您的应用程序目标的“构建阶段”设置选项卡中,点击“+”图标并选择“新运行脚本阶段”,然后添加在 Carthage 入门步骤 4、5 和 6 中提到的框架路径。

Swift 包管理器

要使用 Apple 的 Swift 包管理器 集成,请将以下内容添加到您的 Package.swift 中,作为依赖项。

dependencies: [
    .package(url: "https://github.com/serjooo/SwiftyLocalization.git", from: "1.0.0")
]

或者,打开您的 Xcode 项目,选择 Swift Packages 并点击 + 图标以搜索 SwiftyLocalization

手动

如果您不希望使用上述任何依赖管理器,可以直接将 SwiftyLocalization 集成到您的项目中。只需将 文件夹拖入您的 Xcode 项目中。

使用

有两个主要的协议帮助简化本地化。

可本地化

在您想要定义一组键,并将它们映射到某个 Strings 文件中的翻译时,请使用可本地化。

可本地化依赖于枚举实现 RawRepresentable 并拥有 rawValueString 类型的值,因此使用 String 类型的枚举是开始入门最简单的方式。

enum Season: String, Localzable {
    case summer
    case winter
    case autumn
    case spring

    // Conformance Requirements:

    // This is the name of the file where the translations exists. In this case `Seasons.strings`
    var filename: String { "Seasons" }
}

Localizable 的以下属性具有默认实现:

/// The bundle in which the Localization File exists in
/// Default Value: .main
var bundle: Bundle { get }

/// The enum String case localized
/// This is how the enum cases are translated
var localized: String { get }

/// The fallback value in case localization fails
/// Default Value: "**\(rawValue)**"
var fallbackValue: String { get }

要获取本地化字符串,您只需在枚举上调用 localized 方法,在本例中为 Season.summer.localized

Self: LocalizedError 时,Localizable 还有一个扩展功能:它通过返回那些值的 localized 实现 localizedDescriptionerrorDescription

那么 localized 是如何本地化这些值的呢?在 String 上添加了以下辅助扩展:

extension String {
    /// Localizes a String
    /// - Parameter bundle: The Bundle where the localiztion file exists. Defaults to .main
    /// - Parameter tableName: The Localization file name.
    /// - Parameter value: The fallback value if localization fails.
    /// - returns: The localized string
    func localized(bundle: Bundle = .main, tableName: String? = nil, value: String) -> String {
        return NSLocalizedString(self, tableName: tableName, bundle: bundle, value: value, comment: "")
    }
}

PlaceholderLocalizable

该协议最适用于主要本地化字符串也有占位符时。要本地化,该协议使用枚举的 case name 作为键,然后遍历该枚举用例的每个 associatedValue 并将其传递给 localizedString。这是通过使用镜像 API 完成的。

// Becareful to not inherit String here as the protocol already
// defines the default implementation for it
enum CheckoutString: PlaceholderLocalizable {
    // Define type safe enum cases to get the localized string
    // with the placeholders replaced with the values passed
    case buyBooks(count: Int)

    // Always have to provide to the protocol from where
    // it could find the localized string
    var fileName: String { "Checkout" }

    // If you need to define a custom key override rawValue
    // and provide your own key.
    var rawValue: String {
        switch self {
        case .buyBooks:
            return "checkout_buy_books"
        }
    }
}

PlaceholderLocalizable 继承自 Localizable,因此它也继承了一些默认实现,这些实现当然可以被覆盖。

贡献

贡献非常欢迎🙌

许可协议

SwiftyLocalization
Copyright (c) 2019 SwiftyLocalization [email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.