Localization & Translations
The Reachu SDK supports multiple languages through a configurable translation system. You can define translations directly in your configuration JSON file or in a separate file.
Overview
The localization system provides:
- Multi-language supportfor all SDK components
- Separate translation filesto keep config clean
- Default English translationsbuilt-in
- Fallback languagesupport
- Easy key managementwith centralized translation keys
Configuration
Option 1: Separate Translation File (Recommended)
To keep your configuration file clean, use a separate file for translations:
reachu-config.json:
reachu-config.json
{
"apiKey": "your-api-key",
"environment": "sandbox",
"localization": {
"defaultLanguage": "es",
"fallbackLanguage": "en",
"translationsFile": "reachu-translations"
}
}
reachu-translations.json(in the same folder):
reachu-translations.json
{
"translations": {
"en": {
"cart.title": "Cart",
"cart.empty": "Your cart is empty",
"checkout.title": "Checkout"
},
"es": {
"cart.title": "Carrito",
"cart.empty": "Tu carrito está vacío",
"checkout.title": "Checkout"
},
"no": {
"cart.title": "Handlekurv",
"cart.empty": "Handlekurven din er tom",
"checkout.title": "Kasse"
}
}
}
Option 2: Inline Translations (For Few Translations)
If you prefer everything in one file:
reachu-config.json
{
"apiKey": "your-api-key",
"environment": "sandbox",
"localization": {
"defaultLanguage": "es",
"fallbackLanguage": "en",
"translations": {
"en": {
"cart.title": "Cart",
"checkout.title": "Checkout"
},
"es": {
"cart.title": "Carrito",
"checkout.title": "Checkout"
}
}
}
}
File Structure
YourApp/
Configuration/
reachu-config.json ← Main configuration
reachu-translations.json ← Translations (optional)
Configuration Properties
defaultLanguage: Default language (e.g., "en", "es", "no", "sv")fallbackLanguage: Fallback language if translation missing (default: "en")translationsFile: Name of external translation file (without .json extension)translations: Object with translations by language (optional if usingtranslationsFile)
Usage in Code
Option 1: Helper Function (Recommended)
ContentView.swift
import ReachuCore
struct ContentView: View {
var body: some View {
VStack {
// Use helper function
Text(RLocalizedString("cart.title"))
// With default value
Text(RLocalizedString("cart.title", defaultValue: "Cart"))
}
}
}
Option 2: Direct Access
ContentView.swift
import ReachuCore
struct ContentView: View {
var body: some View {
VStack {
// Direct access
Text(ReachuLocalization.shared.getString("cart.title"))
// With default value
Text(ReachuLocalization.shared.getString("cart.title", defaultValue: "Cart"))
}
}
}
Option 3: Change Language Dynamically
SettingsView.swift
import ReachuCore
struct SettingsView: View {
@State var selectedLanguage = "en"
var body: some View {
Form {
Picker("Language", selection: $selectedLanguage) {
Text("English").tag("en")
Text("Español").tag("es")
Text("Norsk").tag("no")
}
.onChange(of: selectedLanguage) { newLanguage in
ReachuLocalization.shared.setLanguage(newLanguage)
}
}
}
}
Available Translation Keys
Common
common.addToCart- "Add to Cart"common.remove- "Remove"common.close- "Close"common.cancel- "Cancel"common.confirm- "Confirm"common.continue- "Continue"common.back- "Back"common.next- "Next"common.done- "Done"common.loading- "Loading..."common.error- "Error"common.success- "Success"common.retry- "Retry"common.apply- "Apply"common.save- "Save"common.edit- "Edit"common.delete- "Delete"
Cart
cart.title- "Cart"cart.empty- "Your cart is empty"cart.emptyMessage- "Add products to continue with checkout"cart.itemCount- "Items"cart.items- "items"cart.item- "item"cart.quantity- "Quantity"cart.subtotal- "Subtotal"cart.total- "Total"cart.shipping- "Shipping"cart.tax- "Tax"cart.discount- "Discount"cart.removeItem- "Remove item"cart.updateQuantity- "Update quantity"
Checkout
checkout.title- "Checkout"checkout.proceed- "Proceed to Checkout"checkout.initiatePayment- "Initiate Payment"checkout.completePurchase- "Complete Purchase"checkout.purchaseComplete- "Purchase Complete!"checkout.purchaseCompleteMessage- "Your order has been confirmed..."checkout.paymentFailed- "Payment Failed"checkout.paymentFailedMessage- "Your payment could not be processed..."checkout.tryAgain- "Try Again"checkout.goBack- "Go Back"checkout.processingPayment- "Processing Payment"checkout.processingPaymentMessage- "Please complete your payment..."
Address
address.shipping- "Shipping Address"address.billing- "Billing Address"address.firstName- "First Name"address.lastName- "Last Name"address.email- "Email"address.phone- "Phone"address.address- "Address"address.city- "City"address.state- "State"address.zip- "ZIP"address.country- "Country"