Skip to content

Commit 57d0054

Browse files
authored
Make localization splitting configurable with the --separator option (#38)
1 parent bbcc724 commit 57d0054

5 files changed

Lines changed: 43 additions & 3 deletions

File tree

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,21 @@ In case your Xcode project has multiple application targets, you should specify
177177
shark $PROJECT_FILE_PATH $PROJECT_DIR/$PROJECT_NAME --target MyAppTarget
178178
```
179179

180+
### --separator
181+
182+
Shark will split localization keys using the separator character value, and create nested enums until we hit the last element. For example, the lines `login.button.positive = "Log in!";` and `login.button.negative = "Go back...";` will create the following structure inside the top level localizations enum `L`:
183+
184+
```swift
185+
public enum login {
186+
public enum button {
187+
public static var positive: String { return NSLocalizedString("login.button.positive") }
188+
public static var negative: String { return NSLocalizedString("login.button.negative") }
189+
}
190+
}
191+
```
192+
193+
By default, the separator is `.`, only single character inputs are accepted for this option.
194+
180195
### --top-level-scope
181196

182197
Declares the `I, C, F, L` enums in the top level scope instead of nesting it in a top level `Shark` enum.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// File.swift
3+
// File
4+
//
5+
// Created by Kaan Dedeoglu on 24.08.21.
6+
//
7+
8+
import ArgumentParser
9+
10+
extension Character: ExpressibleByArgument {
11+
public init?(argument: String) {
12+
guard argument.count == 1 else {
13+
return nil
14+
}
15+
self = argument.first!
16+
}
17+
}

Sources/Shark/LocalizationEnumBuilder.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ enum LocalizationBuilderError: LocalizedError {
117117
}
118118

119119
enum LocalizationEnumBuilder {
120-
static func localizationsEnumString(forFilesAtPaths paths: [String], topLevelName: String) throws -> String? {
120+
static func localizationsEnumString(forFilesAtPaths paths: [String], separator: Character, topLevelName: String) throws -> String? {
121121
let termsDictionaries = try paths.compactMap({ path -> [String: String]? in
122122
guard FileManager.default.fileExists(atPath: path) else { return nil }
123123
guard let termsDictionary = NSDictionary(contentsOfFile: path) as? [String: String] else {
@@ -132,7 +132,7 @@ enum LocalizationEnumBuilder {
132132

133133
for termsDictionary in termsDictionaries {
134134
for (name, value) in termsDictionary {
135-
var parts = name.split(separator: ".")
135+
var parts = name.split(separator: separator)
136136

137137
guard parts.isEmpty == false else { continue }
138138

Sources/Shark/Shark.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ struct Options: ParsableArguments {
3232
help: "Target name of the application, useful in case there are multiple application targets")
3333
private(set) var targetName: String?
3434

35+
@Option(name: .long,
36+
help: "Separator character used to split localization keys")
37+
private(set) var separator: Character = "."
38+
3539
@Option(name: .long,
3640
help: "Localization code to use when selecting the Localizable.strings. i.e en, de, es.")
3741
private(set) var locale: String = "en"

Sources/Shark/SharkEnumBuilder.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ enum SharkEnumBuilder {
1111

1212
let imagesString = try ImageEnumBuilder.imageEnumString(forFilesAtPaths: resourcePaths.assetsPaths, topLevelName: "I")
1313
let colorsString = try ColorEnumBuilder.colorEnumString(forFilesAtPaths: resourcePaths.assetsPaths, topLevelName: "C")
14-
let localizationsString = try LocalizationEnumBuilder.localizationsEnumString(forFilesAtPaths: resourcePaths.localizationPaths, topLevelName: "L")
14+
let localizationsString = try LocalizationEnumBuilder.localizationsEnumString(
15+
forFilesAtPaths: resourcePaths.localizationPaths,
16+
separator: options.separator,
17+
topLevelName: "L"
18+
)
1519
let fontsString = try FontEnumBuilder.fontsEnumString(forFilesAtPaths: resourcePaths.fontPaths, topLevelName: "F")
1620
let dataAssetsString = try DataAssetEnumBuilder.dataAssetEnumString(forFilesAtPaths: resourcePaths.assetsPaths, topLevelName: "D")
1721
let storyboardString = try StoryboardBuilder.storyboardEnumString(forFilesAtPaths: resourcePaths.storyboardPaths, topLevelName: "S")

0 commit comments

Comments
 (0)