Skip to content

Commit

Permalink
Introduce t() method
Browse files Browse the repository at this point in the history
A new public static method has been introduced in the `TXNative` class,
allowing developers to programmatically trigger the translation
mechanism of Transifex SDK in cases where this is not supported.

For example, in SwiftUI, where the swizzling mechanism of Transifex SDK
does currently work, developers can replace views like this:

```
Text("test string")
```

with this:

```
Text(TXNative.t("test string"))
```

The method uses the default locale that Transifex SDK has been
configured with.

Note: If you want to change the current locale used by the
SwiftUI preview to test different locales, click on the 'Product' menu
item and then visit 'Scheme' > 'Edit Scheme...' > 'Run' > 'Options' >
'App Language' and pick the language you want to test.
  • Loading branch information
stelabouras committed Jul 7, 2023
1 parent 5ba6b0e commit 9f7b6e5
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Sources/Transifex/Core.swift
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,23 @@ token: \(token)
)
}

/// Helper method used when translation is not possible (e.g. in SwiftUI views).
///
/// This method applies the translation using the currently selected locale. For pluralization use the
/// `localizedString(format:arguments:)` method.
///
/// Make sure that this method is called after the SDK has been initialized, otherwise
/// "<SDK NOT INITIALIZED>" string will be shown instead.
///
/// - Parameter sourceString: The source string to be translated
/// - Returns: The translated string
public static func t(_ sourceString: String) -> String {
return tx?.translate(sourceString: sourceString,
localeCode: nil,
params: [:],
context: nil) ?? "<SDK NOT INITIALIZED>"
}

/// Used by the Swift localizedString(format:arguments:) methods found in the
/// TXExtensions.swift file.
public static func localizedString(format: String,
Expand Down

0 comments on commit 9f7b6e5

Please sign in to comment.