-
Notifications
You must be signed in to change notification settings - Fork 0
/
localization-swift5.stencil
140 lines (126 loc) · 5.11 KB
/
localization-swift5.stencil
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// 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 prefer_self_in_static_references
// MARK: - Strings
{% macro parametersBlock types %}
{%- for type in types -%}
{%- if type == "String" -%}
_ p{{forloop.counter}}: Any
{%- else -%}
_ p{{forloop.counter}}: {{type}}
{%- endif -%}
{{ ", " if not forloop.last }}
{%- endfor -%}
{% endmacro %}
{% macro argumentsBlock types %}
{%- 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 -%}
{% endmacro %}
{% macro recursiveBlock table item %}
{% for string in item.strings %}
{% if not param.noComments %}
{% for line in string.comment|default:string.translation|split:"\n" %}
/// {{line}}
{% endfor %}
{% endif %}
{% set translation string.translation|replace:'"','\"'|replace:' ','\t' %}
{% 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 %}, fallback: "{{translation}}")
}
{% elif param.lookupFunction %}
{{accessModifier}} static var {{string.name|swiftIdentifier:"pretty"|lowerFirstWord|escapeReservedKeywords}}: String { return {{enumName}}.tr("{{table}}", "{{string.key}}", fallback: "{{translation}}") }
{% else %}
{{accessModifier}} static var {{string.name|swiftIdentifier:"pretty"|lowerFirstWord|escapeReservedKeywords}}: String { {{enumName}}.tr("{{table}}", "{{string.key}}", fallback: "{{translation}}") }
{% endif %}
{% endfor %}
{% for child in item.children %}
{{accessModifier}} enum {{child.name|swiftIdentifier:"pretty"|escapeReservedKeywords}} {
{% filter indent:2," ",true %}{% 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}} {
private(set) public static var languageProvider = LanguageProvider()
{% if tables.count > 1 or param.forceFileNameEnum %}
{% for table in tables %}
{{accessModifier}} enum {{table.name|swiftIdentifier:"pretty"|escapeReservedKeywords}} {
{% filter indent:2," ",true %}{% 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
public enum Languages: String, CaseIterable {
case {{param.languages}}
}
public class LanguageProvider: ObservableObject {
let constant = "L10nLanguageProvider-IDAP-language"
let defaults = UserDefaults.standard
public var selectedLanguage: Languages? {
didSet {
defaults.set(selectedLanguage?.rawValue ?? "", forKey: constant)
defaults.synchronize()
}
}
public init() {
var currentLanguage = Languages(rawValue: defaults.string(forKey: constant) ?? "")
if currentLanguage == nil {
for language in Locale.preferredLanguages {
let languageID = String(language.prefix(2))
if let findedLanguage = Languages(rawValue: languageID) {
currentLanguage = findedLanguage
break
}
}
}
self.selectedLanguage = currentLanguage ?? Languages.allCases.first
}
}
extension {{enumName}} {
private static func tr(_ table: String, _ key: String, _ args: CVarArg..., fallback value: String) -> String {
guard let selectedLanguage = self.languageProvider.selectedLanguage?.rawValue else {
return key
}
guard let path = BundleToken.bundle.path(forResource: selectedLanguage, ofType: "lproj"),
let bundle = Bundle(path: path) else {
return key
}
let format = NSLocalizedString(key, tableName: table, bundle: bundle, value: "", comment: "")
return String(format: format, locale: Locale.current, arguments: args)
}
}
{% 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 string found
{% endif %}