profile
viewpoint
If you are wondering where the data of this site comes from, please visit https://api.github.com/users/SwiftGen/events. GitMemory does not store any data, but only uses NGINX to cache data for a period of time. The idea behind GitMemory is simply to give users a better reading experience.

SwiftGen/SwiftGen 7254

The Swift code generator for your assets, storyboards, Localizable.strings, … — Get rid of all String-based APIs!

SwiftGen/StencilSwiftKit 184

A framework bringing additional nodes & filters to Stencil dedicated to Swift code generation

SwiftGen/templates 11

Stencil templates for SwiftGen

SwiftGen/SwiftGenKit 9

The framework behind SwiftGen, responsible for parsing various resources and turn them into Stencil contexts

SwiftGen/Eve 1

A repository for shared resources among SwiftGen repos

SwiftGen/homebrew-core 0

🍻 Default formulae for the missing package manager for macOS

SwiftGen/swift-docker 0

Docker image for deploying swift applications

SwiftGen/swiftgen.github.io 0

The website for SwiftGen's home page and documentation

startedSwiftGen/SwiftGen

started time in 4 hours

startedSwiftGen/SwiftGen

started time in 5 hours

startedSwiftGen/StencilSwiftKit

started time in 8 hours

startedSwiftGen/SwiftGen

started time in 12 hours

startedSwiftGen/SwiftGen

started time in a day

startedSwiftGen/StencilSwiftKit

started time in 2 days

startedSwiftGen/SwiftGen

started time in 2 days

startedSwiftGen/SwiftGen

started time in 2 days

startedSwiftGen/SwiftGen

started time in 3 days

startedSwiftGen/SwiftGen

started time in 3 days

startedSwiftGen/SwiftGen

started time in 4 days

startedSwiftGen/SwiftGen

started time in 4 days

startedSwiftGen/SwiftGen

started time in 5 days

startedSwiftGen/SwiftGen

started time in 5 days

fork oncezw/SwiftGen

The Swift code generator for your assets, storyboards, Localizable.strings, … — Get rid of all String-based APIs!

fork in 5 days

startedSwiftGen/SwiftGen

started time in 5 days

startedSwiftGen/SwiftGen

started time in 6 days

startedSwiftGen/SwiftGen

started time in 6 days

startedSwiftGen/SwiftGen

started time in 7 days

startedSwiftGen/SwiftGen

started time in 7 days

pull request commentSwiftGen/SwiftGen

Strings: Parse comments from strings file

Hey @djbe and @AliSoftware,

I made some updates to this branch a little while and was hoping you could take a look again.

I changed the parsing to handle keys and values that look like they contain comments. I think it's better to avoid rebuilding all the string parsing and still rely on PlistDecoder to parse out the keys and values since there's probably way more edge cases we may not be aware of. Parsing out the comments feels like more of a "nice to have" thing, so I figured it shouldn't mess with pairings the strings and just try to add comments to this if it can find them. Was case scenario, it would just teat a string like it didn't have a comment.

CraigSiemens

comment created time in 7 days

issue commentSwiftGen/SwiftGen

SwiftUI use of LocalizedStringKey

Thanks for the solution shared by @Jeehut . I created my ownself version base on that, and it works well so far:

// Generated using SwiftGen — https://github.com/SwiftGen/SwiftGen

{% if tables.count > 0 %}
import SwiftUI

// MARK: - Strings

{% macro parametersBlock types %}{% filter removeNewlines:"leading" %}
  {% for type in types %}
    {% if type == "String" %}
    _ p{{forloop.counter}}: Any
    {% else %}
    _ p{{forloop.counter}}: {{type}}
    {% endif %}
    {{ ", " if not forloop.last }}
  {% endfor %}
{% endfilter %}{% endmacro %}
{% macro argumentsBlock types %}{% filter removeNewlines:"leading" %}
  {% for type in types %}
    {% if type == "String" %}
    String(describing: p{{forloop.counter}})
    {% elif type == "UnsafeRawPointer" %}
    Int(bitPattern: p{{forloop.counter}})
    {% else %}
    p{{forloop.counter}}
    {% endif %}
    {{ ", " if not forloop.last }}
  {% endfor %}
{% endfilter %}{% endmacro %}
{% macro recursiveBlock table item %}
  {% for string in item.strings %}
  {% if not param.noComments %}
  /// {{string.translation}}
  {% endif %}
  {% if string.types %}
  public static func {{string.name|swiftIdentifier:"pretty"|lowerFirstWord|escapeReservedKeywords}}({% call parametersBlock string.types %}) -> String {
    return tr("{{table}}", "{{string.key}}", {% call argumentsBlock string.types %})
  }
  {% else %}
  public static let {{string.name|swiftIdentifier:"pretty"|lowerFirstWord|escapeReservedKeywords}} = LocalizedString(table: "{{table}}", lookupKey: "{{string.key}}")
  {% endif %}
  {% endfor %}
  {% for child in item.children %}
  public enum {{child.name|swiftIdentifier:"pretty"|escapeReservedKeywords}} {
    {% filter indent:2 %}{% call recursiveBlock table child %}{% endfilter %}
  }
  {% endfor %}
{% endmacro %}
{% set enumName %}{{param.enumName|default:"L10n"}}{% endset %}
public enum {{enumName}} {
  {% if tables.count > 1 or param.forceFileNameEnum %}
  {% for table in tables %}
  public enum {{table.name|swiftIdentifier:"pretty"|escapeReservedKeywords}} {
    {% filter indent:2 %}{% call recursiveBlock table.name table.levels %}{% endfilter %}
  }
  {% endfor %}
  {% else %}
  {% call recursiveBlock tables.first.name tables.first.levels %}
  {% endif %}
}

