summacristian/localizerkit
**LocalizerKit** is a Swift Package that provides the foundation to localize your apps built in Swift Playground.
๐ Usage
First, import the package wherever you need it:
import LocalizerKit๐๏ธ Creating the keyset for your strings
This package relies on an enum to define the keys used for localized strings. This improves autocomplete, avoids typos, and makes the code easier to maintain.
Your keyset should conform to LocalizedKeyProtocol, and also be RawRepresentable with String and Hashable.
enum LocalizedKey: String, LocalizedKeyProtocol {
case welcome
case goodbye
// ...
}๐ง Why use an enum for keys?
LocalizerKit ensures your keys are compiler-checked. If you reference a missing key, your code wonโt compile โ no more runtime crashes due to typos. Plus, the IDE autocompletes your keys automatically, making localization fast, safe, and efficient.
<p align="center"> <picture> <source srcset="Assets/AutocompletePreview_Dark.jpeg" media="(prefers-color-scheme: dark)"> <source srcset="Assets/AutocompletePreview_Light.jpeg" media="(prefers-color-scheme: light)"> <img src="Assets/AutocompletePreview_Light.jpeg" width="600" alt="Autocomplete in Swift Playground"> </picture> </p>
๐ Defining localized strings
To define the actual strings, create one struct per supported language, conforming to LocalizedLanguage. Each struct must:
- Specify its
SupportedLanguage(e.g..en,.it) - Provide a
dictionarymapping keys to strings
struct EnglishStrings: LocalizedLanguage {
static let language: SupportedLanguage = .en
static let strings: [LocalizedKey: String] = [
.welcome: "Welcome",
.goodbye: "Goodbye"
]
}๐ก Tip: While LocalizerKit supports different keysets per language, itโs highly recommended to use a shared one for consistency and easier maintenance.
๐ Registering localizations
You must register each languageโs struct so that Localizer knows where to look.
You can do this automatically using a SwiftUI view modifier:
WindowGroup {
ContentView()
.localized(in: [EnglishStrings.self, ItalianStrings.self, GermanStrings.self])
}This modifier registers each language into LocalizerRegistry, using the language specified in each struct.
โ ๏ธ If multiple structs register for the same language, the last one will overwrite the previous entry.
๐งฉ Manual registration (advanced use)
You can skip the view modifier and register manually. For example, in your init():
init() {
let languages = [EnglishStrings.self, ItalianStrings.self, GermanStrings.self]
for language in languages {
LocalizerRegistry.register(language)
}
}๐ Language resolution and overrides
By default, LocalizerKit uses the first language from Locale.preferredLanguages that is also registered in LocalizerRegistry.
You can override this behavior with LocalizerSettings:
@StateObject private var settings = LocalizerSettings()
Toggle("Override system language", isOn: $settings.overrideSystemLanguages)
Picker("Language", selection: $settings.selectedLanguage) {
ForEach(Localizer.availableLanguages, id: \.self) { lang in
Text("\(lang.flag) \(lang.displayName)").tag(lang)
}
}The Localizer struct exposes two variables you can tinker with:
.overrideSystemLanguages: aBoolthat, when settrue, makesLocalizerignore the system'sLocale.preferredLanguages.selectedLanguage: aSupportedLanguagevalue that defines the app's language when override is enabled
When Localizer.overrideSystemLanguages is true, all localization lookups will use .selectedLanguage instead of the system locale. This makes it easy to detach your appโs language from the deviceโs settings, ideal for providing an in-app language picker for example.
๐ฅ๏ธ Using localized strings in views
Once set up, retrieving a localized string is simple:
Text(Localizer.get(LocalizedKey.welcome))๐ก Cleaner syntax with extension
To avoid writing LocalizedKey. every time, add this helper:
extension Localizer {
public static func get(
_ key: LocalizedKey,
in lang: SupportedLanguage = Localizer.currentLanguage
) -> String {
return get(key as any LocalizedKeyProtocol, in: lang)
}
}Then use:
Text(Localizer.get(.welcome))You can locally override the language in a specific Text:
Text(Localizer.get(.welcome, in: .fr))๐ Adding a new localization
To add support for a new language:
- Create a struct for the new language (e.g.
SpanishStrings), implementingLocalizedLanguage. - Add it to the
.localized(in: [...])modifier or manually register it.
Done! The app will now automatically use that language if it matches the userโs preferred locale.
๐ Supported Languages
LocalizerKit currently includes out-of-the-box translations for 30 languages, and growing. You can register just the ones you need for your app, no need to rush all of them at launch.
Supported languages include: | | | | |--------|--------|--------| | ๐บ๐ธ English | ๐ฎ๐น Italian | ๐ซ๐ท French | | ๐ฉ๐ช German | ๐ช๐ธ Spanish | ๐ต๐น Portuguese | | ๐ณ๐ฑ Dutch | ๐ธ๐ช Swedish | ๐ฉ๐ฐ Danish | | ๐ซ๐ฎ Finnish | ๐ต๐ฑ Polish | ๐จ๐ฟ Czech | | ๐ท๐ด Romanian | ๐ญ๐บ Hungarian | ๐ฌ๐ท Greek | | ๐น๐ท Turkish | ๐ท๐บ Russian | ๐บ๐ฆ Ukrainian | | ๐ฎ๐ฑ Hebrew | ๐ธ๐ฆ Arabic | ๐ฎ๐ณ Hindi | | ๐จ๐ณ Chinese | ๐ฏ๐ต Japanese | ๐ฐ๐ท Korean | | ๐ป๐ณ Vietnamese | ๐น๐ญ Thai | ๐ฎ๐ฉ Indonesian | | ๐ฒ๐พ Malay | ๐ณ๐ด Norwegian | ๐ฎ๐ท Persian |
๐ฆ Installation
To use LocalizerKit in your project:
- Open Swift Playground or Xcode.
- Add a Swift Package dependency with this URL:
https://github.com/SummaCristian/LocalizerKit.git- Import the module in your files:
import LocalizerKit๐ License
LocalizerKit is released under the MIT License. See the LICENSE file for more details.
Package Metadata
Repository: summacristian/localizerkit
Default branch: main
README: README.md