https://github.com/sgr-ksmt/i18nswift
i18n for Swift
https://github.com/sgr-ksmt/i18nswift
carthage cocoapods currency framework i18n localization swift swift-library swift3
Last synced: 23 days ago
JSON representation
i18n for Swift
- Host: GitHub
- URL: https://github.com/sgr-ksmt/i18nswift
- Owner: sgr-ksmt
- License: mit
- Created: 2017-03-26T05:01:23.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2020-03-25T04:11:05.000Z (about 5 years ago)
- Last Synced: 2025-04-13T14:07:39.953Z (23 days ago)
- Topics: carthage, cocoapods, currency, framework, i18n, localization, swift, swift-library, swift3
- Language: Swift
- Homepage:
- Size: 220 KB
- Stars: 4
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
![]()
i18n for Swift.
[](https://travis-ci.org/sgr-ksmt/i18nSwift)
[](https://github.com/sgr-ksmt/i18nSwift/releases)

[](https://github.com/Carthage/Carthage)
[](https://cocoapods.org/pods/i18nSwift)
[](https://cocoapods.org/pods/i18nSwift)
[](https://codecov.io/gh/sgr-ksmt/i18nSwift)```swift
"order_view_title" = "Order"
"order_view_button" = "Order (price: %@)"
==========================// "Order"
titleLabel.text = NSLocalizedString("order_view_title", comment: "")
// "Order (price: USD100.00)"
let price = "$100.00"
let buttonTitle = String(format: NSLocalizedString("order_view_button", comment: ""), arguments: price)
orderButton.setTitle(buttonTitle, for: .normal)⬇️⬇️⬇️
// "Order"
titleLabel.text = i18n.t(.orderViewTitle)
// "Order (price: USD100.00)"
orderButton.setTitle(i18n.t(.orderViewButton, args: i18n.currencyISO(100)), for: .normal)
```## Features
- Swifty handling
- Localization
- **Safety**,
- **Static typing**
- Change language without switching system language setting.
- Currency (convert number into currency using locale)
- Tested perfectly.### TODO
- [ ] Number
- [ ] Date
- [ ] Pluralization## Requirements
- Swift 3.x
- Xcode 8.x
- iOS 9.0 or later## Usage
### Localization
- **Create `Localizable.strings`**
```swift
"hello_world" = "Hello, world!";
"total_count" = "Total: %d";
```Add `Localizable.strings` in your project and define keys and values.
- **Define `LocalizedString`**
```swift
extension i18n.LocalizedString {
static let helloWorld: i18n.LocalizedString = "hello_world"
static let totalCount: i18n.LocalizedString = "total_count"
// or
static let helloWorld = i18n.LocalizedString(rawValue: "hello_world")
}
```Add `LocalizedString`'s static variable as extension of `i18n.LocalizedString`.
It's similar to `Notification.Name`. :heart:```swift
extension Notification.Name {
static let fooDidUpdate = Notification.Name(rawValue: "FooDidUpdate")
}
```- **localize with `i18n`**
```swift
let message = i18n.t(.helloWorld)
print(message) // "Hello, world!// localize and embed paramter(s)
let total = i18n.t(.totalCount, args: 100)
print(total) // "Total: 100"
```**Tips** Add `args:` label and parameter(s) if you can embed arguments in localized string.
## Language
**i18nSwift** equips function of changing the language in appllication.
If you change the language, result of localization will change.```swift
// en
"greeting" = "Hello!";
// fr
"greeting" = "Bonjour!";
``````swift
// system language is "en"
print(i18n.t(.greeting)) // "Hello!"// Change language to "fr"
i18n.Language.current = "fr"
print(i18n.t(.greeting)) // "Bonjour!"// ===================
// Reboot application!!
// ===================// `Language.current` is being stored.
print(i18n.t(.greeting)) // "Bonjour!"// Reset to system language
i18n.Language.reset()// change temporary in localization
print(i18n.t(.greeting, "fr")) // "Bonjour!"
```**Tips** : `i18n.Language.current` is saved in UserDefaults using `i18n.Language.Constant.currentLanguageKey`
### Currency
**i18nSwift** converts number into localized currency.```swift
// default is `Locale.current`print(i18n.currency(100)) // $100.00
print(i18n.currency(100, Locale(identifier: "ja_JP"))) // ¥100
print(i18n.currency(100, fractionDigits: (4, 4))) // $100.0000
print(i18n.currencyISO(100)) // USD100.00
```## Installation
### Carthage
- Add the following to your *Cartfile*:
```bash
github "sgr-ksmt/i18nSwift" ~> 0.3
```- Run `carthage update`
- Add the framework as described.
Details: [Carthage Readme](https://github.com/Carthage/Carthage#adding-frameworks-to-an-application)### CocoaPods
**i18nSwift** is available through [CocoaPods](http://cocoapods.org). To install
it, simply add the following line to your Podfile:```ruby
pod 'i18nSwift', '~> 0.3'
```and run `pod install`
### Manually Install
Download all `*.swift` files and put your project.## Change log
Change log is [here](https://github.com/sgr-ksmt/i18nSwift/blob/master/CHANGELOG.md).## Communication
- If you found a bug, open an issue.
- If you have a feature request, open an issue.
- If you want to contribute, submit a pull request.:muscle:## License
**i18nSwift** is under MIT license. See the [LICENSE](LICENSE) file for more info.