Back to Swiftgen

Flat Swift4

Documentation/templates/strings/flat-swift4.md

6.6.33.4 KB
Original Source

Template Information

NameDescription
File namestrings/flat-swift4.stencil
Configuration example<pre>strings:
inputs: path/to/Localizable.strings
outputs:
templateName: flat-swift4
output: Strings.swift</pre> |

| Language | Swift 4 | | Author | Olivier Halligon |

When to use it

  • When you need to generate Swift 4 code.
  • If you use unstructured key names for your strings, or a structure that we don't support (yet). If you use "dot-syntax" keys, please check out the structured-swift4 template.

Customization

You can customize some elements of this template by overriding the following parameters when invoking swiftgen. See the dedicated documentation.

Parameter NameDefault ValueDescription
bundleBundleToken.bundleAllows 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.
enumNameL10nAllows you to change the name of the generated enum containing all string tables.
forceFileNameEnumN/ASetting this parameter will generate an enum <FileName> even if only one FileName was provided as input.
lookupFunctionN/A¹Allows you to set your own custom localization function. The function needs to have as signature: (key: String, table: String, fallbackValue: 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:fallbackValue:). Note: if you define this parameter, the bundle parameter will be ignored.
noCommentsN/ASetting this parameter will disable the comments containing the comment from the strings file or the translation of a key.
publicAccessN/AIf set, the generated constants will be marked as public. Otherwise, they'll be declared internal.
  1. If you don't provide a lookupFunction, we will use localizedString(forKey:value:table:) on the bundle parameter instead.

Generated Code

Extract:

swift
internal enum L10n {
  /// Some alert body there
  internal static let alertMessage = L10n.tr("Localizable", "alert__message", fallback: "Some alert body there")
  /// Title for an alert
  internal static let alertTitle = L10n.tr("Localizable", "alert__title", fallback: "Title of the alert")
  /// You have %d apples
  internal static func applesCount(_ p1: Int) -> String {
    return L10n.tr("Localizable", "apples.count", p1, fallback: "You have %d apples")
  }
  /// A comment with no space above it
  internal static func bananasOwner(_ p1: Int, _ p2: Any) -> String {
    return L10n.tr("Localizable", "bananas.owner", p1, String(describing: p2), fallback: "Those %d bananas belong to %@.")
  }
}

Full generated code

Usage example

swift
// 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.applesCount(3)
let bananas = L10n.bananasOwner(5, "Olivier")