Name | Description |
---|---|
File name | strings/structured-swift5.stencil |
Configuration example | strings: |
Language | Swift 5 |
Author | Olivier Halligon |
- When you need to generate Swift 5 code.
- If you use "structured" keys for your strings, that is components separated by the
.
character, for example:
"some.deep.structure"
"some.deep.something"
"hello.world"
You can customize some elements of this template by overriding the following parameters when invoking swiftgen
. See the dedicated documentation.
Parameter Name | Default Value | Description |
---|---|---|
bundle |
BundleToken.bundle |
Allows you to set from which bundle strings are loaded from. By default, it'll point to the same bundle as where the generated code is. Note: ignored if lookupFunction parameter is set. |
enumName |
L10n |
Allows you to change the name of the generated enum containing all string tables. |
forceFileNameEnum |
N/A | Setting this parameter will generate an enum <FileName> even if only one FileName was provided as input. |
lookupFunction |
N/A¹ | Allows you to set your own custom localization function. The function needs to have as signature: (key: String, table: String) -> String . The parameters of your function can have any name (or even no external name), but if it has named parameters, you must provide the complete function signature, including those named parameters – e.g. yourFunctionName(forKey:table:) . Note: if you define this parameter, the bundle parameter will be ignored. |
noComments |
N/A | Setting this parameter will disable the comments describing the translation of a key. |
publicAccess |
N/A | If set, the generated constants will be marked as public . Otherwise, they'll be declared internal . |
- If you don't provide a
lookupFunction
, we will uselocalizedString(forKey:value:table:)
on thebundle
parameter instead.
Extract:
internal enum L10n {
/// Some alert body there
internal static let alertMessage = L10n.tr("alert_message")
/// Title of the alert
internal static let alertTitle = L10n.tr("alert_title")
internal enum Apples {
/// You have %d apples
internal static func count(_ p1: Int) -> String {
return L10n.tr("apples.count", p1)
}
}
internal enum Bananas {
/// Those %d bananas belong to %@.
internal static func owner(_ p1: Int, _ p2: Any) -> String {
return L10n.tr("bananas.owner", p1, String(describing: p2))
}
}
}
// Simple strings
let message = L10n.alertMessage
let title = L10n.alertTitle
// with parameters, note that each argument needs to be of the correct type
let apples = L10n.Apples.count(3)
let bananas = L10n.Bananas.owner(5, "Olivier")