Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Localized Strings in iOS Development

Introduction

Localization is the process of adapting an application to support multiple languages and regions. In iOS development, localized strings are used to display text in different languages based on the user's preferences.

Creating Localized Strings

To start with localization, you need to create a Localizable.strings file. This file will contain key-value pairs where the key is a unique identifier, and the value is the translated string.

Example:

"hello_world" = "Hello, World!";

To add support for another language, create another Localizable.strings file in a specific language folder (e.g., fr.lproj for French) and add the translations.

Example:

"hello_world" = "Bonjour, le monde!";

Accessing Localized Strings in Code

Use the NSLocalizedString function to access localized strings in your code. This function retrieves the value for a specified key from the Localizable.strings file.

let greeting = NSLocalizedString("hello_world", comment: "A friendly greeting")

Handling Pluralization and Gender

Localization often requires handling pluralization and gender. iOS provides Stringsdict files to handle different grammatical rules.

To create a Stringsdict file, add a new file to your project and select "Property List." Name it Localizable.stringsdict. Add the necessary keys and values to handle pluralization.

Example:

<?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>apples_count</key>
    <dict>
        <key>NSStringLocalizedFormatKey</key>
        <string>%#@apples@</string>
        <key>apples</key>
        <dict>
            <key>NSStringFormatSpecTypeKey</key>
            <string>NSStringPluralRuleType</string>
            <key>NSStringFormatValueTypeKey</key>
            <string>d</string>
            <key>one</key>
            <string>%d apple</string>
            <key>other</key>
            <string>%d apples</string>
        </dict>
    </dict>
</dict>
</plist>
                

Testing Localized Strings

To test localized strings, change the language settings on your iOS device or simulator. This will allow you to see how your app behaves with different languages.

Additionally, you can use the "Scheme" menu in Xcode to run your app in different languages without changing the device settings.

Steps:

  1. Open your project in Xcode.
  2. Go to the "Product" menu and select "Scheme" > "Edit Scheme...".
  3. Select the "Run" option from the left panel.
  4. Go to the "Options" tab and select a language from the "Application Language" dropdown.
  5. Click "Close" and run your app.

Conclusion

Localization is a crucial aspect of creating a user-friendly application that can reach a global audience. By using localized strings, you ensure that your app can adapt to different languages and regions seamlessly.

Remember to keep your localization files organized and test your app thoroughly in all supported languages to provide the best user experience.