Add customisation point for replacing NSLocalizedString
implementation
#841
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hello 👋 We've used R.swift for years now in our project and I'm currently looking at migrating from 5.x to 7.x since we want to migrate our project to SPM in the near future.
In our project, we don't rely on the
NSLocalizedString
method for two reasons:es_MX
and our implementation can look ines_419
ores
if the phrase is missing)R.string(preferredLanguages: [ ... ])
In v5 of R.swift, we were able to use our custom implementation with the R.generated.swift code by providing an overload to
NSLocalizedString(_:tableName:bundle:value:comment:)
in the same module of the generatedR.string
struct. It wasn't great, but it did the job 😄Looking into v7 however, it's not possible for us to use a similar trick because the resolution of a localized string has now moved into the RswiftResources module and therefor our overloaded implementation of
NSLocalizedString(_:tableName:bundle:value:comment:)
won't be called.I've spent a bit of time trying to come up with various alternatives. I first started trying to just reimplement RswiftResources myself but this started getting complicated and I couldn't help but feel like I was having to write a lot of code that I didn't want to. It also meant that I had to update the generated code after running
rswift
:My fallback option is to just swizzle
NSLocalizedString
and replace the implementation with our own however this is my least favourite option for a lot of reasons 😄 I'm also not entirely sure if it's possible to achieve it without messing up other frameworks that call toNSLocalizedString
(i.e UIKit internals).Before going with swizzling, I have one last proposed idea that is this Pull Request. In this PR, i've added a public
RswiftLocalizedString
global property:By default, it calls to Foundation's
NSLocalizedString
method , which is what 99.99% of users will want. But it provides me with a slightly nicer way to hook in and replace the implementation without swizzling at hopefully a very minimal cost in terms of maintenance within this project 🤞.Please do let me know what you think though. I understand that this might need some more discussion but I opened a PR rather than an Issue to hopefully provide more context of what i'm trying to achieve. I guess that another option could be to make R.swift's
preferredLanguages
feature more powerful but my hunch is that it wouldn't be a simple challenge.Thanks again for everything so far!