Fixes #4881 - Using the bundle display name as the app name in localizable .strings files. Exposing generated .strings and info.plist members to Objective-C.

This commit is contained in:
Stefan Ceriu
2021-09-22 14:10:16 +03:00
parent b7b2fff7e3
commit 4dd70594cc
32 changed files with 1696 additions and 1663 deletions

View File

@@ -0,0 +1,118 @@
// swiftlint:disable all
// Generated using SwiftGen — https://github.com/SwiftGen/SwiftGen
{% if files %}
{% set accessModifier %}{% if param.publicAccess %}public{% else %}internal{% endif %}{% endset %}
import Foundation
// swiftlint:disable superfluous_disable_command
// swiftlint:disable file_length
// MARK: - Plist Files
{% macro fileBlock file %}
{% call documentBlock file file.document %}
{% endmacro %}
{% macro documentBlock file document %}
{% set rootType %}{% call typeBlock document.metadata %}{% endset %}
{% if document.metadata.type == "Array" %}
{{accessModifier}} static let items: {{rootType}} = arrayFromPlist(at: "{% call transformPath file.path %}")
{% elif document.metadata.type == "Dictionary" %}
private static let _document = PlistDocument(path: "{% call transformPath file.path %}")
{% for key,value in document.metadata.properties %}
{{accessModifier}} {% call propertyBlock key value %}
{% endfor %}
{% else %}
// Unsupported root type `{{rootType}}`
{% endif %}
{% endmacro %}
{% macro typeBlock metadata %}{% filter removeNewlines:"leading" %}
{% if metadata.type == "Array" %}
[{% call typeBlock metadata.element %}]
{% elif metadata.type == "Dictionary" %}
[String: Any]
{% else %}
{{metadata.type}}
{% endif %}
{% endfilter %}{% endmacro %}
{% macro propertyBlock key metadata %}{% filter removeNewlines:"leading" %}
{% set propertyName %}{{key|swiftIdentifier:"pretty"|lowerFirstWord|escapeReservedKeywords}}{% endset %}
{% set propertyType %}{% call typeBlock metadata %}{% endset %}
static let {{propertyName}}: {{propertyType}} = _document["{{key}}"]
{% endfilter %}{% endmacro %}
{% macro transformPath path %}{% filter removeNewlines %}
{% if param.preservePath %}
{{path}}
{% else %}
{{path|basename}}
{% endif %}
{% endfilter %}{% endmacro %}
// swiftlint:disable identifier_name line_length type_body_length
@objcMembers
{{accessModifier}} class {{param.className|default:"PlistFiles"}}: NSObject {
{% if files.count > 1 or param.forceFileNameEnum %}
{% for file in files %}
{{accessModifier}} class {{file.name|swiftIdentifier:"pretty"|escapeReservedKeywords}} {
{% filter indent:2 %}{% call fileBlock file %}{% endfilter %}
}
{% endfor %}
{% else %}
{% call fileBlock files.first %}
{% endif %}
}
// swiftlint:enable identifier_name line_length type_body_length
// MARK: - Implementation Details
private func arrayFromPlist<T>(at path: String) -> [T] {
{% if param.lookupFunction %}
guard let url = {{param.lookupFunction}}(path),
{% else %}
guard let url = {{param.bundle|default:"BundleToken.bundle"}}.url(forResource: path, withExtension: nil),
{% endif %}
let data = NSArray(contentsOf: url) as? [T] else {
fatalError("Unable to load PLIST at path: \(path)")
}
return data
}
private struct PlistDocument {
let data: [String: Any]
init(path: String) {
{% if param.lookupFunction %}
guard let url = {{param.lookupFunction}}(path),
{% else %}
guard let url = {{param.bundle|default:"BundleToken.bundle"}}.url(forResource: path, withExtension: nil),
{% endif %}
let data = NSDictionary(contentsOf: url) as? [String: Any] else {
fatalError("Unable to load PLIST at path: \(path)")
}
self.data = data
}
subscript<T>(key: String) -> T {
guard let result = data[key] as? T else {
fatalError("Property '\(key)' is not of type \(T.self)")
}
return result
}
}
{% if not param.bundle and not param.lookupFunction %}
// swiftlint:disable convenience_type
private final class BundleToken {
static let bundle: Bundle = {
#if SWIFT_PACKAGE
return Bundle.module
#else
return Bundle(for: BundleToken.self)
#endif
}()
}
// swiftlint:enable convenience_type
{% endif %}
{% else %}
// No files found
{% endif %}

