l21strings.stencil 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // swiftlint:disable all
  2. // Generated using SwiftGen — https://github.com/SwiftGen/SwiftGen
  3. {% if tables.count > 0 %}
  4. {% set accessModifier %}{% if param.publicAccess %}public{% else %}internal{% endif %}{% endset %}
  5. import Foundation
  6. // swiftlint:disable superfluous_disable_command file_length implicit_return prefer_self_in_static_references
  7. // MARK: - Strings
  8. {% macro parametersBlock types %}
  9. {%- for type in types -%}
  10. {%- if type == "String" -%}
  11. _ p{{forloop.counter}}: Any
  12. {%- else -%}
  13. _ p{{forloop.counter}}: {{type}}
  14. {%- endif -%}
  15. {{ ", " if not forloop.last }}
  16. {%- endfor -%}
  17. {% endmacro %}
  18. {% macro argumentsBlock types %}
  19. {%- for type in types -%}
  20. {%- if type == "String" -%}
  21. String(describing: p{{forloop.counter}})
  22. {%- elif type == "UnsafeRawPointer" -%}
  23. Int(bitPattern: p{{forloop.counter}})
  24. {%- else -%}
  25. p{{forloop.counter}}
  26. {%- endif -%}
  27. {{ ", " if not forloop.last }}
  28. {%- endfor -%}
  29. {% endmacro %}
  30. {% macro recursiveBlock table item %}
  31. {% for string in item.strings %}
  32. {% if not param.noComments %}
  33. {% for line in string.comment|default:string.translation|split:"\n" %}
  34. /// {{line}}
  35. {% endfor %}
  36. {% endif %}
  37. {% set translation string.translation|replace:'"','\"'|replace:' ','\t' %}
  38. {% if string.types %}
  39. {{accessModifier}} static func {{string.name|swiftIdentifier:"pretty"|lowerFirstWord|escapeReservedKeywords}}({% call parametersBlock string.types %}) -> String {
  40. return {{enumName}}.tr("{{string.key}}", {%+ call argumentsBlock string.types %}, fallback: "{{translation}}")
  41. }
  42. {% elif param.lookupFunction %}
  43. {{accessModifier}} static var {{string.name|swiftIdentifier:"pretty"|lowerFirstWord|escapeReservedKeywords}}: String { return {{enumName}}.tr("{{string.key}}", fallback: "{{translation}}") }
  44. {% else %}
  45. {{accessModifier}} static let {{string.name|swiftIdentifier:"pretty"|lowerFirstWord|escapeReservedKeywords}} = {{enumName}}.tr("{{string.key}}", fallback: "{{translation}}")
  46. {% endif %}
  47. {% endfor %}
  48. {% for child in item.children %}
  49. {{accessModifier}} enum {{child.name|swiftIdentifier:"pretty"|escapeReservedKeywords}} {
  50. {% filter indent:2," ",true %}{% call recursiveBlock table child %}{% endfilter %}
  51. }
  52. {% endfor %}
  53. {% endmacro %}
  54. // swiftlint:disable explicit_type_interface function_parameter_count identifier_name line_length
  55. // swiftlint:disable nesting type_body_length type_name vertical_whitespace_opening_braces
  56. {% set enumName %}{{param.enumName|default:"L10n"}}{% endset %}
  57. {{accessModifier}} enum {{enumName}} {
  58. {% if tables.count > 1 or param.forceFileNameEnum %}
  59. {% for table in tables %}
  60. {{accessModifier}} enum {{table.name|swiftIdentifier:"pretty"|escapeReservedKeywords}} {
  61. {% filter indent:2," ",true %}{% call recursiveBlock table.name table.levels %}{% endfilter %}
  62. }
  63. {% endfor %}
  64. {% else %}
  65. {% call recursiveBlock tables.first.name tables.first.levels %}
  66. {% endif %}
  67. }
  68. // swiftlint:enable explicit_type_interface function_parameter_count identifier_name line_length
  69. // swiftlint:enable nesting type_body_length type_name vertical_whitespace_opening_braces
  70. // MARK: - Implementation Details
  71. extension {{enumName}} {
  72. private static func tr(_ key: String, _ args: CVarArg..., fallback value: String) -> String {
  73. {% if param.lookupFunction %}
  74. let format = {{ param.lookupFunction }}(key, table, value)
  75. {% else %}
  76. let format = {{param.bundle|default:"BundleToken.bundle"}}.localizedString(forKey: key, value: value, table: nil)
  77. {% endif %}
  78. return String(format: format, locale: Locale.current, arguments: args)
  79. }
  80. }
  81. {% if not param.bundle and not param.lookupFunction %}
  82. // swiftlint:disable convenience_type
  83. private final class BundleToken {
  84. static let bundle: Bundle = {
  85. #if SWIFT_PACKAGE
  86. return Bundle.module
  87. #else
  88. return Bundle(for: BundleToken.self)
  89. #endif
  90. }()
  91. }
  92. // swiftlint:enable convenience_type
  93. {% endif %}
  94. {% else %}
  95. // No string found
  96. {% endif %}