Created
September 29, 2022 02:44
-
-
Save AdamWhitcroft/deeb1b743e2c3018727352760ff41406 to your computer and use it in GitHub Desktop.
This file contains hidden or 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.strings | |
// | |
// This file holds my text strings and their keys, some examples below: | |
// | |
"entryBottle" = "Bottle"; | |
"entryLeftBoob" = "Left boob"; | |
"entryRightBoob" = "Right boob"; | |
// | |
// Localization.swift | |
// | |
// This file is used to generate an easy way to reference strings. | |
// This method is dope af because you get autocompletion when you type, making | |
// it real hard to mess up. | |
// | |
import SwiftUI | |
enum Strings { | |
// "Bottle" | |
static var entryBottle: String { | |
localization(LocalizationKeys.entryBottle.rawValue) | |
} | |
// "Left boob" | |
static var entryLeftBoob: String { | |
localization(LocalizationKeys.entryLeftBoob.rawValue) | |
} | |
// "Right boob" | |
static var entryRightBoob: String { | |
localization(LocalizationKeys.entryRightBoob.rawValue) | |
} | |
// etc... | |
enum LocalizationKeys: String, CaseIterable { | |
case entryBottle | |
case entryLeftBoob | |
case entryRightBoob | |
// etc... | |
} | |
private class BundleToken {} | |
static let StringBundle = Bundle(for: BundleToken.self) | |
static func localization(_ key: String, arguments: CVarArg...) -> String { | |
let localization = NSLocalizedString(key, bundle: StringBundle, comment: "") | |
return String(format: localization, arguments: arguments) | |
} | |
} | |
// | |
// Usage: Referencing a string this way is super simple: | |
// | |
// Text(Strings.entryBottle) | |
// |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment