When starting a new app, it’s important to have a mechanism in place for Localization. In this case, we will keep it simple by having a local Localizable file, with one variation in English and one in Spanish.
The benefit of having this mechanism in place before starting coding the new app, is that we will be able to collect all the strings the app needs to display in one single place.
We can use that file in the future to decide if we want to keep having local translations, or if we want a more dynamic approach using a backend.
Create the Localizable file
This is the easiest step, just create a new Strings
file in Xcode and call it Localizable
.
In the right panel, you can select more languages. Each language will have it’s own .strings
file.
Example Localizable file:
English:
"Welcome$1" = "Welcome %@";
"Nice to meet you" = "Nice to meet you";
Spanish:
"Welcome$1" = "Bienvenido %@";
"Nice to meet you" = "Encantado de conocerte";
Add the String extension
To be able to localize our strings from the code, we can create this simple extension to String:
Usage
The last step is to actually use the localized strings.
Even though SwiftUI’s Text automatically looks for the localized strings, I think it’s more clear to always include the .localized
suffix to the strings:
The result
English | Spanish |
---|---|