TestsTested | ✓ |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | Feb 2017 |
SwiftSwift Version | 3.0 |
SPMSupports SPM | ✗ |
Maintained by Rivukis.
Parlance is a localization and pluralization framework for iOS apps written in Swift.
shared
instanceLocale
Int
and Language
Plural Rules Plural Rules Per Language (Note: use the Cardinal type)
// Supported Languages
enum Language {
case english
case irish
}
final class ParlanceCoordinator: ParlanceCoordinatable {
static let shared = ParlanceCoordinator()
// Language to use for the given Locale
func currentLanguage(for locale: Locale) -> Language {
switch locale.languageCode {
case "ga"?: return .irish
default: return .english
}
}
// Plural Category for the given Int and Language
func category(for int: Int, language: Language) -> PluralCategory {
switch language {
case .english:
switch int {
case 1: return .one
default: return .other
}
case .irish:
switch int {
case 1: return .one
case 2: return .two
case _ where int >= 3 && int <= 6: return .few
case _ where int >= 7 && int <= 10: return .many
default: return .other
}
}
}
}
BaseModule
t()
to the corressponding language specific SpecificParlance
’s static function, t()
enum LogInParlanceKey {
case signedInWelcomeMessage(name: String)
case guestWelcomeMessage
}
class LogInParlance: BaseParlance {
typealias _ParlanceCoordinator = ParlanceCoordinator
func t(_ key: LogInParlanceKey) -> String {
switch currentLanguage {
case .english: return LogInParlance_en.t(key)
case .irish: return LogInParlance_ga.t(key)
}
}
}
enum ShoppingCartParlanceKey {
case numberOfItems(Int)
}
class ShoppingCartParlance: BaseParlance {
typealias _ParlanceCoordinator = ParlanceCoordinator
func t(_ key: ShoppingCartParlanceKey) -> String {
switch currentLanguage {
case .english: return en.t(key)
case .irish: return ga.t(key)
}
}
}
SpecificModule
category(for:)
to get the plural category for any Int
senum EnglishPluralCategory: String {
case one
case other
}
class LoginParlance_EN: SpecificModule {
typealias _ParlanceCoordinator = ParlanceCoordinator
typealias PluralCategory = EnglishPluralCategory
static func t(_ key: LogInParlanceKey) -> String {
switch key {
case .signedInWelcomeMessage(name: let name): return "Welcome, " + name
case .guestWelcomeMessage: return "Welcome"
}
}
}
class ShoppingCartParlance_EN: SpecificModule {
typealias _ParlanceCoordinator = ParlanceCoordinator
typealias PluralCategory = EnglishPluralCategory
static func t(_ key: ShoppingCartParlanceKey) -> String {
switch key {
case .numberOfItems(let count):
switch category(for: count) {
case .one: return "1 item"
case .other: return "\(count) items"
}
}
}
}
To run the example project, clone the repo, and run pod install
from the Example directory first.
Parlance is available through CocoaPods. To install it, simply add the following line to your Podfile:
platform :ios, "10.0"
use_frameworks!
target "<YOUR_TARGET>" do
pod "Parlance"
end
Brian Radebaugh, [email protected]
Parlance is available under the MIT license. See the LICENSE file for more info.