Skip to content

Commit ffaa8c8

Browse files
committed
reamde almost ready
1 parent ba639af commit ffaa8c8

File tree

2 files changed

+116
-121
lines changed

2 files changed

+116
-121
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 Tolgee
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 95 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# Tolgee Mobile Swift SDK (Alpha) 🐁
22

33
[![Tolgee](https://img.shields.io/badge/Tolgee-f06695)](https://tolgee.io/)
4-
![Android](https://img.shields.io/badge/Android-Supported-green?logo=android)
5-
![language](https://img.shields.io/github/languages/top/tolgee/tolgee-mobile-kotlin-sdk)
6-
[![github release](https://img.shields.io/github/v/release/tolgee/tolgee-mobile-kotlin-sdk?label=GitHub%20Release)](https://github.com/tolgee/tolgee-mobile-kotlin-sdk/releases/latest)
7-
[![licence](https://img.shields.io/badge/license-Apache%202%20-blue)](https://github.com/tolgee/tolgee-mobile-kotlin-sdk/blob/master/LICENSE)
8-
[![github stars](https://img.shields.io/github/stars/tolgee/tolgee-mobile-kotlin-sdk?style=social&label=Tolgee%20Mobile%20Kotlin%20SDK)](https://github.com/tolgee/tolgee-mobile-kotlin-sdk)
4+
![Android](https://img.shields.io/badge/iOS-Supported-green?logo=ios)
5+
![language](https://img.shields.io/github/languages/top/tolgee/tolgee-mobile-swift-sdk)
6+
[![github release](https://img.shields.io/github/v/release/tolgee/tolgee-mobile-swift-sdk?label=GitHub%20Release)](https://github.com/tolgee/tolgee-mobile-swift-sdk/releases/latest)
7+
![Licence](https://img.shields.io/github/license/tolgee/tolgee-mobile-swift-sdk)
8+
[![github stars](https://img.shields.io/github/stars/tolgee/tolgee-mobile-swift-sdk?style=social&label=Tolgee%20Mobile%20Swift%20SDK)](https://github.com/tolgee/tolgee-mobile-swift-sdk)
99
[![github stars](https://img.shields.io/github/stars/tolgee/tolgee-platform?style=social&label=Tolgee%20Platform)](https://github.com/tolgee/tolgee-platform)
1010
[![Github discussions](https://img.shields.io/github/discussions/tolgee/tolgee-platform)](https://github.com/tolgee/tolgee-platform/discussions)
1111
[![Dev.to](https://img.shields.io/badge/Dev.to-tolgee_i18n?logo=devdotto&logoColor=white)](https://dev.to/tolgee_i18n)
@@ -18,13 +18,19 @@
1818
## What is Tolgee?
1919

2020
[Tolgee](https://tolgee.io/) is a powerful localization platform that simplifies the translation process for your applications.
21-
This SDK provides integration for Kotlin-based projects, with a primary focus on Android.
21+
This SDK provides integration for iOS and macOS projects.
2222

23-
Currently, Android is fully supported, but any Kotlin-based codebase can in theory use this library.
24-
25-
## Features
23+
## ✨ Features
2624

27-
- **Over-the-air updates**: Update your translations without releasing a new app version
25+
- 🌍 **Remote translation loading** from Tolgee CDN with automatic updates
26+
- 📦 **Namespace-based organization** for scalable translation management
27+
- 💾 **Intelligent caching** with ETag support and background synchronization
28+
- 🔄 **Automatic fallback** to bundle-based localizations when offline
29+
- 🎯 **Smart language detection** from device settings with manual override
30+
-**Modern async/await API** for Swift concurrency
31+
- 🔧 **SwiftUI integration** with reactive updates and `TolgeeText` component
32+
- 📱 **Multi-platform support** for iOS, macOS, tvOS, and watchOS
33+
- 🔍 **Advanced debugging** with comprehensive logging
2834

2935

3036
## Installation
@@ -56,36 +62,21 @@ let greeting = Tolgee.shared.translate("hello_world")
5662
let personalGreeting = Tolgee.shared.translate("hello_name", "Alice")
5763
```
5864

59-
## ✨ Features
60-
61-
- 🌍 **Remote translation loading** from Tolgee CDN with automatic updates
62-
- 📦 **Namespace-based organization** for scalable translation management
63-
- 💾 **Intelligent caching** with ETag support and background synchronization
64-
- 🔄 **Automatic fallback** to bundle-based localizations when offline
65-
- 🎯 **Smart language detection** from device settings with manual override
66-
-**Modern async/await API** for Swift concurrency
67-
- 🧪 **ICU plural forms** support for complex pluralization rules
68-
- 🔧 **SwiftUI integration** with reactive updates and `TolgeeText` component
69-
- 📱 **Multi-platform support** for iOS, macOS, tvOS, and watchOS
70-
- 🏗️ **Type-safe** and performant with strict concurrency
71-
- 🔍 **Advanced debugging** with comprehensive logging
72-
- 🎨 **String interpolation** with format specifiers and multiple arguments
73-
7465
## 📦 Installation
7566

76-
### Swift Package Manager (Recommended)
67+
### Swift Package Manager
7768

7869
Add to your `Package.swift`:
7970

8071
```swift
8172
dependencies: [
82-
.package(url: "https://github.com/petrpavlik/tolgee-ios", from: "1.0.0")
73+
.package(url: "https://github.com/tolgee/tolgee-mobile-swift-sdk", from: "1.0.0")
8374
]
8475
```
8576

8677
Or through Xcode:
8778
1. File → Add Package Dependencies...
88-
2. Enter: `https://github.com/petrpavlik/tolgee-ios`
79+
2. Enter: `https://github.com/tolgee/tolgee-mobile-swift-sdk`
8980
3. Choose version and add to your target
9081

9182
## 🎯 Basic Usage
@@ -95,23 +86,13 @@ Or through Xcode:
9586
```swift
9687
import Tolgee
9788

98-
class AppDelegate: UIResponder, UIApplicationDelegate {
99-
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
100-
101-
// Basic initialization with automatic language detection
102-
let cdnURL = URL(string: "https://cdn.tolgee.io/your-project-id")!
103-
Tolgee.shared.initialize(cdn: cdnURL)
104-
105-
// Optional: Fetch translations immediately
106-
Task {
107-
await Tolgee.shared.remoteFetch()
108-
}
109-
110-
return true
111-
}
112-
}
89+
// Set the CDN format to Apple in your Tolgee project
90+
let cdnURL = URL(string: "https://cdn.tolgee.io/your-project-id")!
91+
Tolgee.shared.initialize(cdn: cdnURL)
11392
```
11493

94+
Refer to our SwiftUI and UIKit examples for a complete setup.
95+
11596
### Advanced Initialization
11697

11798
```swift
@@ -124,6 +105,12 @@ Tolgee.shared.initialize(
124105
)
125106
```
126107

108+
### Fetch Remote Translations
109+
You have to explicitly call the `fetch` method to fetch translations from the CDN.
110+
```swift
111+
await Tolgee.shared.remoteFetch()
112+
```
113+
127114
### Basic Translation
128115

129116
```swift
@@ -133,25 +120,15 @@ let title = Tolgee.shared.translate("app_title")
133120
// Translation with arguments
134121
let welcomeMessage = Tolgee.shared.translate("welcome_user", "John")
135122
let itemCount = Tolgee.shared.translate("items_count", 5)
136-
137-
// Translation from specific namespace/table
138-
let saveButton = Tolgee.shared.translate("save", table: "buttons")
123+
let nameAndAge = Tolgee.shared.translate("My name is %@ and I'm %lld years old", "John", 30)
139124
```
140125

141-
### Plural Forms & ICU Support
142-
143-
```swift
144-
// Automatic plural form selection based on count
145-
let appleCount1 = Tolgee.shared.translate("apple_count", 1) // "You have 1 apple"
146-
let appleCount5 = Tolgee.shared.translate("apple_count", 5) // "You have 5 apples"
147-
148-
// Complex pluralization for different languages
149-
let czechPlural = Tolgee.shared.translate("items_count", 2.5) // Handles Czech plural rules
150-
```
126+
> [!NOTE]
127+
> Strings with multiple pluralized parameters are currently **not supported**, for example `Tolgee.shared.translate("I have %lld apples and %lld oranges", 2, 3)`
151128
152-
## 🔧 SwiftUI Integration
129+
### 🔧 SwiftUI Integration
153130

154-
### TolgeeText Component
131+
Tolgee is designed to work great with SwiftUI and SwiftUI previews.
155132

156133
```swift
157134
import SwiftUI
@@ -165,47 +142,56 @@ struct ContentView: View {
165142
VStack {
166143
// Simple text translation
167144
TolgeeText("welcome_title")
168-
.font(.largeTitle)
169145

170146
// Text with arguments
171147
TolgeeText("hello_user", userName)
172-
.foregroundColor(.blue)
173148

174149
// Text with pluralization
175150
TolgeeText("item_count", itemCount)
176-
177-
// Text from specific table/namespace
178-
TolgeeText("save", tableName: "buttons")
179151
}
180152
}
181153
}
182154
```
183155

184156
### Reactive Updates
185157

158+
Tolgee provides a hook to allow the consumer of the SDK to be notified about when the translation cache has been updated.
159+
160+
```swift
161+
Task {
162+
for await _ in Tolgee.shared.onTranslationsUpdated() {
163+
// update your UI
164+
}
165+
}
166+
```
167+
168+
When using SwiftUI, Tolgee offers a convenience utility that automatically triggers a redraw of a view when the translations cache has been updated.
169+
186170
```swift
187-
struct TranslationView: View {
188-
@State private var translationText = ""
171+
struct ContentView: View {
172+
173+
// This will automatically re-render the view when
174+
// the localization cache is updated from a CDN.
175+
@StateObject private var updater = TolgeeSwiftUIUpdater()
189176

190177
var body: some View {
191-
Text(translationText)
192-
.task {
193-
// Listen for translation updates
194-
for await _ in Tolgee.shared.onTranslationsUpdated() {
195-
translationText = Tolgee.shared.translate("dynamic_content")
196-
}
197-
}
198-
.onAppear {
199-
// Initial translation
200-
translationText = Tolgee.shared.translate("dynamic_content")
201-
}
178+
VStack {
179+
TolgeeText("My name is %@ and I have %lld apples", "John", 3)
180+
}
202181
}
203182
}
204183
```
205184

185+
### Swizzling of Apple's APIs
186+
Tolgee optionally supports swizzling of `Bundle.localizedString`, which is being used by `NSLocalizedString` function. In order to enable swizzling, set enviromental variable `TOLGEE_ENABLE_SWIZZLING=true` in your scheme settings. Refer to our UIKit example.
187+
188+
206189
## 🌐 Advanced Features
207190

208-
### Namespace Management
191+
### Custom Tables/Namespaces
192+
Tolgee iOS SDK supports loading of local translations from multiple local tables by providing the `table` parameter. When using `.xcstrings` files, the names of the tables match the names of your files without the extension. You do not need to provide the table name when loading strings stored in the default `Localizable.xcstrings` file.
193+
194+
To have the OTA updates working properly, make sure that you have enabled namespaces for your Tolgee project and that you have created namespaces matching the names of your local tables.
209195

210196
```swift
211197
// Initialize with multiple namespaces for better organization
@@ -216,54 +202,34 @@ Tolgee.shared.initialize(
216202

217203
// Use translations from specific namespaces
218204
let commonGreeting = Tolgee.shared.translate("hello", table: "common")
219-
let authError = Tolgee.shared.translate("invalid_credentials", table: "auth")
220-
let profileTitle = Tolgee.shared.translate("edit_profile", table: "profile")
205+
// or for SwiftUI
206+
TolgeeText("hello", table: "common")
221207
```
222208

223-
### Background Updates
209+
### Custom Bundles
224210

225-
```swift
226-
class TranslationManager {
227-
func setupPeriodicUpdates() {
228-
Timer.scheduledTimer(withTimeInterval: 300, repeats: true) { _ in
229-
Task {
230-
await Tolgee.shared.remoteFetch()
231-
print("Translations updated at \(Date())")
232-
}
233-
}
234-
}
235-
}
236-
```
237-
238-
### Translation Status Monitoring
211+
You may have your strings resources stored in a dedicated XCFramework or a Swift Package.
239212

240213
```swift
241-
// Check initialization status
242-
if Tolgee.shared.isInitialized {
243-
print("Tolgee is ready to use")
244-
}
214+
let bundle: Bundle = ... // access the bundle
245215

246-
// Check last fetch timestamp
247-
if let lastFetch = Tolgee.shared.lastFetchDate {
248-
print("Last update: \(lastFetch)")
249-
} else {
250-
print("No remote fetch performed yet")
251-
}
216+
// Use the SDK directly
217+
let commonGreeting = Tolgee.shared.translate("hello", bundle: bundle)
218+
// or for SwiftUI
219+
TolgeeText("hello", bundle: bundle)
252220
```
253221

254-
### Offline-First with Fallbacks
222+
### Log Forwarding
223+
Tolgee allows forwarding of logs that are printed to the console by default.
224+
You can use this feature to forward errors and other logs into your analytics.
255225

256226
```swift
257-
// Tolgee automatically falls back to bundle-based localizations
258-
// when remote translations are unavailable
259-
260-
// 1. First tries remote translations from CDN
261-
// 2. Falls back to cached translations
262-
// 3. Finally falls back to bundle-based Localizable.strings
263-
264-
let text = Tolgee.shared.translate("offline_message") // Always works
227+
for await logMessage in Tolgee.shared.onLogMessage() {
228+
// Here you can forward logs from Tolgee SDK to your analytics SDK.
229+
}
265230
```
266231

232+
267233
## 📱 Platform Support
268234

269235
| Platform | Minimum Version |
@@ -276,15 +242,24 @@ let text = Tolgee.shared.translate("offline_message") // Always works
276242
## ⚙️ Requirements
277243

278244
- **Swift:** 6.0+
279-
- **Xcode:** 15.0+
245+
- **Xcode:** 16.3+
246+
247+
## 🧵 Thread-safety
248+
249+
Tolgee SDK is designed to be used synchronously on the main actor (except the `fetch` method). Access to the SDK from other actors generally has to be awaited.
250+
251+
```swift
252+
Task.deattached {
253+
// notice that the call has to be awaited outside of the main actor
254+
let str = await Tolgee.shared.translate("key")
255+
}
256+
```
280257

281258
## 🤝 Why Choose Tolgee?
282259

283260
**Tolgee saves a lot of time** you would spend on localization tasks otherwise. It enables you to provide **perfectly translated software**.
284261

285262
### All-in-one localization solution for your iOS application 🙌
286-
### Out-of-box in-context localization 🎉
287-
### Automated screenshot generation 📷
288263
### Translation management platform 🎈
289264
### Open-source 🔥
290265

@@ -293,20 +268,19 @@ let text = Tolgee.shared.translate("offline_message") // Always works
293268
## 📚 Examples & Demos
294269

295270
Check out our example projects:
296-
- [Basic iOS App](examples/basic-ios)
297-
- [SwiftUI Integration](examples/swiftui-demo)
298-
- [Multi-platform App](examples/multiplatform)
271+
- [SwiftUI Integration](Examples/TolgeeSwiftUIExample)
272+
- [UIKit Integration](Examples/TolgeeUIKitExample)
299273

300274
## 🆘 Need Help?
301275

302276
- 📖 [Documentation](https://docs.tolgee.io)
303-
- 💬 [Community Discord](https://discord.gg/tolgee)
277+
- 💬 [Community Slack](https://tolg.ee/slack)
304278
- 🐛 [Report Issues](https://github.com/petrpavlik/tolgee-ios/issues)
305279
- 💡 [Feature Requests](https://github.com/petrpavlik/tolgee-ios/discussions)
306280

307281
## 🏗️ Contributing
308282

309-
Contributions are welcome! Please read our [Contributing Guide](CONTRIBUTING.md) for details on our code of conduct and the process for submitting pull requests.
283+
Contributions are welcome!
310284

311285
## 📄 License
312286

0 commit comments

Comments
 (0)