Skip to content

Commit

Permalink
Add L10n (#39)
Browse files Browse the repository at this point in the history
Have all localisation components at one place.
  • Loading branch information
melle committed Nov 24, 2022
1 parent 9eacde8 commit def4b8d
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Sources/FoundationExtensions/L10n/.sourcery-l10n-example.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
sources:
include:
- Sources/FooBar/Extensions/L10n.swift
exclude:
- Sources/FooBar/Generated
templates:
- ../Tools/Sourcery/Templates/Localizable.stencil
output:
Sources/FooBar/Generated
args:
imports: ["Foundation"]
testable-imports: []
50 changes: 50 additions & 0 deletions Sources/FoundationExtensions/L10n/L10n.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright © 2022 Lautsprecher Teufel GmbH. All rights reserved.

import Foundation

/**
Type-safe localization in 3 easy steps ✨.
**Step 1**: Define your strings in an extension of L10n (i.e. in `App/Sources/L10n/L10n.swift`).
```
extension L10n {
public enum HomeScreen: Localizable {
// sourcery: comment=Navigation Bar Title on Home screen
case navigationBarTitle
// sourcery: comment=Some value with parameters
case value(/* sourcery: format=%d */ count: Int, another: String, /* sourcery: format=%.2f */ exacly: Float)
// sourcery: comment=Title for submit button on Home screen
case submitButton
}
}
```
**Step 2**: Use [sourcery](https://github.com/krzysztofzablocki/Sourcery) + genstrings + [MergeL10n](https://github.com/teufelaudio/MergeL10n) to generate `Localizable.strings`.
See .sourcery-l10n-example.yml and adjust the paths to your project. The sourcery template
can be found in `Tools/Sourcery/Templates/Sources/Localizable.stencil`.
```
# Makefile excerpt
App/Sources/Generated/Localizable.generated.swift: App/Sources/L10n/L10n.swift
sourcery --config App/Sources/.sourcery-l10n.yml
genstrings -o App/Sources/Resources/zz.lproj -s LS App/Sources/Generated/Localizable.generated.swift
Tools/MergeL10n pseudo-to-languages --base-paths="App/Sources/Resources"
```
**Step 3**: Refer to the strings with the `.localizedString` method.
```
public struct Example: View {
public var body: some View {
Text(L10n.HomeScreen.navigationBarTitle.localizedString)
}
}
```
*/
public enum L10n {
}
22 changes: 22 additions & 0 deletions Sources/FoundationExtensions/L10n/Localizable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright © 2022 Lautsprecher Teufel GmbH. All rights reserved.

import SwiftUI

@available(iOS 13.0, *)
public protocol Localizable {
var key: LocalizedStringKey { get }
var rootKey: String { get }
}

@available(iOS 13.0, *)
extension Localizable {
public var key: LocalizedStringKey {
.init(
"\(rootKey).\(Mirror(reflecting: self).children.first?.label ?? "\(self)")"
)
}

public var rootKey: String {
String(reflecting: Self.self)
}
}

0 comments on commit def4b8d

Please sign in to comment.