|
| 1 | +// The MIT License (MIT) |
| 2 | +// Copyright © 2020 Ivan Varabei ( [email protected]) |
| 3 | +// |
| 4 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +// of this software and associated documentation files (the "Software"), to deal |
| 6 | +// in the Software without restriction, including without limitation the rights |
| 7 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | +// copies of the Software, and to permit persons to whom the Software is |
| 9 | +// furnished to do so, subject to the following conditions: |
| 10 | +// |
| 11 | +// The above copyright notice and this permission notice shall be included in all |
| 12 | +// copies or substantial portions of the Software. |
| 13 | +// |
| 14 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | +// SOFTWARE. |
| 21 | + |
| 22 | +import Foundation |
| 23 | + |
| 24 | +public enum SPLocale: String { |
| 25 | + |
| 26 | + case ru = "ru" // Russian |
| 27 | + case en = "en" // English |
| 28 | + case uk = "uk" // Ukrainian |
| 29 | + case es = "es" // Spanish |
| 30 | + case ar = "ar" // Arabic |
| 31 | + case fa = "fa" // Persian |
| 32 | + case de = "de" // German |
| 33 | + case fr = "fr" // French |
| 34 | + case it = "it" // Italian |
| 35 | + case nl = "nl" // Dutch |
| 36 | + case id = "id" // Indonesian |
| 37 | + case ms = "ms" // Malay |
| 38 | + case tr = "tr" // Turkish |
| 39 | + case hy = "hy" // Armenian |
| 40 | + case zh = "zh" // Chinese |
| 41 | + case ja = "ja" // Japanese |
| 42 | + case ur = "ur" // Urdu |
| 43 | + case be = "be" // Belarusian |
| 44 | + case pt = "pt" // Portuguese |
| 45 | + case pl = "pl" // Polish |
| 46 | + case gsw = "gsw" // Swiss German |
| 47 | + case fil = "fil" // Filipino |
| 48 | + |
| 49 | + /** |
| 50 | + SparrowKit: Uniq identifier. |
| 51 | + */ |
| 52 | + public var identifier: String { rawValue } |
| 53 | + |
| 54 | + /** |
| 55 | + SparrowKit: Code if language which using Apple without split. |
| 56 | + */ |
| 57 | + public var languageCode: String { rawValue } |
| 58 | + |
| 59 | + /** |
| 60 | + SparrowKit: Current locale, which using in app |
| 61 | + */ |
| 62 | + public static var current: SPLocale { |
| 63 | + get { |
| 64 | + var code = Locale.preferredLanguages.first ?? "en" |
| 65 | + var locale = SPLocale(rawValue: code) |
| 66 | + if locale == nil { |
| 67 | + code = String(code.split(separator: "-").first ?? "en") |
| 68 | + locale = SPLocale(rawValue: code) |
| 69 | + } |
| 70 | + return locale ?? .en |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + SparrowKit: Localize description of language. |
| 76 | + */ |
| 77 | + public func description(in locale: SPLocale) -> String { |
| 78 | + let locale = NSLocale(localeIdentifier: locale.languageCode) |
| 79 | + let text = locale.displayName(forKey: NSLocale.Key.identifier, value: languageCode) ?? .empty |
| 80 | + return text.localizedCapitalized |
| 81 | + } |
| 82 | +} |
0 commit comments