With Swift-I18n, you can give users the possibility to make your app's language different from the system's. Language can be changed from inside the app, and if you use locTitleKey in UIKit or LocText in SwiftUI, language change will be automatically reflected on all the UI elements that are already loaded and rendered on the screen.
- Xcode 8.0
- Swift 5.0
- iOS 9.0+
SwiftI18n is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'SwiftI18n/I18n'
pod 'SwiftI18n/I18n+Case'SwiftI18n is also available through SPM. To install it, simply add the following line to your Package.swift file:
.package(url: "git@github.com:infinum/ios-swiftI18n.git", .upToNextMajor(from: "1.3.0"))To run the example project, clone the repo, and run pod install from the Example directory first.
Add:
import SwiftI18nSet or get currently selected language from:
I18nManager.instance.languageSet available languages and default language:
I18nManager.instance.availableLanguages = ["en_US", "hr"]
I18nManager.instance.defaultLanguage = "hr"If availableLanguages are present, and defaultLanguage isn't, language is initialy set to language most preferable by the user from availableLanguages.
Handling language change:
Subscribe to I18nManager.subscribeForLocaleDidChange(block: { ... } -> -> NSObjectProtocol
or if you prefer notifications, you can use:
NSNotification.Name.loc_LanguageDidChangeNotification
someLabel.locTitleKey = "some_key"
someButton.locAccessibilityLabelKey = "button_accessibility_label_key"
someButton.locAccessibilityHintKey = "button_accessibility_hint_key"Now when language changes, your someLabel's title, accessibility label and hint, automaticaly change. Isn't that great!?
Supported elements:
UILabelUIButtonUITextFiledUITextViewUIViewControllerUIBarButtonItemUITabBarItemUINavigationItem
And another important thing, all of those locTitleKeys, locAccessibilityLabelKeys, locAccessibilityHintKeys are supported in Storyboards as @IBInspectable .
Using constructor where key is of String type:
LocText("some_key")Just copy/paste this extension somewhere in your project
extension LocText {
init(key: Strings) {
self.init(key.rawValue)
}
}Sometimes you have to work with translations that are all lowercased, but you want them to be uppercased or maybe capitalized. To handle this use:
pod 'SwiftI18n/I18n+Case'Now you have an enum:
enum I18nCaseTransform: String {
case uppercased = "up"
case lowercased = "low"
case capitalized = "cap"
}which you can use like this:
someButton.setCaseTransform(.uppercased, for: .normal)You can also set case transform in Storyboards
In some scenarios, a requested localization may not be available for a given language. To ensure a consistent user experience, the app provides a fallback language mechanism. This feature guarantees that if a translation is missing in the preferred language, a predefined fallback language will be used to retrieve the localization instead.
You can configure the fallback language by setting the fallbackLanguage property in your localization manager.
Simply use the following line:
I18nManager.instance.fallbackLanguage = "en_gb"For SwiftI18n to work beautifully with Strings enum from Polyglot, you will need to copy PolyglotSwiftI18Extensions.swift into your project.
By doing this you can now set translations to your UI elements with ease:
someLabel.loc.titleKey = .somePolygotKey
someButton.loc.accessibilityLabelKey = .somePolyglotKey
someButton.loc.accessibilityLabelHint = .somePolyglotKeyAttributed strings
SwiftI18n does not collect any user data. We have provided a privacy manifest file that can be included in your app.
We believe that the community can help us improve and build better a product. Please refer to our contributing guide to learn about the types of contributions we accept and the process for submitting them.
To ensure that our community remains respectful and professional, we defined a code of conduct that we expect all contributors to follow.
We appreciate your interest and look forward to your contributions.
Copyright 2024 Infinum
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Maintained and sponsored by Infinum.