Skip to content

Instantly share code, notes, and snippets.

@abesmon
Forked from youmee/PluralForm.swift
Created January 16, 2020 14:20
Show Gist options
  • Save abesmon/596e68506ccb18c88288091cc8f8dd97 to your computer and use it in GitHub Desktop.
Save abesmon/596e68506ccb18c88288091cc8f8dd97 to your computer and use it in GitHub Desktop.
Swift Russian Plural Form Function
//
// Orig created by youmee https://gist.github.com/youmee/bc23dd6088e59609609f
// Modified by Лысенко Алексей Димитриевич on 09/09/2019.
// Copyright © 2019 SMG All rights reserved.
//
import Foundation
@objc class Pluralizer: NSObject {
@objc class PluralForms: NSObject {
let one: String
let two: String
let many: String
@objc init(_ one: String, _ two: String, _ many: String) {
self.one = one
self.two = two
self.many = many
}
}
/*
pluralForm(28, forms: .init("год", "года", "лет"))
output: "лет"
*/
@objc static func pluralForm(number: Int, forms: PluralForms) -> String {
return number % 10 == 1 && number % 100 != 11 ? forms.one :
(number % 10 >= 2 && number % 10 <= 4 && (number % 100 < 10 || number % 100 >= 20) ? forms.two : forms.many)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment