Reputation: 17299
I want to migrate my project in the long-term to use the brand-new String Catalogs in Xcode. However, I still have a Localizable.strings
file in my project and I don't want to migrate the entire file at this moment. So I intend to use both in parallel for some time.
In the related WWDC video, Apple says that
String Catalogs can coexist with the legacy formats.
However, when I add a String Catalog to my project and give it the default name Localizable.xcstrings
, Xcode throws a compile error:
Localizable.xcstrings cannot co-exist with other .strings or .stringsdict tables with the same name.
which is kind of contradictory.
Anyway, when I renamed the String Catalog file (e.g. to WidgetLocalizable.xcstrings
), the project build successfully. However, Xcode does not automatically create the entries in the String Catalog for me as I no longer use the default name.
I could add all those keys manually, but this has 3 downsides:
table
name every time I create a LocalizedStringResource
in code.Upvotes: 3
Views: 4625
Reputation: 12678
Can I use a custom file name for a String Catalog and still have Xcode automatically create the keys for me? How?
Yes, but you have to annotate your source code with the correct table name.
It's not immediately obvious, but the name of your .strings/.xcstrings file is what is referred to as the "Table Name" when doing a localized string lookup. The default value (Localizable) happens to be the default table name used if not otherwise specified for any of the apis.
So you could get keys generated for your WidgetLocalizable.xcstrings Strings Catalog like so:
// Legacy
let _ = NSLocalizedString("MyKey", tableName: "WidgetLocalizable")
// iOS 15+
let _ = String(localized: "MyKey", table: "WidgetLocalizable")
// SwiftUI
Text("MyKey", tableName: "WidgetLocalizable")
// LocalizedStringResource (iOS 16+)
LocalizedStringResource("MyKey", table: "WidgetLocalizable")
When the compiler detects these references in your source, it'll correctly populate them into WidgetLocalizable.xcstrings.
Is there any way to tell entire files or folders in Xcode to use a specific non-standard String Catalog file, so I don't need to append it to every single string I create in code?
Nope, not out of the box.
Although an alternative approach is to use an external tool instead to generate helper symbols/constants from your Strings Catalogs.
It goes against your original desire to avoid having to define the strings in the Strings Catalog manually, although I personally think that having the Strings Catalog as the source of truth ends up being more preferable (especially because of this scenario of needing to reference the table name in source).
In fact, I wrote xcstrings-tool specifically to cover this workflow and I wrote about my motivation on my blog if you are interested.
The general idea is to use many Strings Catalogs as a form of namespacing. For example, having App.xcstrings, Map.xcstrings, Profile.xcstrings etc. Then, with the symbols that XCStrings Tool generates, you can reference the phrases trouble free in your source code as Text(.app.name)
or Text(.profile.title)
and so on
Upvotes: 4