Skip to content

Commit

Permalink
Added SPLocale.
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanvorobei committed Sep 3, 2021
1 parent 0a210ad commit 19d0fc2
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 6 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ import Foundation

public struct SPFileManagerDestination {

// MARK: - Data

public var file: String
public var directory: URL

public var url: URL {
return directory.appendingPathComponent(file)
}
public var url: URL { directory.appendingPathComponent(file) }

// MARK: - Init

public init(directory: FileManager.SearchPathDirectory = .documentDirectory, path: String, file: String) {
let fileManager = FileManager.default
Expand All @@ -46,10 +48,12 @@ public struct SPFileManagerDestination {

public init(fileName: String) {
let bundleFileURL = Bundle.main.url(forResource: fileName, withExtension: nil)
self.directory = bundleFileURL?.deletingLastPathComponent() ?? URL.init(string: "")!
self.file = bundleFileURL?.lastPathComponent ?? ""
self.directory = bundleFileURL?.deletingLastPathComponent() ?? URL.init(string: .empty)!
self.file = bundleFileURL?.lastPathComponent ?? .empty
}

// MARK: - Private

private static func cleaned(_ path: String) -> String {
return path.removedPrefix("/").removedSuffix("/")
}
Expand Down
82 changes: 82 additions & 0 deletions Sources/SparrowKit/Foundation/Classes/SPLocale.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// The MIT License (MIT)
// Copyright © 2020 Ivan Varabei ([email protected])
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

import Foundation

public enum SPLocale: String {

case ru = "ru" // Russian
case en = "en" // English
case uk = "uk" // Ukrainian
case es = "es" // Spanish
case ar = "ar" // Arabic
case fa = "fa" // Persian
case de = "de" // German
case fr = "fr" // French
case it = "it" // Italian
case nl = "nl" // Dutch
case id = "id" // Indonesian
case ms = "ms" // Malay
case tr = "tr" // Turkish
case hy = "hy" // Armenian
case zh = "zh" // Chinese
case ja = "ja" // Japanese
case ur = "ur" // Urdu
case be = "be" // Belarusian
case pt = "pt" // Portuguese
case pl = "pl" // Polish
case gsw = "gsw" // Swiss German
case fil = "fil" // Filipino

/**
SparrowKit: Uniq identifier.
*/
public var identifier: String { rawValue }

/**
SparrowKit: Code if language which using Apple without split.
*/
public var languageCode: String { rawValue }

/**
SparrowKit: Current locale, which using in app
*/
public static var current: SPLocale {
get {
var code = Locale.preferredLanguages.first ?? "en"
var locale = SPLocale(rawValue: code)
if locale == nil {
code = String(code.split(separator: "-").first ?? "en")
locale = SPLocale(rawValue: code)
}
return locale ?? .en
}
}

/**
SparrowKit: Localize description of language.
*/
public func description(in locale: SPLocale) -> String {
let locale = NSLocale(localeIdentifier: locale.languageCode)
let text = locale.displayName(forKey: NSLocale.Key.identifier, value: languageCode) ?? .empty
return text.localizedCapitalized
}
}
2 changes: 1 addition & 1 deletion SparrowKit.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

s.name = 'SparrowKit'
s.version = '3.2.5'
s.version = '3.2.6'
s.summary = 'Collection of native Swift extensions to boost your development. Support tvOS and watchOS.'
s.homepage = 'https://github.com/ivanvorobei/SparrowKit'
s.source = { :git => 'https://github.com/ivanvorobei/SparrowKit.git', :tag => s.version }
Expand Down

0 comments on commit 19d0fc2

Please sign in to comment.