// MARK: - Implementation Details
{% if not param.withoutSupporter %}
fileprivate func tr(_ table: String, _ key: String, _ locale: Locale = Locale.current, _ args: CVarArg...) -> String {
  let path = Bundle.main.path(forResource: locale.identifier, ofType: "lproj") ?? ""
  let format: String
  if let bundle = Bundle(path: path) {
    format = NSLocalizedString(key, tableName: table, bundle: bundle, comment: "")
  } else {
    format = BundleToken.bundle.localizedString(forKey: key, value: nil, table: table)
  }
  return String(format: format, locale: locale, arguments: args)
}

public struct LocalizedString: Hashable {
  let table: String
  fileprivate let lookupKey: String

  init(table: String, lookupKey: String) {
    self.table = table
    self.lookupKey = lookupKey
  }

  var key: LocalizedStringKey {
    LocalizedStringKey(lookupKey)
  }

  var text: String {
    tr(table, lookupKey)
  }

  func text(withLocale locale: Locale) -> String {
    tr(table, lookupKey, locale)
  }
}
{% endif %}
{% if not param.bundle %}

private final class BundleToken {
  static let bundle: Bundle = {
    #if SWIFT_PACKAGE
      return Bundle.module
    #else
      return Bundle(for: BundleToken.self)
    #endif
  }()
}
{% endif %}
{% else %}
// No string found
{% endif %}

swiftgen.yml

input_dir: ${TARGET_NAME}/Resources/
output_dir: ${TARGET_NAME}/Generated/
strings:
    - inputs: en.lproj/Localizable.strings
      outputs:
        - templatePath: swiftui-strings-template.stencil
          output: strings.swift
    - inputs: en.lproj/
      filter: ^((?!InfoPlist|Localizable).)+\.strings$
      outputs:
        - templatePath: swiftui-strings-template.stencil
          output: stringsExtra.swift
          params:
            enumName: L10nExtra
            withoutSupporter:
xcassets:
  inputs: .
  filter: .+\.xcassets$
  outputs:
    - templatePath: swiftui-assets-template.stencil
      output: assets.swift

Here is how you can use it:

// Any string defined at Localizable.strings will be compiled into L10n
L10n.textName      // return type as LocalizedString which conform Hashable
L10n.textName.key  // return type as LocalizedStringKey
L10n.textName.text // return type as String
L10n.textName.text(withLocale: locale) // return type as String with particular locale's version

// And the others defined at .strings file not named as Localizable.strings will be compiled into L10nExtra
L10nExtra.fileName.textName
L10nExtra.fileName.textName.key
L10nExtra.fileName.textName.text
L10nExtra.fileName.textName.text(withLocale: locale)

The repo of config files has been create at https://github.com/wavky/SwiftGenConfigForSwiftUI but only described in Chinese. Anyone who can translate it into English would be helpful :)

krummler

comment created time in 7 days

startedSwiftGen/SwiftGen

started time in 7 days

startedSwiftGen/templates

started time in 8 days

startedSwiftGen/SwiftGen

started time in 8 days

startedSwiftGen/SwiftGen

started time in 8 days

fork Tomas-Shao/SwiftGen

The Swift code generator for your assets, storyboards, Localizable.strings, … — Get rid of all String-based APIs!

fork in 8 days

startedSwiftGen/SwiftGen

started time in 8 days

startedSwiftGen/StencilSwiftKit

started time in 9 days