An implementation of our vision of dealing with localization and app language changing in a straightforward, scalable and elegant way.
- Manipulate with unlimited languages in your apps
- Get a strict guideline of localizing your views and a unified approach for whole app
// MARK: - Localizable
extension ViewController: Localizable {
func localize() {
exampleLabel.text = ...
changeLanguageButton.setTitle(..., for: .normal)
}
}
...
/// in the ViewController class
override func viewDidLoad() {
super.viewDidLoad()
localizer.add(localizable: self)
}
That's it. Every time you need to change a language every localizable observer will be updated.
Define your app languages:
// MARK: - AppLanguage
enum AppLanguage: String {
case russian = "ru"
case english = "en"
}
// MARK: - Language
extension AppLanguage: Language {
static var `default`: AppLanguage {
russian
}
var localizedPath: String {
Bundle.main.path(forResource: rawValue, ofType: "lproj").unsafelyUnwrapped
}
var localeIdentifier: String {
switch self {
case .russian:
return "ru_RU"
case .english:
return "en_US"
}
}
}
Choose a way how you update your app's global appearance. For example:
override func didSelectCell() {
localizer.refresh(language: viewModel.language)
...
}
And all Localizable observers will be re-localized after localizer.refresh(language:)
.
You can see more in the example folder
- iOS 12.1+ / macOS 10.12+ / tvOS 12.1+ / watchOS 3.1+
- Xcode 12.0
- Swift 5
- If you found a bug, open an issue.
- If you have a feature request, open an issue.
- If you want to contribute, submit a pull request.
CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:
$ gem install cocoapods
To integrate DAO into your Xcode project using CocoaPods, specify it in your Podfile
:
use_frameworks!
target "<Your Target Name>" do
pod "incetro-localizable"
end
Then, run the following command:
$ pod install
If you prefer not to use any dependency managers, you can integrate Localizable
into your project manually.
-
Open up Terminal,
cd
into your top-level project directory, and run the following command "if" your project is not initialized as a git repository:$ git init
-
Add
Localizable
as a git submodule by running the following command:$ git submodule add https://github.com/Incetro/localizable.git
-
Open the new
Localizable
folder, and drag theLocalizable.xcodeproj
into the Project Navigator of your application's Xcode project.It should appear nested underneath your application's blue project icon. Whether it is above or below all the other Xcode groups does not matter.
-
Select the
Localizable.xcodeproj
in the Project Navigator and verify the deployment target matches that of your application target. -
Next, select your application project in the Project Navigator (blue project icon) to navigate to the target configuration window and select the application target under the "Targets" heading in the sidebar.
-
In the tab bar at the top of that window, open the "General" panel.
-
Click on the
+
button under the "Embedded Binaries" section. -
You will see two different
Localizable.xcodeproj
folders each with two different versions of theLocalizable.framework
nested inside aProducts
folder.It does not matter which
Products
folder you choose from, but it does matter whether you choose the top or bottomLocalizable.framework
. -
Select the top
Localizable.framework
for iOS and the bottom one for OS X.You can verify which one you selected by inspecting the build log for your project. The build target for
Localizable
will be listed as eitherLocalizable iOS
,Localizable macOS
,Localizable tvOS
orLocalizable watchOS
. -
And that's it!
The
Localizable.framework
is automagically added as a target dependency, linked framework and embedded framework in a copy files build phase which is all you need to build on the simulator and a device.
incetro, incetro@ya.ru
DAO is available under the MIT license. See the LICENSE file for more info.