Translating your iOS app is essential for reaching a global audience. Apple provides excellent built-in tools for localization in Xcode, making it straightforward to support multiple languages. This guide will walk you through the process of translating an iOS app using Swift.
Understanding iOS Localization
iOS localization uses Localizable.strings files to store translations for different languages. Xcode automatically loads the correct strings based on the user’s device language settings.
Step 1: Enable Localization in Your Project
First, you need to enable localization for your project:
- Open your project in Xcode
- Select your project in the Project Navigator
- Go to the Info tab
- Under Localizations, click the + button
- Add the languages you want to support (e.g., Spanish, French, German)
Step 2: Create Localizable.strings Files
For each language, you’ll create a Localizable.strings file:
- In Xcode, go to File > New > File
- Select Strings File under Resource
- Name it
Localizable.strings - In the File Inspector, click Localize…
- Select the languages you want to support
Step 3: Add Translation Strings
In your Localizable.strings files, add key-value pairs for each language:
English (Localizable.strings):
"welcome_message" = "Welcome to our app!";
"button_submit" = "Submit";
"error_network" = "Network error. Please try again.";
"items_count" = "%d items";
Spanish (Localizable.strings (es)):
"welcome_message" = "¡Bienvenido a nuestra aplicación!";
"button_submit" = "Enviar";
"error_network" = "Error de red. Por favor, inténtalo de nuevo.";
"items_count" = "%d elementos";
Step 4: Use NSLocalizedString in Your Code
Replace hardcoded strings with NSLocalizedString:
Before:
let welcomeLabel = UILabel()
welcomeLabel.text = "Welcome to our app!"
After:
let welcomeLabel = UILabel()
welcomeLabel.text = NSLocalizedString("welcome_message", comment: "Welcome message displayed on home screen")
Step 5: Handle String Formatting
For strings with variables, use String.localizedStringWithFormat:
let itemCount = 5
let message = String.localizedStringWithFormat(
NSLocalizedString("items_count", comment: "Number of items"),
itemCount
)
// English: "5 items"
// Spanish: "5 elementos"
Step 6: Pluralization
iOS supports pluralization using .stringsdict files. Create a Localizable.stringsdict file:
Localizable.stringsdict:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>items_count</key>
<dict>
<key>NSStringLocalizedFormatKey</key>
<string>%#@items@</string>
<key>items</key>
<dict>
<key>NSStringFormatSpecTypeKey</key>
<string>NSStringPluralRuleType</string>
<key>NSStringFormatValueTypeKey</key>
<string>d</string>
<key>zero</key>
<string>No items</string>
<key>one</key>
<string>%d item</string>
<key>other</key>
<string>%d items</string>
</dict>
</dict>
</dict>
</plist>
Usage:
let count = 1
let message = String.localizedStringWithFormat(
NSLocalizedString("items_count", comment: ""),
count
)
// English: "1 item" (singular)
// English: "5 items" (plural)
Step 7: Localize Images and Assets
You can also localize images:
- Select an image in your Assets.xcassets
- In the Attributes Inspector, check Localize
- Provide different images for each language
Or use localized image names:
let imageName = NSLocalizedString("icon_name", comment: "")
let image = UIImage(named: imageName)
Step 8: Format Dates and Numbers
Use Locale and DateFormatter for localized formatting:
let date = Date()
let formatter = DateFormatter()
formatter.locale = Locale.current
formatter.dateStyle = .long
formatter.timeStyle = .short
let localizedDate = formatter.string(from: date)
// English: "March 20, 2025 at 10:00 AM"
// Spanish: "20 de marzo de 2025, 10:00"
For numbers:
let number = 1234.56
let formatter = NumberFormatter()
formatter.locale = Locale.current
formatter.numberStyle = .decimal
let localizedNumber = formatter.string(from: NSNumber(value: number))
// English: "1,234.56"
// Spanish: "1.234,56"
Step 9: Test Your Localization
Test your app in different languages:
- In Xcode, go to Product > Scheme > Edit Scheme
- Select Run and go to Options
- Change Application Language to test different languages
- Run your app to see the translations
Or test on a device:
- Go to Settings > General > Language & Region
- Change the device language
- Launch your app
Best Practices
1. Use Descriptive Keys
Bad:
NSLocalizedString("msg1", comment: "")
Good:
NSLocalizedString("welcome_screen_title", comment: "Title displayed on the welcome screen")
2. Provide Context in Comments
Always include comments to help translators understand the context:
NSLocalizedString("delete_button", comment: "Button to delete an item. Shown in the item detail view.")
3. Avoid Concatenating Strings
Bad:
let message = NSLocalizedString("hello", comment: "") + " " + name
Good:
let message = String.localizedStringWithFormat(
NSLocalizedString("hello_user", comment: "Greeting with user name"),
name
)
4. Handle Right-to-Left Languages
iOS automatically handles RTL languages like Arabic and Hebrew. Ensure your UI constraints support both directions:
// Use leading/trailing instead of left/right
button.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20)
5. Test String Lengths
Some languages are longer than others. German and Finnish, for example, can be 30-50% longer than English. Design your UI to accommodate longer text.
Common Pitfalls
1. Forgetting to Localize All Strings
Make sure to localize all user-facing strings, including:
- Button labels
- Error messages
- Alert titles
- Placeholder text
2. Hardcoding Format Strings
Bad:
let price = String(format: "$%.2f", amount)
Good:
let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.locale = Locale.current
let price = formatter.string(from: NSNumber(value: amount)) ?? ""
3. Not Testing Edge Cases
Test with:
- Very long strings
- Special characters
- Numbers and dates
- Plural forms
Advanced: Dynamic Language Switching
To allow users to change language within the app:
func changeLanguage(to language: String) {
UserDefaults.standard.set([language], forKey: "AppleLanguages")
UserDefaults.standard.synchronize()
// Restart app or reload UI
Bundle.setLanguage(language)
}
Note: This requires additional setup and may require app restart.
Conclusion
Localizing your iOS app with Swift is straightforward when you follow these steps:
- Enable localization in Xcode
- Create
Localizable.stringsfiles for each language - Use
NSLocalizedStringthroughout your code - Handle pluralization with
.stringsdictfiles - Format dates and numbers using
Locale - Test thoroughly in all supported languages
By following these practices, you’ll create an app that provides a native experience for users worldwide, significantly expanding your potential user base.
Streamline Your iOS Localization Workflow
Managing translations for multiple languages can become complex as your app grows. Consider using a translation management platform to:
- Collaborate with translators
- Keep translations in sync with your codebase
- Automate the translation workflow
- Maintain consistency across all languages
Ready to take your iOS app global? Explore AZbox’s localization platform and streamline your translation workflow: