Library for localizing UI of application on the fly
ACLocalized
Library for localizing UI of application on the fly.
Requirements
- IOS 11 or above
- Xcode 12.5 or above
Core
Singleton class for general bundle settings, locale and language.
public class ACLocalizedCore {
public static let shared = ACLocalizedCore()
public var coreBundle: Bundle
public var defaultLanguage: ACLocalizedLanguage
public var language: ACLocalizedLanguage
public var supportedLanguages: [ACLocalizedLanguage]
public func localizedString(key: String, table: String, args: CVarArg...) -> String
}
Strings
The library provides several types of localized strings. Each of them corresponds to the general protocol ACLocalizedStringProtocol. Which provides the translation of a localized string to String and NSAttributedString.
public protocol ACLocalizedStringProtocol {
func toString() -> String
func toAttributedString() -> NSAttributedString
}
ACLocalizedKeyString
Localized string that takes its value from localization tables by key.
public struct ACLocalizedKeyString {
public let key: String
public let table: String
public let args: [CVarArg]
}
The string is received using the method from ACLocalizedCore:
extension ACLocalizedKeyString: ACLocalizedStringProtocol {
public func toString() -> String {
ACLocalizedCore.shared.localizedString(key: self.key, table: self.table, args: self.args)
}
public func toAttributedString() -> NSAttributedString {
NSAttributedString(string: self.toString())
}
}
ACLocalizedAttributedString
Localized string with attributes.
public struct ACLocalizedAttributedString {
public let string: ACLocalizedStringProtocol
public let attributes: [NSAttributedString.Key: Any]?
}
Any string corresponding to the ACLocalizedStringProtocol can be used as the string parameter. Attributes will be applied to the result from toString():
extension ACLocalizedAttributedString: ACLocalizedStringProtocol {
public func toString() -> String {
self.string.toString()
}
public func toAttributedString() -> NSAttributedString {
NSAttributedString(string: self.toString(), attributes: self.attributes)
}
}
If the value of the string parameter is ACLocalizedAttributedString then its attributes will be ignored.
ACLocalizedString
Represents a composition of various strings corresponding to the protocol ACLocalizedStringProtocol.
public struct ACLocalizedString {
public var strings: [ACLocalizedStringProtocol]
}
Strings can contain any value. When translated into a string, they will be combined:
extension ACLocalizedString: ACLocalizedStringProtocol {
public func toString() -> String {
self.strings.reduce("", { $0 + $1.toString() })
}
public func toAttributedString() -> NSAttributedString {
let result = NSMutableAttributedString()
self.strings.forEach({ result.append($0.toAttributedString()) })
return result
}
}
Operations are also available:
ACLocalizedString=ACLocalizedString+ACLocalizedStringACLocalizedString+=ACLocalizedStringACLocalizedString= “(ACLocalizedString)” (Interpolation)
UIKit
To support on the fly localization each UI-component implements properties that store localized strings. For example:
public extension UILabel {
var textLocalized: ACLocalizedString?
var attributedTextLocalized: ACLocalizedString?
}
When setting one of the localized properties, the localized string value will be set to the corresponding native property. For example, textLocalized corresponds to text. And when calling the <component_prefix>LocalizeIfNeeded() method, all set properties will be localized.
For automatic localization, it is enough to create a base class for the UIViewController from which the rest of the viewControllers will be inherited. For example:
class AppViewController: UIViewController, ACLocalizedResponderProtocol {
func applyLocalize() {}
func didLocalized() {}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.viewControllerLocalizeIfNeeded()
}
}
Starting from viewControllerLocalizeIfNeeded() the <component_prefix>LocalizeIfNeeded() methods will be called for all UI-components:
func viewControllerLocalizeIfNeeded() {
self.objectLocalizeIfNeeded()
self.tabBarController?.tabBar.tabBarLocalizeIfNeeded()
self.navigationController?.navigationBar.navigationBarLocalizeIfNeeded()
self.navigationItem.objectLocalizeIfNeeded()
self.tabBarItem.objectLocalizeIfNeeded()
self.view.viewLocalizeIfNeeded()
}
SwiftGen Template
Documentation on template generation can be found here. Template files are here.
Demo
Small project demonstrating the interaction of ACLocalized modules in the application.
Install
Swift Package Manager(SPM) is Apple’s dependency manager tool. It is now supported in Xcode 11. So it can be used in all appleOS types of projects. It can be used alongside other tools like CocoaPods and Carthage as well.
Install package into your packages
Add a reference and a targeting release version in the dependencies section in Package.swift file:
import PackageDescription
let package = Package(
name: "<your_project_name>",
products: [],
dependencies: [
.package(url: "https://github.com/DPLibs/appcraft-localized-ios.git", from: "<current_version>")
]
)
Install package via Xcode
- Go to
File->Swift Packages->Add Package Dependency... - Then search for https://github.com/DPLibs/appcraft-localized-ios.git
- And choose the version you want
License
Distributed under the MIT License.
Author
Email: dmitriyap11@gmail.com