How can I change locale programmatically with Swift
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Swift, you usually do not change the device locale from inside the app. What you can change programmatically is the locale used by your own formatting, parsing, sorting, or view logic. That distinction matters because system locale is a user setting, while formatter locale is an application choice.
If your goal is to display a date in French, format currency in Canada, or preview another region for testing, create the appropriate Locale and assign it where needed. Do not try to override the phone’s global regional settings.
Using Locale With Formatters
Most locale changes happen through DateFormatter, NumberFormatter, or related APIs.
That changes how the value is displayed without changing any system setting.
The same pattern applies to dates:
Choosing a Locale Identifier
Locale identifiers usually follow a language and region pattern such as en_US, en_GB, fr_CA, or ja_JP. The exact locale affects separators, month names, currency symbols, and other formatting conventions.
You can inspect the current locale at runtime:
Locale.current reflects the locale at the time it is read. Locale.autoupdatingCurrent tracks system changes while the app is running.
Applying Locale to Specific Operations
Locale is not just for display. It also affects parsing and comparison behavior in some APIs.
The en_US_POSIX locale is commonly used for stable machine-readable date parsing because it avoids user-region surprises.
Locale in SwiftUI
In SwiftUI, you can inject a locale into the view environment.
That changes how the view renders localized values without affecting the rest of the system.
What You Cannot Reliably Change
An app cannot generally force the user’s entire device locale to a new value. Older workarounds involving UserDefaults keys such as Apple language settings are brittle and not recommended for normal application behavior.
If your app supports an in-app language or region preference, store that preference yourself and apply the selected locale to the parts of the UI that need it.
Common Pitfalls
The biggest mistake is assuming Locale changes translation strings automatically. Locale affects formatting and some text behavior, but localized strings usually come from your app’s localization resources.
Another issue is using the current user locale for fixed-format machine data. That can break date parsing and server communication. Use a stable locale such as en_US_POSIX for machine formats.
Be careful when caching formatters. If the locale should respond to user preference changes, cached formatters may need to be rebuilt.
Finally, do not confuse locale with language, calendar, or time zone. They are related concepts, but they are not interchangeable.
Summary
- In Swift, you usually set locale on formatters or views, not on the entire device.
- Use
Locale(identifier:)to choose a specific regional format. - Assign that locale to
DateFormatter,NumberFormatter, or SwiftUI environment values. - Use
en_US_POSIXfor stable machine-readable parsing. - Treat system locale as a user preference rather than something the app should override globally.

