You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
2.9 KiB

  1. [![Platform](https://img.shields.io/cocoapods/p/Localize-Swift.svg?maxAge=2592000)](http://cocoapods.org/?q=Localize-Swift)
  2. [![Version](http://img.shields.io/cocoapods/v/Localize-Swift.svg)](http://cocoapods.org/?q=Localize-Swift)
  3. [![Build Status](https://travis-ci.org/marmelroy/Localize-Swift.svg?branch=master)](https://travis-ci.org/marmelroy/Localize-Swift)
  4. [![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)
  5. # Localize-Swift
  6. Localize-Swift is a simple framework that improves i18n and localization in Swift iOS apps - providing cleaner syntax and in-app language switching.
  7. <p align="center"><img src="http://i.imgur.com/vsrpqBt.gif" width="242" height="425"/></p>
  8. ## Features
  9. - Keep the Localizable.strings file your app already uses.
  10. - Allow your users to change the app's language without changing their device language.
  11. - Use .localized() instead of NSLocalizedString(key,comment) - a more Swifty syntax.
  12. - Generate your strings with a new genstrings swift/python script that recognises .localized().
  13. ## Usage
  14. Import Localize at the top of each Swift file that will contain localized text.
  15. If CocoaPods -
  16. ```swift
  17. import Localize_Swift
  18. ```
  19. Add `.localized()` following any `String` object you want translated:
  20. ```swift
  21. textLabel.text = "Hello World".localized()
  22. ```
  23. To get an array of available localizations:
  24. ```swift
  25. Localize.availableLanguages()
  26. ```
  27. To change the current language:
  28. ```swift
  29. Localize.setCurrentLanguage("fr")
  30. ```
  31. To update the UI in the view controller where a language change can take place, observe LCLLanguageChangeNotification:
  32. ```swift
  33. NotificationCenter.default.addObserver(self, selector: #selector(setText), name: NSNotification.Name(LCLLanguageChangeNotification), object: nil)
  34. ```
  35. To reset back to the default app language:
  36. ```swift
  37. Localize.resetCurrentLanguageToDefault()
  38. ```
  39. ## genstrings
  40. To support this new i18n syntax, Localize-Swift includes custom genstrings swift script.
  41. Copy the genstrings.swift file into your project's root folder and run with
  42. ```bash
  43. ./genstrings.swift
  44. ```
  45. This will print the collected strings in the terminal. Select and copy to your default Localizable.strings.
  46. The script includes the ability to specify excluded directories and files (by editing the script).
  47. ### Setting up with Carthage
  48. [Carthage](https://github.com/Carthage/Carthage) is a decentralized dependency manager that automates the process of adding frameworks to your Cocoa application.
  49. You can install Carthage with [Homebrew](http://brew.sh/) using the following command:
  50. ```bash
  51. $ brew update
  52. $ brew install carthage
  53. ```
  54. To integrate Localize-Swift into your Xcode project using Carthage, specify it in your `Cartfile`:
  55. ```ogdl
  56. github "marmelroy/Localize-Swift"
  57. ```
  58. ### Setting up with [CocoaPods](http://cocoapods.org/?q=Localize-Swift)
  59. ```ruby
  60. source 'https://github.com/CocoaPods/Specs.git'
  61. pod 'Localize-Swift', '~> 2.0'
  62. ```