View File

@@ -32,11 +32,11 @@ import Foundation
{% endif %}
{% if string.types %}
{{accessModifier}} static func {{string.key|swiftIdentifier:"pretty"|lowerFirstWord|escapeReservedKeywords}}({% call parametersBlock string.types %}) -> String {
return {{enumName}}.tr("{{table}}", "{{string.key}}", {% call argumentsBlock string.types %})
return {{className}}.tr("{{table}}", "{{string.key}}", {% call argumentsBlock string.types %})
}
{% else %}
{{accessModifier}} static var {{string.key|swiftIdentifier:"pretty"|lowerFirstWord|escapeReservedKeywords}}: String {
return {{enumName}}.tr("{{table}}", "{{string.key}}")
return {{className}}.tr("{{table}}", "{{string.key}}")
}
{% endif %}
{% endfor %}
@@ -45,11 +45,12 @@ import Foundation
{% endfor %}
{% endmacro %}
// swiftlint:disable function_parameter_count identifier_name line_length type_body_length
{% set enumName %}{{param.enumName|default:"L10n"}}{% endset %}
{{accessModifier}} enum {{enumName}} {
{% set className %}{{param.className|default:"L10n"}}{% endset %}
@objcMembers
{{accessModifier}} class {{className}}: NSObject {
{% if tables.count > 1 %}
{% for table in tables %}
{{accessModifier}} enum {{table.name|swiftIdentifier:"pretty"|escapeReservedKeywords}} {
{{accessModifier}} class {{table.name|swiftIdentifier:"pretty"|escapeReservedKeywords}} {
{% filter indent:2 %}{% call recursiveBlock table.name table.levels %}{% endfilter %}
}
{% endfor %}
@@ -61,7 +62,7 @@ import Foundation
// MARK: - Implementation Details
extension {{enumName}} {
extension {{className}} {
static func tr(_ table: String, _ key: String, _ args: CVarArg...) -> String {
let format = NSLocalizedString(key, tableName: table, bundle: Bundle(for: BundleToken.self), comment: "")
let locale: Locale

View File

@@ -1,39 +1,41 @@
input_dir: ../../Riot/
output_dir: ../../Riot/Generated/
ib:
- inputs:
inputs:
- ../Tools/Templates/buildable/
- Modules/
filter: ^((?!(DeactivateAccountViewController|Security)).)*\.(storyboard)
outputs:
- templateName: scenes-swift4
output: Storyboards.swift
filter: ^((?!(DeactivateAccountViewController|Security)).)*\.(storyboard)
outputs:
templateName: scenes-swift4
output: Storyboards.swift
strings:
inputs: Assets/en.lproj/Vector.strings
outputs:
- templatePath: Templates/Strings/flat-swift4-vector.stencil
output: Strings.swift
params:
enumName: VectorL10n
templatePath: Templates/Strings/flat-swift4-vector.stencil
output: Strings.swift
params:
className: VectorL10n
publicAccess: true
xcassets:
- inputs:
- Assets/Images.xcassets
- Assets/SharedImages.xcassets
outputs:
- templateName: swift4
output: Images.swift
inputs:
- Assets/Images.xcassets
- Assets/SharedImages.xcassets
outputs:
templateName: swift4
output: Images.swift
plist:
inputs: Assets/Riot-Defaults.plist
outputs:
templateName: runtime-swift4
output: RiotDefaults.swift
params:
enumName: RiotDefaults
enumName: RiotDefaults
plist:
inputs: SupportingFiles/Info.plist
outputs:
templateName: runtime-swift4
templatePath: Templates/Plist/runtime-swift5.stencil
output: InfoPlist.swift
params:
enumName: InfoPlist
className: InfoPlist
publicAccess: true