Skip to content

Commit

Permalink
Make localization splitting configurable with the --separator option (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
kaandedeoglu authored Aug 24, 2021
1 parent bbcc724 commit 57d0054
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 3 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,21 @@ In case your Xcode project has multiple application targets, you should specify
shark $PROJECT_FILE_PATH $PROJECT_DIR/$PROJECT_NAME --target MyAppTarget
```

### --separator

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`:

```swift
public enum login {
public enum button {
public static var positive: String { return NSLocalizedString("login.button.positive") }
public static var negative: String { return NSLocalizedString("login.button.negative") }
}
}
```

By default, the separator is `.`, only single character inputs are accepted for this option.

### --top-level-scope

Declares the `I, C, F, L` enums in the top level scope instead of nesting it in a top level `Shark` enum.
Expand Down
17 changes: 17 additions & 0 deletions Sources/Shark/Character+ExpressibleByArgument.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// File.swift
// File
//
// Created by Kaan Dedeoglu on 24.08.21.
//

import ArgumentParser

extension Character: ExpressibleByArgument {
public init?(argument: String) {
guard argument.count == 1 else {
return nil
}
self = argument.first!
}
}
4 changes: 2 additions & 2 deletions Sources/Shark/LocalizationEnumBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ enum LocalizationBuilderError: LocalizedError {
}

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

for termsDictionary in termsDictionaries {
for (name, value) in termsDictionary {
var parts = name.split(separator: ".")
var parts = name.split(separator: separator)

guard parts.isEmpty == false else { continue }

Expand Down
4 changes: 4 additions & 0 deletions Sources/Shark/Shark.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ struct Options: ParsableArguments {
help: "Target name of the application, useful in case there are multiple application targets")
private(set) var targetName: String?

@Option(name: .long,
help: "Separator character used to split localization keys")
private(set) var separator: Character = "."

@Option(name: .long,
help: "Localization code to use when selecting the Localizable.strings. i.e en, de, es.")
private(set) var locale: String = "en"
Expand Down
6 changes: 5 additions & 1 deletion Sources/Shark/SharkEnumBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ enum SharkEnumBuilder {

let imagesString = try ImageEnumBuilder.imageEnumString(forFilesAtPaths: resourcePaths.assetsPaths, topLevelName: "I")
let colorsString = try ColorEnumBuilder.colorEnumString(forFilesAtPaths: resourcePaths.assetsPaths, topLevelName: "C")
let localizationsString = try LocalizationEnumBuilder.localizationsEnumString(forFilesAtPaths: resourcePaths.localizationPaths, topLevelName: "L")
let localizationsString = try LocalizationEnumBuilder.localizationsEnumString(
forFilesAtPaths: resourcePaths.localizationPaths,
separator: options.separator,
topLevelName: "L"
)
let fontsString = try FontEnumBuilder.fontsEnumString(forFilesAtPaths: resourcePaths.fontPaths, topLevelName: "F")
let dataAssetsString = try DataAssetEnumBuilder.dataAssetEnumString(forFilesAtPaths: resourcePaths.assetsPaths, topLevelName: "D")
let storyboardString = try StoryboardBuilder.storyboardEnumString(forFilesAtPaths: resourcePaths.storyboardPaths, topLevelName: "S")
Expand Down

0 comments on commit 57d0054

Please sign in to comment.