Skip to content

How to avoid translating certain strings in SwiftUI

Published: at 04:50 PM

In the previous blog “How to localize the strings as parameter in SwiftUI”, we explained how to avoid missing string translations. Today, we will discuss how to “ignore” certain strings and prevent them from being added to our Localizable.strings file.

Why

In many apps, there are numerous strings that do not require internationalization. Take the following View as an example, where we’ve added a TextField control for users to enter the OpenAI service API Key, and a Label to displaying prompt text and a 🔑 icon:

struct ContentView: View {
  @State private var keyString: String = ""

  var body: some View {
    HStack {
      Label("OpenAI Key", systemImage: "key")
      TextField("sk-***********", text: $keyString)
    }
    .padding()
  }
}

Image.png

After building the project, we can view the Localizable.xcstring to see that the text has been added to the list of translatable items.

Image.png

Now we are facing some problems:

  1. OpenAI Key” is a very generic term and it acts more like a proprietary noun. Regardless of whether the user’s native language is English, Chinese, or Japanese, it seems that there is no need for internationalization.
  2. sk-*****” serves as a placeholder to prompt users, and we expect to use it to ensure that users do not fill in the wrong content. Therefore, it needs to completely display the general format of a Key, and there is no need for internationalization neither.

We may say: Hey, just let them stay in the Localizable.xcstring, it’s no big deal, they will work fine even if we don’t do anything with them.

Indeed, we could do that, but it would bring some side effects:

  1. We cannot measure the current language’s internationalization status by checking the completion percentage on the language tags in Localizable.xcstring. When a large amount of blank text is left in the project, the completion percentage will always stay at a relatively low level. This forces us to frequently check the internationalization status of the current language to ensure no important text is missed.

Image.png

  1. Blank entries in Localizable.xcstring can interfere with our normal internationalization efforts. Imagine we added a string that needs translation; when we open Localizable.xcstring, we find many untranslated entries, and we need to search among them to find the newly added string.

  2. As the number of languages we support increases, our blank entries will multiply, eventually making Localizable.xcstring a mess.

Solution

We have a very quick way to kick a string out of Localizable.xcstrings, the key is to use a special Text:

Image.png

The improved code is as follows: Text(verbatim: "Content") which will ignore “Content”.

struct ContentView: View {
  @State private var keyString: String = ""

  var body: some View {
    HStack {
      Label(
        title: { Text(verbatim: "OpenAI Key") },
        icon: { Image(systemName: "key") }
      )
      TextField(text: $keyString) {
        Text(verbatim: "sk-***********")
      }
    }
    .padding()
  }
}

Image.png

As you can see, in order to gain additional benefits from using Text(""), we appropriately modified the initialization methods for Label and TextField to accept the content of Text.

Finally, we achieved a clean and tidy Localizable.xcstring.

Image.png