Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 39 additions & 38 deletions CHANGELOG.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public struct NormalizeOptions {
public let sourceLocale: String
public let harmonizeWithSource: Bool
public let sortByKeys: Bool
public let separateWithEmptyLine: Bool
}

extension NormalizeOptions: TomlCodable {
Expand All @@ -20,18 +21,20 @@ extension NormalizeOptions: TomlCodable {
subpathsToIgnore: toml.array(update, normalize, "subpathsToIgnore") ?? Constants.defaultSubpathsToIgnore,
sourceLocale: toml.string(update, normalize, "sourceLocale") ?? "en",
harmonizeWithSource: toml.bool(update, normalize, "harmonizeWithSource") ?? true,
sortByKeys: toml.bool(update, normalize, "sortByKeys") ?? true
sortByKeys: toml.bool(update, normalize, "sortByKeys") ?? true,
separateWithEmptyLine: toml.bool(update, normalize, "separateWithEmptyLine") ?? true
)
}

func tomlContents() -> String {
var lines: [String] = ["[update.normalize]"]

lines.append("paths = \(paths)")
lines.append("subpathsToIgnore = \(subpathsToIgnore)")
lines.append("sourceLocale = \"\(sourceLocale)\"")
lines.append("harmonizeWithSource = \(harmonizeWithSource)")
lines.append("sortByKeys = \(sortByKeys)")
lines.append("paths = \(self.paths)")
lines.append("subpathsToIgnore = \(self.subpathsToIgnore)")
lines.append("sourceLocale = \"\(self.sourceLocale)\"")
lines.append("harmonizeWithSource = \(self.harmonizeWithSource)")
lines.append("sortByKeys = \(self.sortByKeys)")
lines.append("separateWithEmptyLine = \(self.separateWithEmptyLine)")

return lines.joined(separator: "\n")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public struct TransformOptions {
public let typeName: String
public let translateMethodName: String
public let customLocalizableName: String?
public let separateWithEmptyLine: Bool
}

extension TransformOptions: TomlCodable {
Expand Down Expand Up @@ -43,25 +44,28 @@ extension TransformOptions: TomlCodable {
supportedLanguageEnumPath: toml.string(update, transform, "supportedLanguageEnumPath") ?? ".",
typeName: toml.string(update, transform, "typeName") ?? "BartyCrouch",
translateMethodName: toml.string(update, transform, "translateMethodName") ?? "translate",
customLocalizableName: toml.string(update, transform, "customLocalizableName")
customLocalizableName: toml.string(update, transform, "customLocalizableName"),
separateWithEmptyLine: toml.bool(update, transform, "separateWithEmptyLine") ?? true
)
}

func tomlContents() -> String {
var lines: [String] = ["[update.transform]"]

lines.append("codePaths = \(codePaths)")
lines.append("subpathsToIgnore = \(subpathsToIgnore)")
lines.append("localizablePaths = \(localizablePaths)")
lines.append("transformer = \"\(transformer.rawValue)\"")
lines.append("supportedLanguageEnumPath = \"\(supportedLanguageEnumPath)\"")
lines.append("typeName = \"\(typeName)\"")
lines.append("translateMethodName = \"\(translateMethodName)\"")
lines.append("codePaths = \(self.codePaths)")
lines.append("subpathsToIgnore = \(self.subpathsToIgnore)")
lines.append("localizablePaths = \(self.localizablePaths)")
lines.append(#"transformer = "\#(self.transformer.rawValue)""#)
lines.append(#"supportedLanguageEnumPath = "\#(self.supportedLanguageEnumPath)""#)
lines.append(#"typeName = "\#(self.typeName)""#)
lines.append(#"translateMethodName = "\#(self.translateMethodName)""#)

if let customLocalizableName = customLocalizableName {
lines.append("customLocalizableName = \"\(customLocalizableName)\"")
lines.append(#"customLocalizableName = "\#(customLocalizableName)""#)
}

lines.append("separateWithEmptyLine = \(self.separateWithEmptyLine)")

return lines.joined(separator: "\n")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public struct TranslateOptions {
public let subpathsToIgnore: [String]
public let secret: Secret
public let sourceLocale: String
public let separateWithEmptyLine: Bool
}

extension TranslateOptions: TomlCodable {
Expand All @@ -25,6 +26,7 @@ extension TranslateOptions: TomlCodable {
let paths = toml.filePaths(update, translate, singularKey: "path", pluralKey: "paths")
let subpathsToIgnore = toml.array(update, translate, "subpathsToIgnore") ?? Constants.defaultSubpathsToIgnore
let sourceLocale: String = toml.string(update, translate, "sourceLocale") ?? "en"
let separateWithEmptyLine = toml.bool(update, translate, "separateWithEmptyLine") ?? true
let secret: Secret
switch Translator(rawValue: translator) {
case .microsoftTranslator, .none:
Expand All @@ -38,7 +40,8 @@ extension TranslateOptions: TomlCodable {
paths: paths,
subpathsToIgnore: subpathsToIgnore,
secret: secret,
sourceLocale: sourceLocale
sourceLocale: sourceLocale,
separateWithEmptyLine: separateWithEmptyLine
)
}
else {
Expand All @@ -56,13 +59,14 @@ extension TranslateOptions: TomlCodable {
lines.append("subpathsToIgnore = \(subpathsToIgnore)")
switch secret {
case let .deepL(secret):
lines.append("secret = \"\(secret)\"")
lines.append(#"secret = "\#(secret)""#)

case let .microsoftTranslator(secret):
lines.append("secret = \"\(secret)\"")
lines.append(#"secret = "\#(secret)""#)
}

lines.append("sourceLocale = \"\(sourceLocale)\"")
lines.append(#"sourceLocale = "\#(sourceLocale)""#)
lines.append("separateWithEmptyLine = \(self.separateWithEmptyLine)")

return lines.joined(separator: "\n")
}
Expand Down
50 changes: 37 additions & 13 deletions Sources/BartyCrouchKit/FileHandling/StringsFileUpdater.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ public class StringsFileUpdater {
keepExistingKeys: Bool = false,
overrideComments: Bool = false,
keepWhitespaceSurroundings: Bool = false,
ignoreEmptyStrings: Bool = false
ignoreEmptyStrings: Bool = false,
separateWithEmptyLine: Bool = true
) {
do {
let newContentString = try String(contentsOfFile: otherStringFilePath)
Expand Down Expand Up @@ -132,14 +133,18 @@ public class StringsFileUpdater {
return translations.sorted(by: sortingClosure)
}()

rewriteFile(with: updatedTranslations, keepWhitespaceSurroundings: keepWhitespaceSurroundings)
rewriteFile(
with: updatedTranslations,
keepWhitespaceSurroundings: keepWhitespaceSurroundings,
separateWithEmptyLine: separateWithEmptyLine
)
}
catch {
print(error.localizedDescription, level: .error, file: path)
}
}

func insert(translateEntries: [CodeFileHandler.TranslateEntry]) {
func insert(translateEntries: [CodeFileHandler.TranslateEntry], separateWithEmptyLine: Bool) {
guard let langCode = extractLangCode(fromPath: path) else {
print("Could not extract langCode from file.", level: .warning, file: path)
return
Expand All @@ -151,14 +156,22 @@ public class StringsFileUpdater {
}
let newTranslations: [TranslationEntry] = translateEntries.map { ($0.key, getTranslation($0), $0.comment, 0) }

rewriteFile(with: oldTranslations + newTranslations, keepWhitespaceSurroundings: true)
rewriteFile(
with: oldTranslations + newTranslations,
keepWhitespaceSurroundings: true,
separateWithEmptyLine: separateWithEmptyLine
)
}

public func sortByKeys(keepWhitespaceSurroundings: Bool = false) {
public func sortByKeys(separateWithEmptyLine: Bool, keepWhitespaceSurroundings: Bool = false) {
let translations = findTranslations(inString: oldContentString)
let sortedTranslations = translations.sorted(by: translationEntrySortingClosure(lhs:rhs:), stable: true)

rewriteFile(with: sortedTranslations, keepWhitespaceSurroundings: false)
rewriteFile(
with: sortedTranslations,
keepWhitespaceSurroundings: false,
separateWithEmptyLine: separateWithEmptyLine
)
}

private func translationEntrySortingClosure(lhs: TranslationEntry, rhs: TranslationEntry) -> Bool {
Expand All @@ -181,9 +194,13 @@ public class StringsFileUpdater {
}

// Rewrites file with specified translations and reloads lines from new file.
func rewriteFile(with translations: [TranslationEntry], keepWhitespaceSurroundings: Bool) {
func rewriteFile(with translations: [TranslationEntry], keepWhitespaceSurroundings: Bool, separateWithEmptyLine: Bool)
{
do {
var newContentsOfFile = stringFromTranslations(translations: translations)
var newContentsOfFile = stringFromTranslations(
translations: translations,
separateWithEmptyLine: separateWithEmptyLine
)

if keepWhitespaceSurroundings {
var whitespacesOrNewlinesAtEnd = ""
Expand Down Expand Up @@ -239,6 +256,7 @@ public class StringsFileUpdater {
public func translateEmptyValues(
usingValuesFromStringsFile sourceStringsFilePath: String,
clientSecret: Secret,
separateWithEmptyLine: Bool,
override: Bool = false
) throws -> Int {
guard let (sourceLanguage, sourceRegion) = extractLocale(fromPath: sourceStringsFilePath) else {
Expand Down Expand Up @@ -356,7 +374,13 @@ public class StringsFileUpdater {
}
}

if translatedValuesCount > 0 { rewriteFile(with: updatedTargetTranslations, keepWhitespaceSurroundings: false) }
if translatedValuesCount > 0 {
rewriteFile(
with: updatedTargetTranslations,
keepWhitespaceSurroundings: false,
separateWithEmptyLine: separateWithEmptyLine
)
}

return translatedValuesCount
}
Expand Down Expand Up @@ -408,14 +432,14 @@ public class StringsFileUpdater {
return translations
}

func stringFromTranslations(translations: [TranslationEntry]) -> String {
func stringFromTranslations(translations: [TranslationEntry], separateWithEmptyLine: Bool) -> String {
return
translations.map { key, value, comment, _ -> String in
let translationLine = "\"\(key)\" = \"\(value)\";"
if let comment = comment { return "/*\(comment)*/\n" + translationLine }
return translationLine
}
.joined(separator: "\n\n") + "\n"
.joined(separator: separateWithEmptyLine ? "\n\n" : "\n") + "\n"
}

/// Extracts locale from a path containing substring `{language}-{region}.lproj` or `{language}.lproj`.
Expand Down Expand Up @@ -465,7 +489,7 @@ public class StringsFileUpdater {
return translations.filter { $0.value.isEmpty }
}

func harmonizeKeys(withSource sourceFilePath: String) throws {
func harmonizeKeys(withSource sourceFilePath: String, separateWithEmptyLine: Bool) throws {
let sourceFileContentString = try String(contentsOfFile: sourceFilePath)

let sourceTranslations = findTranslations(inString: sourceFileContentString)
Expand Down Expand Up @@ -501,7 +525,7 @@ public class StringsFileUpdater {
fixedTranslations = fixedTranslations.filter { $0.key != keyToRemove }
}

rewriteFile(with: fixedTranslations, keepWhitespaceSurroundings: true)
rewriteFile(with: fixedTranslations, keepWhitespaceSurroundings: true, separateWithEmptyLine: separateWithEmptyLine)
}
}

Expand Down
24 changes: 18 additions & 6 deletions Sources/BartyCrouchKit/OldCommandLine/CommandLineActor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ public class CommandLineActor {
override: Bool,
verbose: Bool,
secret: Secret,
locale: String
locale: String,
separateWithEmptyLine: Bool
) {
let inputFilePaths =
paths.flatMap {
Expand All @@ -132,7 +133,14 @@ public class CommandLineActor {

let outputStringsFilePaths = StringsFilesSearch.shared.findAllLocalesForStringsFile(sourceFilePath: inputFilePath)
.filter { $0 != inputFilePath }
self.translate(secret: secret, inputFilePath, outputStringsFilePaths, override: override, verbose: verbose)
self.translate(
secret: secret,
inputFilePath,
outputStringsFilePaths,
override: override,
verbose: verbose,
separateWithEmptyLine: separateWithEmptyLine
)
}
}

Expand All @@ -143,7 +151,8 @@ public class CommandLineActor {
verbose: Bool,
locale: String,
sortByKeys: Bool,
harmonizeWithSource: Bool
harmonizeWithSource: Bool,
separateWithEmptyLine: Bool
) {
let sourceFilePaths =
paths.flatMap {
Expand Down Expand Up @@ -176,7 +185,8 @@ public class CommandLineActor {
for filePath in targetStringsFilePaths {
let stringsFileUpdater = StringsFileUpdater(path: filePath)
do {
try stringsFileUpdater?.harmonizeKeys(withSource: sourceFilePath)
try stringsFileUpdater?
.harmonizeKeys(withSource: sourceFilePath, separateWithEmptyLine: separateWithEmptyLine)
}
catch {
print("Could not harmonize keys with source file at path \(sourceFilePath).", level: .error)
Expand All @@ -188,7 +198,7 @@ public class CommandLineActor {
if sortByKeys {
for filePath in allStringsFilePaths {
let stringsFileUpdater = StringsFileUpdater(path: filePath)
stringsFileUpdater?.sortByKeys()
stringsFileUpdater?.sortByKeys(separateWithEmptyLine: separateWithEmptyLine)
}
}
}
Expand Down Expand Up @@ -418,7 +428,8 @@ public class CommandLineActor {
_ inputFilePath: String,
_ outputStringsFilePaths: [String],
override: Bool,
verbose: Bool
verbose: Bool,
separateWithEmptyLine: Bool
) {
var overallTranslatedValuesCount = 0
var filesWithTranslatedValuesCount = 0
Expand All @@ -430,6 +441,7 @@ public class CommandLineActor {
let translationsCount = try stringsFileUpdater.translateEmptyValues(
usingValuesFromStringsFile: inputFilePath,
clientSecret: secret,
separateWithEmptyLine: separateWithEmptyLine,
override: override
)

Expand Down
6 changes: 0 additions & 6 deletions Sources/BartyCrouchKit/TaskHandlers/CodeTaskHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@ import Foundation

struct CodeTaskHandler {
let options: CodeOptions

init(
options: CodeOptions
) {
self.options = options
}
}

extension CodeTaskHandler: TaskHandler {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@ import Foundation

struct InterfacesTaskHandler {
let options: InterfacesOptions

init(
options: InterfacesOptions
) {
self.options = options
}
}

extension InterfacesTaskHandler: TaskHandler {
Expand Down
6 changes: 0 additions & 6 deletions Sources/BartyCrouchKit/TaskHandlers/LintTaskHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@ import Foundation

struct LintTaskHandler {
let options: LintOptions

init(
options: LintOptions
) {
self.options = options
}
}

extension LintTaskHandler: TaskHandler {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@ import Foundation

struct NormalizeTaskHandler {
let options: NormalizeOptions

init(
options: NormalizeOptions
) {
self.options = options
}
}

extension NormalizeTaskHandler: TaskHandler {
Expand All @@ -23,7 +17,8 @@ extension NormalizeTaskHandler: TaskHandler {
verbose: GlobalOptions.verbose.value,
locale: options.sourceLocale,
sortByKeys: options.sortByKeys,
harmonizeWithSource: options.harmonizeWithSource
harmonizeWithSource: options.harmonizeWithSource,
separateWithEmptyLine: options.separateWithEmptyLine
)
}
}
Expand Down
Loading