To see Localazy SDK in action, you can check our demo project here.
Localazy SDK for iOS and macOS is distributed with Swift Package Manager as a binary package.
Add this URL to Swift Packages in your Project file (Xcode 12 and upwards).
https://github.com/localazy/localazy-swift
Minimum target:
- iOS 13
- macOS 10.15
To install Localazy SDK manually, copy Localazy.xcframework
into your project root (Xcode) and add it to the Frameworks, Libraries and Embedded Content section.
In your code you can import Localazy by adding:
#if os(macOS)
import Localazy_macOS
#else
import Localazy_iOS
#endif
For macOS targets you need to ensure Outgoing connections (Client)
and Disable Library Validation
entitlement is checked in Signing & Capabilities (App Sandbox)
section.
To configure Localazy SDK for iOS and macOS create and set up Localazy.plist
configuration file. You can copy Localazy.plist
from this package to your Xcode project.
Key | Type | Description | Default |
---|---|---|---|
readKey |
String |
Your Localazy read key. (Can be retrieved from settings.) | null |
tag |
String |
Release tag to be used to fetch translations. | latest |
statistics |
Dictionary |
Statistics configuration dictionary allows Localazy to optimize its behaviour and prioritize translations. (see table below) | - |
Key | Type | Description | Default |
---|---|---|---|
statsEnabled |
Boolean |
Should the library send stats | true |
updateInterval |
Number |
Update interval in ms | 86400 |
updateIntervalForFailure |
Number |
Update interval for failure in ms | 14400 |
statsInterval |
Number |
Send stats interval in ms | 86400 |
statsIntervalForFailure |
Number |
Send stats interval for failure in ms | 14400 |
updateDelay |
Number |
Update delay in ms | 0 |
statsDelay |
Number |
Stats delay in ms | 0 |
minimalStatsSize |
Number |
Minimal stats size in bytes | 2048 |
maximumStatsSize |
Number |
Maximum stats size in bytes | 262144 |
sendStatsRegularly |
Boolean |
Send stats regularly | false |
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>readKey</key>
<string>Your-Read-Key</string>
<key>tag</key>
<string>Your-Tag</string>
<key>updateInterval</key>
<integer>3600</integer>
<key>statistics</key>
<dict>
<key>statsEnabled</key>
<true/>
<key>sendStatsRegularly</key>
<true/>
<key>statsInterval</key>
<integer>900</integer>
</dict>
</dict>
</plist>
You can ensure your local strings are uploaded on every build by creating new Run Script Phase for your target:
localazy upload
For more info check Installation – Localazy CLI & Upload Reference - Localazy CLI.
To start using Localazy SDK, access singleton instance by:
Localazy.shared
"Hello".localazyLocalized
Text(localazyKey: "Hello")
Bundle.swizzleLocalizationWithLocalazy()
NSLocalizedString("Hello", comment: "Hello")
NOTE: By using swizzling its important to understand it will replace all calls to
NSLocalizedString
with the custom logic provided by Localazy SDK and will only fallback to system call if there is no Localazy based translation. For this reasons, there may be more logs added to statistics about the missing and used keys.
forceReload
: Invalidate internal caches and reload data from serverforceLocale
: Set locale independently from system settings (allows for custom language selectors)getString
: Get translated StringgetArrayList
: Translated ListgetPlural
: Translated String with pluralssetEnabled
: If Localazy is disabled, translations are obtained by the default system mechanism.getProjectUrl
: Project URLgetCurrentLocale
: Current system localegetCurrentLocalazyLocale
: Current Localazy localeisFullyTranslated
: To get information about whether the current internally resolved locale is fully translatedgetLocales
: To list all locales known to LocalazyisStatsEnabled
: Statistics are enabledsetStatsEnabled
: Enable / disable statistics
We provide several types of notifications which can be produced by the SDK. These notifications are published as a NSNotification
objects and can be observed by NotificationCenter. Some of these notifications can contain userInfo
dictionary to provide more context for you.
localazyMissingTextFound
userInfo
dictionary:
[
"id": LocalazyID,
"locale": LocalazyLocale,
"key": String
]
localazyStringsUpdateStarted
localazyStringsUpdateFinished
localazyStringsUpdateFailed
userInfo
dictionary:
[
“error”: Localazy.LocalazyError
]
localazyStringsUpdateNotNecessary
localazyStringsLoaded