You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SwiftUI中如何导出应用内所有用于本地化的文本?

How to Export All Localizable Text in SwiftUI (Including Text Passed to Custom Views)

Great question—this is a common gotcha when working with reusable SwiftUI views and localization. Xcode's localization scanner only picks up strings directly passed to Text() or wrapped in localization-aware types by default. Here are the two most straightforward fixes to make sure all your app's localizable text gets exported:

1. Use LocalizedStringKey for Custom View Parameters

SwiftUI's Text actually accepts a LocalizedStringKey (not just a plain String) under the hood. When you pass a string literal directly to Text(), Swift implicitly converts it to LocalizedStringKey, which Xcode's scanner recognizes. By changing your custom view's parameter type to LocalizedStringKey, you tell Xcode to scan the string literals passed from parent views:

// Custom view with LocalizedStringKey parameter
struct ListItemView: View {
    var leadingText: LocalizedStringKey // Replace String with this type
    var body: some View {
        Text(leadingText)
    }
}

// Parent view passing a string literal (Xcode will detect this!)
struct AddItemView: View {
    var body: some View {
        ListItemView(leadingText: "Add")
    }
}

Why this works:

LocalizedStringKey is SwiftUI's dedicated type for localization-aware text. Xcode's Export Localization tool scans for instances of this type (and implicit conversions from string literals) to populate your .strings files automatically.

2. Wrap Strings in NSLocalizedString in Parent Views

If you need to keep your custom view's parameter as a plain String (for example, if you sometimes pass dynamic, non-localized text), you can wrap the string literals in NSLocalizedString when passing them to the custom view. Xcode's scanner explicitly looks for this macro:

// Custom view with String parameter
struct ListItemView: View {
    var leadingText: String
    var body: some View {
        Text(leadingText)
    }
}

// Parent view using NSLocalizedString (Xcode will detect this!)
struct AddItemView: View {
    var body: some View {
        ListItemView(leadingText: NSLocalizedString("Add", comment: "Button text for adding a new item"))
    }
}

Pro Tip:

Always include a descriptive comment in NSLocalizedString—this helps translators understand the context of the text, and it will show up in your exported localization files to provide context.

Key Notes

  • Dynamic text loaded from APIs, JSON, or user input won't be picked up by Xcode's scanner—you'll need to handle those cases with a separate localization system (like fetching localized strings from a server).
  • If you have existing custom views, you can update them incrementally using either of these methods without breaking existing code (string literals will implicitly convert to LocalizedStringKey).

内容的提问来源于stack exchange,提问作者mahan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 18:09:09