FlineDev / BartyCrouch

Localization/I18n: Incrementally update/translate your Strings files from .swift, .h, .m(m), .storyboard or .xib files.
MIT License
1.36k stars 121 forks source link

BartyCrouch with SwiftGen creates empty string values #253

Closed CavalcanteLeo closed 2 years ago

CavalcanteLeo commented 2 years ago

I'm getting empties string value when combining BartyCrouch and SwiftGen, it shows me this error:

error:

Starting Task 'Code Transform' ...
2022-05-25 03:26:47.400: ⚠️  Could not find a langCode for langCase 'english' when transforming translation.
2022-05-25 03:26:47.400: ⚠️  Could not find a langCode for langCase 'portuguese' when transforming translation.
2022-05-25 03:26:47.400: ℹ️  Found translate entry with key 'type-a-valid-email' and 0 translations.
2022-05-25 03:26:47.400: ℹ️  Transformed 'BartyCrouch.translate(
                    key: "type-a-valid-email",
                    translations: [
                        .english: "type a valid email",
                        .portuguese: "Digite um e-mail válido"
                    ],
                    comment: "When the user types a wrong email (textfield hint)"
                 )' to 'L10n.typeAValidEmail'.
Task 'Code Transform' took 0.121 seconds.
Starting Task 'Normalize' ...

and my string files are empty like this:

Screen Shot 2022-05-25 at 03 44 34 Screen Shot 2022-05-25 at 03 41 09

all my configs:

.bartycrouch.toml:

[update]
tasks = ["interfaces", "code", "transform", "normalize"]

[update.interfaces]
defaultToBase = false
ignoreEmptyStrings = false
ignoreKeys = ["#bartycrouch-ignore!", "#bc-ignore!", "#i!"]
paths = ["."]
subpathsToIgnore = [".git", "carthage", "pods", "build", ".build", "docs"]
unstripped = false

[update.code]
additive = true
codePaths = ["."]
defaultToKeys = false
ignoreKeys = ["#bartycrouch-ignore!", "#bc-ignore!", "#i!"]
localizablePaths = ["."]
plistArguments = true
subpathsToIgnore = [".git", "carthage", "pods", "build", ".build", "docs"]
unstripped = false

[update.transform]
codePaths = ["."]
localizablePaths = ["."]
subpathsToIgnore = [".git", "carthage", "pods", "build", ".build", "docs"]
supportedLanguageEnumPath = "."
transformer = "swiftgenStructured"
translateMethodName = "translate"
typeName = "BartyCrouch"

[update.normalize]
harmonizeWithSource = true
paths = ["."]
sortByKeys = true
sourceLocale = "en"
subpathsToIgnore = [".git", "carthage", "pods", "build", ".build", "docs"]

[lint]
duplicateKeys = true
emptyValues = true
paths = ["."]
subpathsToIgnore = [".git", "carthage", "pods", "build", ".build", "docs"]

I also tried to change the supportedLanguageEnumPath

supportedLanguageEnumPath = "LitiSaude/Helper/"

swiftgen

strings:
  inputs: LitiSaude/Resources/LocalizableStrings/en.lproj/Localizable.strings
  # inputs: LitiSaude/Resources/LocalizableStrings/en.lproj
  # filter: .+\.strings$
  outputs:
    - templateName: structured-swift5
      output: LitiSaude/Generated/Strings.swift

BartyCrouch.swift

import Foundation

enum BartyCrouch {
    enum SupportedLanguage: String {
        case english = "en"
        case portuguese = "pt-BR"
        case spanish = "es"
    }

    static func translate(key: String, translations: [SupportedLanguage: String], comment: String? = nil) -> String {
        let typeName = String(describing: BartyCrouch.self)
        let methodName = #function

        print(
            "Warning: [BartyCrouch]",
            "Untransformed \(typeName).\(methodName) method call found with key '\(key)' and base translations '\(translations)'.",
            "Please ensure that BartyCrouch is installed and configured correctly."
        )

        // fall back in case something goes wrong with BartyCrouch transformation
        return "BC: TRANSFORMATION FAILED!"
    }
}

and the Strings.swift (for swiftgen):

// swiftlint:disable all
// Generated using SwiftGen — https://github.com/SwiftGen/SwiftGen

import Foundation

// swiftlint:disable superfluous_disable_command file_length implicit_return

// MARK: - Strings

// swiftlint:disable explicit_type_interface function_parameter_count identifier_name line_length
// swiftlint:disable nesting type_body_length type_name vertical_whitespace_opening_braces
internal enum L10n {
  /// 
  internal static let accountCreated = L10n.tr("Localizable", "AccountCreated")
  /// 
  internal static let errorToLogin = L10n.tr("Localizable", "ErrorToLogin")
  /// 
  internal static let typeAValidEmail = L10n.tr("Localizable", "type-a-valid-email")
  /// 
  internal static let typeAValidPassword = L10n.tr("Localizable", "TypeAValidPassword")
}
// 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 L10n {
  private static func tr(_ table: String, _ key: String, _ args: CVarArg...) -> String {
    let format = BundleToken.bundle.localizedString(forKey: key, value: nil, table: table)
    return String(format: format, locale: Locale.current, arguments: args)
  }
}

// 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

a little related to this https://github.com/FlineDev/BartyCrouch/issues/133

FlineDevPublic commented 2 years ago

Hi @CavalcanteLeo, thank you for reporting this issue. I just checked your details and it really looks like exactly the same issue as #133, but let's elaborate together a bit more. From what it looks, all seems correctly configured, although you didn't set any paths like strongly recommended in the README (the "." path causes problems for some people, but there's no other sensible default I could imagine).

Could you please try providing a minimum demo sample that I can investigate? I am actually currently streaming while writing these sentences and I might just check your project in my next stream, Friday next week at 3 PM (CET), so feel free to provide more details and join me while I'm having a look so you can learn directly or provide more info / ask questions in chat. I'll add the timecode where I looked at your issue here later.


This comment was written during my regular Open Source live stream on Twitch. Follow me there to support my work!

CavalcanteLeo commented 2 years ago

Dude, i don't even know u, but i already love u, after i added localizablePaths it works, and this mtf project is a time saver that save a lot of work (and time)

[update.transform]
codePaths = ["."] 
localizablePaths = ["LitiSaude/Resources/Localization/LocalizableStrings"] 
subpathsToIgnore = [".git", "carthage", "pods", "build", ".build", "docs"] 
supportedLanguageEnumPath = "LitiSaude/Resources/Localization/" 
transformer = "swiftgenStructured" # foundation | swiftgenStructured
translateMethodName = "translate" 
typeName = "BartyCrouch" 

also, i'd love to watch u on Twitch, but i will be flying at the exactly time (heading to cupertino for WWDC)