JsonLocalizable 1.1

JsonLocalizable 1.1

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布最新发布2017年1月
SwiftSwift 版本3.0
SPM支持 SPM

Andres Silva 维护。



  • Kekiiwaa

JsonLocalizable

JsonLocalizable 是一个使用 Swift 编写的框架,旨在简化项目的本地化,包括故事板和字符串。

JsonLocalizable Storyboard


特性

  • [x] 本地化字符串
  • [x] 无额外文件本地化 Storyboards
  • [x] 无 Xcode UIView ids 本地化 UIView 组件
  • [x] 仅使用键本地化 UIView 组件
  • [x] 使用类本地化 UIView 组件
  • [x] 更新当前语言并更新所有视图组件
  • [x] 更新当前语言并接收通知

要求

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

安装

Swift 包管理器

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

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

dependencies: [
    .Package(url: "https://github.com/Kekiiwaa/JsonLocalizable.git")
]


使用方法

配置

这并非必需,只在你需要不同结果时才需要。

// AppDelegate.swift

import JsonLocalizable

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    let localize = Localizable.shared
    // Set your file name
    localize.fileName = "myFileName"
    // Set your default languaje.
    localize.defaultLanguage = .french
    // If you want change a user language different to default in device, use this method.
    localize.update(language: .english)
    // If you want remove storaged languaje, use
    localize.resetLanguage()
    // The used language that you configured to localize
    print(localize.language())
    // List of storaged languajes
    print(localize.languages())
    // List of aviable languajes
    print(localize.availableLanguages())

    return true
}

创建 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" : ""
}

本地化字符串

print( "hello.world".localize() )

// Hello world!

本地化字符串,替换文本

JsonLocalizable 使用 % 标识符来替换文本

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

// Hello everyone!

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

JsonLocalizable 使用 % 标识符来替换文本

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

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

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

JsonLocalizable使用:yourid在JSON文件中搜索你的ID

print( "username".localize(dictionary: ["username": "JsonLocalizable"]) )

// My username is JsonLocalizable


使用扩展本地化你的Storyboard

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

// This is lang-en.json
{
    "navigation" : {
        "title" : "JsonLocalizable"
    },
    "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."
    }
}

JsonLocalizable Storyboard

这是你在模拟器中的结果

JsonLocalizable Simulator

你可以使用这些扩展:

  • UIBarButtonItem
  • UIButton
  • UILabel
  • UINavigationItem
  • UISearchBar
  • UISegmentedControl
  • UITabBarItem
  • UITextField
  • UITextView


使用类本地化你的Storyboard

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

// This is lang-en.json
{
    "navigation" : {
        "title" : "JsonLocalizable"
    },
    "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."
    }
}

JsonLocalizable Storyboard

这是你在模拟器中的结果

JsonLocalizable Simulator

你可以使用以下类:

  • LocalizableBarButtonItem
  • LocalizableButton
  • LocalizableLabel
  • LocalizableNavigationBarItem
  • LocalizableSearchBar
  • LocalizableSegmentedControler
  • LocalizableBarItem
  • LocalizableTextField
  • LocalizableTextView


更新语言

当你更改语言时,所有视图将自动更新到新语言的内容

let localize = Localize.shared
localize.update(language: .french)

但是对于字符串,这是不可能的,因此你需要实现一个通知

override func viewDidLoad() {
    super.viewDidLoad()
    NotificationCenter.default.addObserver(self, selector: #selector(localize), name: NSNotification.Name(LanguageChangeNotification), 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 Localizable.shared.availableLanguages() {
        let displayName = Localizable.shared.displayNameForLanguage(language)
        let languageAction = UIAlertAction(title: displayName, style: .default, handler: {
            (alert: UIAlertAction!) -> Void in
            Localizable.shared.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)
}


鸣谢

Kekiiwaa IncAndres Silva GomezAndres Felipe Montoya

许可证

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