Search code examples
iosswiftswiftuilocalizationstring-catalog

Use multiple string catalogs for localization in SwiftUI


I'm developing a SwiftUI app and have plenty of strings to translate so I started with a single string catalog in which I have all my translations and it works great. However, there are too many strings already and the catalog is becoming a bit too large.

Is there a way of having multiple string catalogs and only including certain strings of text in each of them (i.e., one string catalog per view or something similar)?

I have tried creating another string catalog but have to add the strings manually to this one and the translations don't get picked up by the code when compiling.


Solution

  • I have tried creating another string catalog but have to add the strings manually to this one and the translations don't get picked up by the code when compiling.

    It’s possible to have Xcode extract localisations for different strings tables (I.e a Strings Catalog), but you have to explicitly specify the “table name” in your Swift code.

    For example:

    Text("foo", tableName: "Settings")
    

    This would then extract a string called foo into Settings.xcstrings.

    While this is great, and it’s encouraged by Apple in the WWDC 2023 session video on Strings Catalogs, having to specify the tableName every time is inconvenient and clutters your SwiftUI code somewhat which is why it’s not a common pattern we see being adopted.

    You could work around this by extracting your string usage into some constants that you define with the help of LocalizedStringResource and if you want to go one step further, there are even tools that will generate this code for you[1]. Using such tools though requires a bit of a shift in your way of thinking about the source of truth for localised content, something that I wrote about in a blog post.


    1I am the author of the linked tool.