102 lines
4.1 KiB
Plaintext
102 lines
4.1 KiB
Plaintext
// swiftlint:disable all
|
|
// Generated using SwiftGen — https://github.com/SwiftGen/SwiftGen
|
|
|
|
{% if tables.count > 0 %}
|
|
{% set accessModifier %}{% if param.publicAccess %}public{% else %}internal{% endif %}{% endset %}
|
|
import Foundation
|
|
|
|
// swiftlint:disable superfluous_disable_command file_length implicit_return
|
|
|
|
// 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 %}
|
|
{% for line in string.translation|split:"\n" %}
|
|
/// {{line}}
|
|
{% endfor %}
|
|
{% endif %}
|
|
{% if string.types %}
|
|
{{accessModifier}} static func {{string.name|swiftIdentifier:"pretty"|lowerFirstWord|escapeReservedKeywords}}({% call parametersBlock string.types %}) -> String {
|
|
return {{enumName}}.tr("{{table}}", "{{string.key}}", {% call argumentsBlock string.types %})
|
|
}
|
|
{% elif param.lookupFunction %}
|
|
{# custom localization function is mostly used for in-app lang selection, so we want the loc to be recomputed at each call for those (hence the computed var) #}
|
|
{{accessModifier}} static var {{string.name|swiftIdentifier:"pretty"|lowerFirstWord|escapeReservedKeywords}}: String { return {{enumName}}.tr("{{table}}", "{{string.key}}") }
|
|
{% else %}
|
|
{{accessModifier}} static var {{string.name|swiftIdentifier:"pretty"|lowerFirstWord|escapeReservedKeywords}} = {{enumName}}.tr("{{table}}", "{{string.key}}")
|
|
{% endif %}
|
|
{% endfor %}
|
|
{% for child in item.children %}
|
|
|
|
{{accessModifier}} enum {{child.name|swiftIdentifier:"pretty"|escapeReservedKeywords}} {
|
|
{% filter indent:2 %}{% call recursiveBlock table child %}{% endfilter %}
|
|
}
|
|
{% endfor %}
|
|
{% endmacro %}
|
|
// swiftlint:disable explicit_type_interface function_parameter_count identifier_name line_length
|
|
// swiftlint:disable nesting type_body_length type_name vertical_whitespace_opening_braces
|
|
{% set enumName %}{{param.enumName|default:"L10n"}}{% endset %}
|
|
{{accessModifier}} enum {{enumName}} {
|
|
{% if tables.count > 1 or param.forceFileNameEnum %}
|
|
{% for table in tables %}
|
|
{% filter indent:2 %}{% call recursiveBlock table.name table.levels %}{% endfilter %}
|
|
{% endfor %}
|
|
{% else %}
|
|
{% call recursiveBlock tables.first.name tables.first.levels %}
|
|
{% endif %}
|
|
}
|
|
// swiftlint:enable explicit_type_interface function_parameter_count identifier_name line_length
|
|
// swiftlint:enable nesting type_body_length type_name vertical_whitespace_opening_braces
|
|
|
|
// MARK: - Implementation Details
|
|
|
|
extension {{enumName}} {
|
|
static func tr(_ table: String, _ key: String, _ args: CVarArg...) -> String {
|
|
// Use preferredLocalizations to get a language that is in the bundle and the user's preferred list of languages.
|
|
let languages = Bundle.overrideLocalizations ?? Bundle.app.preferredLocalizations
|
|
|
|
for language in languages {
|
|
if let translation = trIn(language, table, key, args) {
|
|
return translation
|
|
}
|
|
}
|
|
return Bundle.app.developmentLocalization.flatMap { trIn($0, table, key, args) } ?? key
|
|
}
|
|
|
|
private static func trIn(_ language: String, _ table: String, _ key: String, _ args: CVarArg...) -> String? {
|
|
guard let bundle = Bundle.lprojBundle(for: language) else { return nil }
|
|
let format = NSLocalizedString(key, tableName: table, bundle: bundle, comment: "")
|
|
let translation = String(format: format, locale: Locale(identifier: language), arguments: args)
|
|
guard translation != key else { return nil }
|
|
return translation
|
|
}
|
|
}
|
|
|
|
{% else %}
|
|
// No string found
|
|
{% endif %}
|
|
// swiftlint:enable all
|