Skip to content

Commit 8efd070

Browse files
authored
Merge pull request #81 from transifex/feature/init-localized
`String.init(localized:...)` support
2 parents c274ae2 + 40ec640 commit 8efd070

File tree

7 files changed

+894
-17
lines changed

7 files changed

+894
-17
lines changed

CHANGELOG.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ logic now normalizes the locale name to match the format that iOS accepts.
145145

146146
* Minor documentation updates.
147147

148-
## Transifex iOS SDK 2.0.6
148+
## Transifex iOS SDK 2.0.7
149149

150150
*February 19, 2025*
151151

@@ -154,3 +154,13 @@ logic now normalizes the locale name to match the format that iOS accepts.
154154
* Logging improvements.
155155
* `TXCDSError` enum improvements.
156156
* Addresses issue with attributed string creation.
157+
158+
## Transifex iOS SDK 2.0.8
159+
160+
*March 11, 2025*
161+
162+
* `String(localized:...)` support.
163+
* Updates `README.md` by adding a 'Supported localization methods' section and
164+
updating the 'Limitations' section.
165+
* Refactors `BypassLocalizer` class.
166+
* Updates `TXNativeExtensions.swift`.

README.md

+79-2
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,14 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
9797
For Swift projects, you will also need to copy the `TXNativeExtensions.swift` file in
9898
your project and include it in all of the targets that call any of the following Swift methods:
9999

100-
* `String.localizedStringWithFormat(format:...)`
101-
* `NSString.localizedStringWithFormat(format:...)`
100+
* `NSString.localizedStringWithFormat(_ format:, _ args:)`
101+
* `String.localizedStringWithFormat(_ format:, _ arguments:)`
102+
* [⚠️](#stringlocalized-initializers) `String.init(localized:)`
103+
* [⚠️](#stringlocalized-initializers) `String.init(localized:options:)`
104+
* [⚠️](#stringlocalized-initializers) `String.init(localized:defaultValue:table:bundle:locale:comment:)`
105+
* [⚠️](#stringlocalized-initializers) `String.init(localized:defaultValue:options:table:bundle:locale:comment:)`
106+
* [⚠️](#stringlocalized-initializers) `String.init(localized:table:bundle:locale:comment:)`
107+
* [⚠️](#stringlocalized-initializers) `String.init(localized:options:table:bundle:locale:comment:)`
102108

103109
If none of your application targets call any of the above methods, then you don't need to
104110
add this file to your project.
@@ -171,6 +177,36 @@ TXNative.initialize(
171177
token:@"<transifex_token>"];
172178
```
173179
180+
### Supported localization methods
181+
182+
On runtime, Transifex SDK overrides / swizzles the following localization
183+
methods and structures in order to ensure that the application source code does
184+
not require any changes:
185+
186+
* `NSLocalizedString(_ key:tableName:bundle:value:comment:)`
187+
* `NSString.localizedStringWithFormat(_ format:, _ args:)`
188+
* `String.localizedStringWithFormat(_ format:, _ arguments:)`
189+
* `-[NSString localizedStringWithFormat:]`
190+
* `NSBundle.localizedString(forKey:value:table:)`
191+
* `-[NSBundle localizedAttributedStringForKey:value:table:]`
192+
* Any SwiftUI view initializer that accepts a `LocalizedStringKey` struct.
193+
* [⚠️](#stringlocalized-initializers) `String.init(localized:)`
194+
* [⚠️](#stringlocalized-initializers) `String.init(localized:options:)`
195+
* [⚠️](#stringlocalized-initializers) `String.init(localized:defaultValue:table:bundle:locale:comment:)`
196+
* [⚠️](#stringlocalized-initializers) `String.init(localized:defaultValue:options:table:bundle:locale:comment:)`
197+
* [⚠️](#stringlocalized-initializers) `String.init(localized:table:bundle:locale:comment:)`
198+
* [⚠️](#stringlocalized-initializers) `String.init(localized:options:table:bundle:locale:comment:)`
199+
200+
Developers are also able to use the public methods of `TXNative` class if they
201+
want to (`t(_:)`, `localizedString(format:arguments:)`, `translate(...)`.
202+
203+
> [!WARNING]
204+
>
205+
> Please note that if the 'Use Compiler to Extract Swift Strings' build settting
206+
> is enabled (`SWIFT_EMIT_LOC_STRINGS`) and you use any of the `TXNative` publicly
207+
> provided methods, the strings will not be automatically collected when building
208+
> the application.
209+
174210
### Fetching translations
175211
176212
As soon as `fetchTranslations` is called, the SDK will attempt to download the
@@ -367,6 +403,47 @@ public `TXStandardLogHandler` class to control the log level printed to the cons
367403

368404
## Limitations
369405

406+
### `String(localized:)` initializers
407+
408+
While the `String(localized:)` family of initializers ([^1] [^2] [^3] [^4] [^5] [^6])
409+
is supported through the `TXNativeExtensions.swift` file, we do not recommend using
410+
them due to their reliance on reflection and regular expressions to extract the
411+
`LocalizationValue` properties.
412+
413+
Developers can choose to switch between the Reflection and the Regular Expression
414+
extraction logic via the `extractionType` argument of the `TXNative.translate(...)`
415+
methods used in the `TXNativeExtensions.swift` file.
416+
417+
> [!WARNING]
418+
>
419+
> **Risk of Breaking Changes**
420+
>
421+
> Apple may change the internal representation of the `LocalizationValue`
422+
> struct at any time, which would break the implemented logic.
423+
424+
We strongly recommend using any of the following supported methods instead of
425+
the `String(localized:)` initializers:
426+
427+
* `NSLocalizedString(_ key:tableName:bundle:value:comment:)`
428+
* `NSString.localizedStringWithFormat(_ format:, _ args:)`
429+
* `String.localizedStringWithFormat(_ format:, _ arguments:)`
430+
* `-[NSString localizedStringWithFormat:]`
431+
* `NSBundle.localizedString(forKey:value:table:)`
432+
* `-[NSBundle localizedAttributedStringForKey:value:table:]`
433+
* Any SwiftUI view initializer that accepts a `LocalizedStringKey` struct.
434+
435+
If you understand the risks, you may uncomment the relevant section in
436+
`TXNativeExtensions.swift` after adding the file to your application’s
437+
target(s). However, we strongly advise using the recommended
438+
alternatives whenever possible.
439+
440+
[^1]: https://developer.apple.com/documentation/swift/string/init(localized:)
441+
[^2]: https://developer.apple.com/documentation/swift/string/init(localized:options:)
442+
[^3]: https://developer.apple.com/documentation/swift/string/init(localized:defaultvalue:table:bundle:locale:comment:)
443+
[^4]: https://developer.apple.com/documentation/swift/string/init(localized:defaultvalue:options:table:bundle:locale:comment:)
444+
[^5]: https://developer.apple.com/documentation/swift/string/init(localized:table:bundle:locale:comment:)
445+
[^6]: https://developer.apple.com/documentation/swift/string/init(localized:options:table:bundle:locale:comment:)
446+
370447
### Special cases
371448

372449
Localized strings that are being managed by the OS are not supported by the Transifex

0 commit comments

Comments
 (0)