Skip to content

The defaulted LocalizedStringResource does not work with localized resources #122

@nonameplum

Description

@nonameplum

Hey,

It is probably not the xcstrings-tool fault, but I played around to verify how it would work for my setup, that's why I created an issue here, to hopefully find a way to fix the problem.

The issue is that when SPM package contains localized resources, either the new String Catalog or legacy *.strings, the localized string by using the default LocalizedStringResource won't be correctly resolved in the app.

I made a simple app to demonstrate the problem: LocalizationSpm.zip

I made sure to set up CFBundleAllowMixedLocalizations (as suggested on swift forums) and in Package.swift define defaultLocalization: "en".

I created a bunch of Text usage checks that give this outcome when the app is running in the German language (by configuring the App Language scheme):
Screenshot 2025-01-06 at 17 01 49

This respective code in TestView.swift:

let string: String = {
    let resource = LocalizedStringResource(localizable: .error)
    let value = String(localized: resource)
    return value
}()
Text("1: ") + Text(string)
// Works
Text("2: ") + Text(String(localized: "error", bundle: .module))
Text("3: ") + Text(localizable: .error)
Text("4: ") + Text(.localizable(.error))
Text("5: ") + Text(.localizable(.error), bundle: .module)
Text("6: ") + Text("error")
// Works
Text("7: ") + Text("error", bundle: .module)
// Works
Text("8: ") + Text("error", tableName: "Localizable", bundle: .module)
Text("9: ") + Text("error", tableName: "Wrong", bundle: .module)
Text("10: ") + Text(LocalizedStringKey("error"))
Text("11: ") + Text(LocalizedStringKey(stringLiteral: "error"))
// Works
Text("12: ") + Text(LocalizedStringKey("error"), bundle: .module)
Text("13: ") + Text(
    LocalizedStringResource(
        "error", defaultValue:
        "",
        bundle: .forClass(BundleClass.self)
    )
)
Text("14: ") + Text(LocalizedStringResource(
    "error",
    defaultValue: "N/A",
    locale: .init(identifier: "de"),
    bundle: .atURL(Bundle.module.bundleURL)
))
Text("15: ") + Text(LocalizedStringResource(
    "error",
    defaultValue: "N/A",
    bundle: .atURL(Bundle.module.bundleURL)
))
// Works
Text("16: ") + Text(NSLocalizedString("error", bundle: Bundle.module, comment: ""))
// Works
Text("17: ") + Text(String(localizable: .error))
Text("18: ") + Text(.localizable(.hello("John")))
// Works
Text("19: ") + Text(String(localizable: .hello("John")))

The biggest issue is that none of those work:

Text("3: ") + Text(localizable: .error)
Text("4: ") + Text(.localizable(.error))

even this one:

Text("5: ") + Text(.localizable(.error), bundle: .module)

because internally all of those in the generated code will be resolved as LocalizedStringResource which seems to be not working in this case.

Do I set up something wrong or is this a known issue of SPM localized resources with LocalizedStringResource?

As you can see variants 2, 7, 8, 12, 16, 17, 19 works properly.

Most importantly this will work:

Text(String(localizable: .error))

This could be used as a workaround at the moment if nothing better cannot be used.

Btw.
In SwiftUI Preview e.g.:

TestView().environment(\.locale, .init(identifier: "de"))

It will work as you would expect with the given examples.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions