https://github.com/dankinsoid/swiftlocalize
Native Swift localization
https://github.com/dankinsoid/swiftlocalize
Last synced: 10 months ago
JSON representation
Native Swift localization
- Host: GitHub
- URL: https://github.com/dankinsoid/swiftlocalize
- Owner: dankinsoid
- License: mit
- Created: 2019-08-07T10:57:43.000Z (almost 7 years ago)
- Default Branch: release
- Last Pushed: 2024-05-31T12:29:38.000Z (about 2 years ago)
- Last Synced: 2025-08-17T09:47:56.164Z (10 months ago)
- Language: Swift
- Size: 75.5 MB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# SwiftLocalize
[](https://travis-ci.org/Voidilov/SwiftLocalize)
[](https://cocoapods.org/pods/SwiftLocalize)
[](https://cocoapods.org/pods/SwiftLocalize)
## Description
Library for native Swift localization of your projects.
## Example
```swift
import Foundation
import SwiftLocalize
public extension String {
@Localized public var ok: String {
[.ru: "Да",
.en: "Ok"]
}
@Localized public var cancel: String {
[.ru: "Отмена",
.en: "Cancel"]
}
@Localized public var never: String {
[.ru: "Никогда",
.en: "Never"]
}
@Localized public var later: String {
[.ru: "Позже",
.en: "Later"]
}
public static func coins(for count: Int) -> String {
Localized([
.ru: [
.cases(NumberCase.accusative): "монеты",
.cases(NumberCase.singular): "монета",
.cases(NumberCase.genitive): "монет"
]
]).string(.cases(NumberCase(for: count)))
}
public static let errors: Localized.Dictionary = [
"unknown": [.ru: "Неизвестная ошибка", .en: "Unknown error"],
"server": [.ru: "Ошибка сервера", .en: "Server error"]
]
}
```
## Usage
To get a localized string create `Localized` object:
```swift
let word = Localized(formsDictionary)
```
where
`string: String` - default value,
`formsDictionary: [Language: Localized.Forms]` - dictionary of forms
To get a string for current language use `word.localized`
To get for a custom language or form call
```swift
word.string(language, form)
```
where
`language: Language` - language, default value is Language.current,
`form: FormType` - word form (`OptionSet`)
Supported forms: none, singular, plural, masculine, feminine, neuter, common and any combination of them.
You can create your own form type (for language cases as example) via `LanguageCaseProtocol` and use it:
```swift
let formType = Localized.FormType.cases(customFormEnum)
```
The repo contains one custom `LanguageCaseProtocol` type `NumberCase` for Russian language as example of usage.
Examples of word with several forms:
```swift
let manWord = Localized([
.ru: [.singular: "человек", .plural: "люди"],
.en: [
[.singular, .masculine]: "man",
[.plural, .masculine]: "men",
[.singular, .feminine]: "woman",
[.plural, .feminine]: "women"
],
.ja: "人"
])
```
You can combine words to get phrases:
```swift
let tree = Localized([
.ru: [
[.neuter, .singular]: "дерево",
.plural: "деревья"
]
])
let beautiful = Localized([
.ru: [
.plural: "красивые",
.singular: [.masculine: "красивый", .feminine: "красивая", .neuter: "красивое"]
]
])
let phrase = beautiful + " " + tree
print(phrase.string(language: .ru, .plural))
//prints "красивые деревья"
print(phrase.string(language: .ru, .singular))
//prints "красивое дерево"
```
## Installation
1. [Swift Package Manager](https://github.com/apple/swift-package-manager)
Create a `Package.swift` file.
```swift
// swift-tools-version:5.0
import PackageDescription
let package = Package(
name: "SomeProject",
dependencies: [
.package(url: "https://github.com/dankinsoid/SwiftLocalize.git", from: "1.9.0")
],
targets: [
.target(name: "SomeProject", dependencies: ["SwiftLocalize"])
]
)
```
```ruby
$ swift build
```
2. [CocoaPods](https://cocoapods.org)
Add the following line to your Podfile:
```ruby
pod 'SwiftLocalize'
```
and run `pod update` from the podfile directory first.
## Author
Voidilov, voidilov@gmail.com
## License
SwiftLocalize is available under the MIT license. See the LICENSE file for more info.