SwiftLocalize
Description
Library for native Swift localization of your projects.
Example
import Foundation
import SwiftLocalize
public enum Strings {
public static let ok = Word("Ok", [.ru: "Да"]).localized
public static let never = Word("Never", [.ru: "Никогда"]).localized
public static let later = Word("Later", [.ru: "Позже"]).localized
public static let error = Word("Error", [.ru: "Ошибка"]).localized
public static let coins = Word(
"coins", [
.ru: [
.cases(NumberCase.accusative): "монеты",
.cases(NumberCase.singular): "монета",
.cases(NumberCase.genitive): "монет"
]
])
//Strings.coins.string(.cases(NumberCase(for: someInt)))
public static let errors: Word.Dictionary = [
"unknown": [.ru: "Неизвестная ошибка", .en: "Unknown error"],
"server": [.ru: "Ошибка сервера", .en: "Server error"]
]
}
Usage
To get a localized string create Word
object:
let word = Word(string, formsDictionary)
where
string: String
- default value,
formsDictionary: [Language: Word.Forms]
- dictionary of forms
To get a string for current language use word.localized
To get for a custom language or form call
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:
let formType = Word.FormType.cases(customFormEnum)
The repo contains one custom LanguageCaseProtocol
type NumberCase
for Russian language as example of usage.
Examples of word with several forms:
let manWord = Word("man", [
.ru: [.singular: "человек", .plural: "люди"],
.en: [
[.singular, .masculine]: "man",
[.plural, .masculine]: "men",
[.singular, .feminine]: "woman",
[.plural, .feminine]: "women"
],
.ja: "人"
])
You can combine words to get phrases:
let tree = Word("tree", [
.ru: [
[.neuter, .singular]: "дерево",
.plural: "деревья"
]
])
let beautiful = Word("beautiful", [
.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
Add the following line to your Podfile:
pod 'SwiftLocalize'
and run pod update
from the podfile directory first.
Create a Package.swift
file.
// 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"])
]
)
$ swift build
Author
Voidilov, [email protected]
License
SwiftLocalize is available under the MIT license. See the LICENSE file for more info.