Created
February 11, 2024 14:28
-
-
Save DoubleREW/05c262028406e574c11e2db86e376bba to your computer and use it in GitHub Desktop.
Add customizable and localized phrases to a Swift Packages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Localization.swift | |
// | |
// | |
// Created by Fausto Ristagno on 11/02/24. | |
// | |
import SwiftUI | |
public struct PhraseKey : Hashable, RawRepresentable, ExpressibleByStringLiteral { | |
public let rawValue: String | |
public init(rawValue: String) { | |
self.rawValue = rawValue | |
} | |
public init(stringLiteral value: StringLiteralType) { | |
self.init(rawValue: value) | |
} | |
} | |
public struct Phrase { | |
public let key: PhraseKey | |
public let text: String | |
public init(key: PhraseKey, text: some StringProtocol) { | |
self.key = key | |
self.text = "\(text)" | |
} | |
} | |
struct PhrasesDictionary { | |
let defaultPhrases: [PhraseKey: Phrase] | |
var phrases: [PhraseKey: Phrase] = [:] | |
init(defaultPhrases phrases: [Phrase]) { | |
var defaultPhrasesDict = [PhraseKey: Phrase]() | |
for phrase in phrases { | |
assert(defaultPhrasesDict[phrase.key] == nil, "Phrase with key '\(phrase.key.rawValue)' already has a default text") | |
defaultPhrasesDict[phrase.key] = phrase | |
} | |
self.defaultPhrases = defaultPhrasesDict | |
} | |
func phrase(for key: PhraseKey) -> String { | |
if let phrase = phrases[key] { | |
return "\(phrase.text)" | |
} | |
assert(defaultPhrases[key] != nil, "Phrase for key '\(key.rawValue)' missing") | |
return "\(defaultPhrases[key]!.text)" | |
} | |
mutating func add(phrase: Phrase) { | |
self.phrases[phrase.key] = phrase | |
} | |
mutating func add(phrases: [Phrase]) { | |
for phrase in phrases { | |
add(phrase: phrase) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyClass { | |
var phrases = PhrasesDictionary(defaultPhrases: [ | |
.init(key: .cancelBtn, text: "Cancel"), | |
.init(key: .interstitialAlertTitle, text: "Upgrade plan"), | |
.init(key: .interstitialAlertWatchAd, text: "Watch ad"), | |
.init(key: .interstitialAlertDiscoverPro, text: "Discover PRO upgrade"), | |
]) | |
init(phrases: [PhraseKey: String] = [:]) { | |
for (phraseKey, text) in phrases { | |
self.phrases.add(phrase: .init(key: phraseKey, text: text)) | |
} | |
} | |
} | |
extension PhraseKey { | |
public static let interstitialAlertTitle: Self = "interstitialAlertTitle" | |
public static let interstitialAlertWatchAd: Self = "interstitialAlertWatchAd" | |
public static let interstitialAlertDiscoverPro: Self = "interstitialAlertDiscoverPro" | |
public static let cancelBtn: Self = "cancelBtn" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment