Skip to content

Commit

Permalink
Add localized resources
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsaidi committed Feb 15, 2021
1 parent 930e9df commit 104e7ce
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions Package.swift
Expand Up @@ -4,6 +4,7 @@ import PackageDescription

let package = Package(
name: "KeyboardKit",
defaultLocalization: "en",
platforms: [
.iOS(.v13)
],
Expand Down
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Expand Up @@ -44,6 +44,7 @@ Besides the new things listed below, there are a bunch of new extensions, images
* `Locale` is a new namespace with a few new locale-specific utils.
* `LocaleDictionary` is a simple dictionary wrapper to work with localized resources.
* `LocaleKey` is a simple enum to gather top-level locale identifiers.
* `KKL10n` is a new enum that provides localized strings from KeyboardKit.
* `SystemKeyboard` now highlights buttons when typing on iPad.
* `View` has new `keyboardInputViewController` convenience extensions.
* `View+keyboardToast` has new context-based function.
Expand Down
50 changes: 50 additions & 0 deletions Sources/KeyboardKit/Localization/KKL10n.swift
@@ -0,0 +1,50 @@
import SwiftUI

/**
This enum maps towards keys in `Localizable.strings` files.
*/
public enum KKL10n: String, CaseIterable, Identifiable {

case
languageName,
locale,
`return`,
space

public var id: String { rawValue }
var key: String { rawValue }

var hasText: Bool { text != key }

var text: String {
let text = NSLocalizedString(key, bundle: .module, comment: "")
let isMissing = text == key
return isMissing ? "-" : text
}
}

struct L10n_Previews: PreviewProvider {

static var missing: [KKL10n] {
KKL10n.allCases.filter { !$0.hasText }
}

static var previews: some View {
NavigationView {
List {
if missing.count == 0 {
Text("All done!")
} else {
Text("Missing translations:")
}
ForEach(KKL10n.allCases.filter { !$0.hasText }) { item in
VStack(alignment: .leading) {
Text("\(item.key)").font(.footnote)
Text("\(item.text)")
}.padding(.vertical, 4)
}
}
}
.environment(\.locale, Locale(identifier: "sv"))
}
}
13 changes: 13 additions & 0 deletions Sources/KeyboardKit/Resources/en.lproj/Localizable.strings
@@ -0,0 +1,13 @@
/*
Localizable.strings
KeyboardKit

Created by Daniel Saidi on 2021-02-15.
Copyright © 2021 Daniel Saidi. All rights reserved.
*/

"locale" = "en";
"languageName" = "English";

"return" = "return";
"space" = "space";
13 changes: 13 additions & 0 deletions Sources/KeyboardKit/Resources/sv.lproj/Localizable.strings
@@ -0,0 +1,13 @@
/*
Localizable.strings
KeyboardKit

Created by Daniel Saidi on 2021-02-15.
Copyright © 2021 Daniel Saidi. All rights reserved.
*/

"locale" = "sv";
"languageName" = "Svenska";

"return" = "retur";
"space" = "mellanslag";

0 comments on commit 104e7ce

Please sign in to comment.