iOS
device language
programming
app development
multilingual support

Getting current device language in iOS?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

With the global reach of applications today, supporting multiple languages is no longer a luxury but a necessity. Adapting your iOS app to cater to various languages not only enhances user experience but also broadens your audience base. Accessing the current device language in iOS is the first critical step for providing tailored content based on the user's preferences.

In this article, we'll delve into how to retrieve the current language setting of an iOS device, discuss technical aspects, and provide code examples to effectively implement localization in your iOS applications.

Understanding Locale and Language

Locale vs. Language

In iOS development, it's crucial to understand the distinction between "locale" and "language":

  • Locale: Represents regional settings that include language, currency, date format, etc. Examples include en_US for English (United States) and fr_FR for French (France).
  • Language: Primarily represents linguistic aspects, such as en for English or fr for French, without regional specifics.

NSLocale and Preferred Language

NSLocale

NSLocale is a foundation class in iOS used to represent locale information. It helps in fetching data that’s locale-specific, such as currency, measurement units, etc.

Preferred Language

For language-specific operations, iOS provides APIs to get the user's preferred language from the system settings. You can use this information to present content and interface in the user's language.

How to Retrieve Current Device Language

To access the current language of the device, iOS offers a straightforward approach using the user's language preferences set in the system settings.

Step-by-step Guide

Here’s how you can retrieve the current language setting in iOS using Swift:

  1. Access Preferred Languages
    iOS maintains a list of preferred languages. The most preferred language will be at the top of this list:
swift
   let preferredLanguages = Locale.preferredLanguages
  1. Get the Top Language
    The first element in the preferredLanguages array represents the highest-priority language set by the user:
swift
   if let primaryLanguage = preferredLanguages.first {
       print("Primary Language: \(primaryLanguage)")
   }
  1. Working with NSLocale
    You can further explore specific locale features using NSLocale:
swift
1   let currentLocale = Locale.current
2   let languageCode = currentLocale.languageCode
3   let regionCode = currentLocale.regionCode
4
5   print("Language Code: \(languageCode ?? "Unknown")")
6   print("Region Code: \(regionCode ?? "Unknown")")

Handling Fallbacks

In scenarios where specific translations are unavailable, it's prudent to implement fallbacks:

swift
1let supportedLanguages = ["en", "fr", "es"]
2
3if supportedLanguages.contains(languageCode ?? "") {
4    // Load specific language resources
5} else {
6    // Fallback to a default language, typically English
7}

Implementing Localization

Localization with Localizable.strings

  1. Creating a Localizable Strings File
    Create a Localizable.strings file for each language your application supports. You can do this by going to File > New > File, selecting "Strings File", and naming it Localizable.strings.
  2. Adding Translations
    Each Localizable.strings file will hold key-value pairs corresponding to the text in your app.
    Example of Localizable.strings for English:
plaintext
   "welcome_message" = "Welcome to our application!";

Example of Localizable.strings for French:

plaintext
   "welcome_message" = "Bienvenue dans notre application!";
  1. Using the Localized Strings
    Load the strings in your Swift code using the NSLocalizedString function:
swift
   let welcomeMessage = NSLocalizedString("welcome_message", comment: "Welcome message for users")

Testing Localization

  • Simulator Language Change: Change the language in the iOS Simulator by navigating to Settings > General > Language & Region.
  • iOS Device: Similar to the simulator, change language settings in Settings > General > Language & Region.

Summary Table

ComponentDescriptionCode Example
LocaleRegional settings, includes language, currency, etc.Locale.current
LanguageLinguistic aspect only, like en, fr.Locale.preferredLanguages.first
Preferred LanguagesList of user's language preferences.Locale.preferredLanguages
NSLocale FeaturesLocale-specific data, more info on language, region codes.Locale.current.languageCode, Locale.current.regionCode
Localization with NSStringsUse Localizable.strings for multilingual content.NSLocalizedString("key", comment: "Description")

Conclusion

Supporting multiple languages in your iOS app can significantly enhance user engagement and broaden your app's reach. By leveraging iOS's locale and language APIs, you can easily tailor your app experience to align with your user's preferences. This article covered the technical implementation of accessing the current device language and provided tips on implementing localization effectively. Embrace localization to offer a truly globalized and inclusive app experience.


Course illustration
Course illustration

All Rights Reserved.